Saturday, December 18, 2010

REPORT  ZTEST_PRG.
DATA:
    BEGIN OF IT_T100 OCCURS 0,
      ARBGB TYPE T100-ARBGB,
      MSGNR TYPE T100-MSGNR,
      TEXT TYPE T100-TEXT,
    END OF IT_T100.
"Variables
DATA :
    G_SENT_TO_ALL   TYPE SONV-FLAG,
    G_TAB_LINES     TYPE I,
    G_ATTACHMENT_LINES TYPE I.
"Types
TYPES:
    T_DOCUMENT_DATA  TYPE  SODOCCHGI1,
    T_PACKING_LIST   TYPE  SOPCKLSTI1,
    T_ATTACHMENT     TYPE  SOLISTI1,
    T_BODY_MSG       TYPE  SOLISTI1,
    T_RECEIVERS      TYPE  SOMLRECI1.
"Workareas
DATA :
    W_DOCUMENT_DATA  TYPE  T_DOCUMENT_DATA,
    W_PACKING_LIST   TYPE  T_PACKING_LIST,
    W_ATTACHMENT     TYPE  T_ATTACHMENT,
    W_BODY_MSG       TYPE  T_BODY_MSG,
    W_RECEIVERS      TYPE  T_RECEIVERS.
"Internal Tables
DATA :
    I_DOCUMENT_DATA  TYPE STANDARD TABLE OF T_DOCUMENT_DATA,
    I_PACKING_LIST   TYPE STANDARD TABLE OF T_PACKING_LIST,
    I_ATTACHMENT     TYPE STANDARD TABLE OF T_ATTACHMENT,
    I_BODY_MSG       TYPE STANDARD TABLE OF T_BODY_MSG,
    I_RECEIVERS      TYPE STANDARD TABLE OF T_RECEIVERS.
PARAMETERS:P_MAIL(99) TYPE C OBLIGATORY.

START-OF-SELECTION.
  SELECT * FROM T100 INTO CORRESPONDING FIELDS OF TABLE IT_T100 UP TO 20 ROWS WHERE SPRSL = SY-LANGU.
  CLASS CL_ABAP_CHAR_UTILITIES DEFINITION LOAD.
  CONSTANTS:
            CON_TAB  TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB,
            CON_CRET(2)        TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>CR_LF.
  "Header data
  CONCATENATE 'ARBGB' 'MSGNR' 'TEXT'  INTO  W_ATTACHMENT SEPARATED BY  CON_TAB.
  APPEND W_ATTACHMENT TO I_ATTACHMENT.

  CLEAR  W_ATTACHMENT.
  LOOP AT IT_T100.
    CONCATENATE IT_T100-ARBGB IT_T100-MSGNR IT_T100-TEXT INTO W_ATTACHMENT SEPARATED BY CON_TAB.
    CONCATENATE CON_CRET W_ATTACHMENT INTO W_ATTACHMENT.
    APPEND W_ATTACHMENT TO I_ATTACHMENT.
    CLEAR  W_ATTACHMENT.
  ENDLOOP.

  "Subject of the Mail.
  W_DOCUMENT_DATA-OBJ_NAME  = 'MAI_TO_HEAD'.
  W_DOCUMENT_DATA-OBJ_DESCR = 'Regarding MAI Program by SAP ABAP'.
  "Body of the MAI
  W_BODY_MSG = 'Hi,'.
  APPEND W_BODY_MSG TO I_BODY_MSG.
  CLEAR  W_BODY_MSG.

  W_BODY_MSG = 'This is body of the mail.'.
  APPEND W_BODY_MSG TO I_BODY_MSG.
  CLEAR  W_BODY_MSG.

  "Write Packing List for Body
  DESCRIBE TABLE I_BODY_MSG LINES G_TAB_LINES.
  W_PACKING_LIST-HEAD_START = 1.
  W_PACKING_LIST-HEAD_NUM   = 1.
  W_PACKING_LIST-BODY_START = 1.
  W_PACKING_LIST-BODY_NUM   = G_TAB_LINES.
  W_PACKING_LIST-DOC_TYPE   = 'RAW'.
  APPEND W_PACKING_LIST TO I_PACKING_LIST.
  CLEAR  W_PACKING_LIST.

  APPEND LINES OF I_ATTACHMENT TO I_BODY_MSG.
  "Write Packing List for Attachment
*   w_packing_list-transf_bin = 'X'.
  W_PACKING_LIST-HEAD_START = 2.
  W_PACKING_LIST-HEAD_NUM   = 1.
  W_PACKING_LIST-BODY_START = G_TAB_LINES + 1.
  DATA LINES TYPE I.
  DESCRIBE TABLE I_ATTACHMENT LINES LINES.
  W_PACKING_LIST-BODY_NUM = LINES.
  W_PACKING_LIST-DOC_TYPE   = 'TXT'.
  W_PACKING_LIST-OBJ_DESCR  = 'TEXT Attachment'.
  W_PACKING_LIST-OBJ_NAME   = 'TXT_ATTACHMENT'.
  W_PACKING_LIST-DOC_SIZE   = W_PACKING_LIST-BODY_NUM * 255.
  APPEND W_PACKING_LIST TO I_PACKING_LIST.
  CLEAR  W_PACKING_LIST.

  "Fill the document data and get size of attachment
  W_DOCUMENT_DATA-OBJ_LANGU  = SY-LANGU.
  READ TABLE I_BODY_MSG INTO W_BODY_MSG INDEX G_ATTACHMENT_LINES.
  W_DOCUMENT_DATA-DOC_SIZE = ( G_ATTACHMENT_LINES - 1 ) * 255 + STRLEN( W_BODY_MSG ).
  "Receivers List.
  W_RECEIVERS-REC_TYPE   = 'U'.  "Internet address
  W_RECEIVERS-RECEIVER   = P_MAIL.
  W_RECEIVERS-COM_TYPE   = 'INT'.
  W_RECEIVERS-NOTIF_DEL  = 'X'.
  W_RECEIVERS-NOTIF_NDEL = 'X'.
  APPEND W_RECEIVERS TO I_RECEIVERS .
  CLEAR:W_RECEIVERS.

  "Function module to send MAI to Recipients
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      DOCUMENT_DATA              = W_DOCUMENT_DATA
      PUT_IN_OUTBOX              = 'X'
      COMMIT_WORK                = 'X'
    IMPORTING
      SENT_TO_ALL                = G_SENT_TO_ALL
    TABLES
      PACKING_LIST               = I_PACKING_LIST
      CONTENTS_TXT               = I_BODY_MSG
      RECEIVERS                  = I_RECEIVERS
    EXCEPTIONS
      TOO_MANY_RECEIVERS         = 1
      DOCUMENT_NOT_SENT          = 2
      DOCUMENT_TYPE_NOT_EXIST    = 3
      OPERATION_NO_AUTHORIZATION = 4
      PARAMETER_ERROR            = 5
      X_ERROR                    = 6
      ENQUEUE_ERROR              = 7
      OTHERS                     = 8.

  IF SY-SUBRC = 0 .
    MESSAGE I303(ME) WITH 'Mail has been Successfully Sent.'.
  ELSE.
    WAIT UP TO 2 SECONDS.
    "This program starts the SAPconnect send process.
    SUBMIT RSCONN01 WITH MODE = 'INT' WITH OUTPUT = 'X' AND RETURN.
  ENDIF.

0 comments:

Post a Comment

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.