Teknologi

LightBlog

Breaking

LightBlog

Wednesday, 26 July 2017

Send Emails with Attachments of any Format

I need to send all those documents one by one via Email to a specific Mail ID with attachments. Since the folder can contain any type of document, The below program will solve this kind of requirement. Please find the below screen shots of the program for a easy go-thru of what exactly this program does.  





Now a Email will be generated to the given Mail ID as shown in the below screen shots.



Similarly you will get different mails with each attachment in a seperate mail.
The explaination of the program is as follows. 
Error rendering macro 'code': Invalid value specified for parameter 'lang'
REPORT YSEND_BULK_DOCUMENTS_FROM_A_FOLDER.
PARAMETERS : P_ATTACH TYPE CHAR200 OBLIGATORY,    " Select the Folder on the Desktop from F4 help
                        P_EMAIL TYPE CHAR50 OBLIGATORY,       " Email ID
                        P_SUBJ  TYPE CHAR50 OBLIGATORY.       " Subject of the Email
DATA : T_HEXTAB TYPE TABLE OF STRING,
           L_FILE TYPE STRING,
           T_MAILHEX TYPE SOLIX_TAB,
           LV_COUNT  TYPE I,
           GV_EMAIL         TYPE ADR6-SMTP_ADDR,
           T_BODY    TYPE TABLE OF SOLISTI1,
           GR_RECIPIENT     TYPE REF TO IF_RECIPIENT_BCS,
           TL_TEXTPOOL TYPE TABLE OF TEXTPOOL,
           WA_TEXTPOOL LIKE LINE OF TL_TEXTPOOL,
           IT_CONTENTS TYPE SOLI_TAB,
           LV_ATTACH_NAME TYPE SOOD-OBJDES,
           L_SUBJECT TYPE SO_OBJ_DES,
           L_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
           W_DOCUMENT TYPE REF TO CL_BCS,
           WA_CONTENTS LIKE LINE OF IT_CONTENTS,
           LV_FILENAME TYPE STRING,
           L_RESULT TYPE OS_BOOLEAN,
          LV_STRING TYPE STRING.
DATA : FILE_TABLE LIKE TABLE OF SDOKPATH WITH HEADER LINE .
DATA : DIR_TABLE LIKE TABLE OF SDOKPATH WITH HEADER LINE .
DATA : FILE_COUNT TYPE I ,
            DIRCOUNT   TYPE I .
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_ATTACH.
  CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE
    CHANGING
      SELECTED_FOLDER      = LV_STRING
    EXCEPTIONS
      CNTL_ERROR           = 1
      ERROR_NO_GUI         = 2
      NOT_SUPPORTED_BY_GUI = 3
      OTHERS               = 4.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ELSE.
    P_ATTACH = LV_STRING.
  ENDIF.
START-OF-SELECTION.
   * CHECK IF THE FOLDER PATH IS INITIAL, IF INITIAL LEAVE THE PROGRAM, ELSE COPY THE SUBJECT. ---> IT ALL DEPENDS ON HOW YOU VALIDATE.
  IF P_ATTACH IS INITIAL.
    LEAVE LIST-PROCESSING.
  ELSE.
    L_SUBJECT = P_SUBJ.
  ENDIF.
   * GETS ALL THE FILES NAMES IN THE SELECTED FOLDER
  CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'
    EXPORTING
      DIRECTORY  = P_ATTACH
    IMPORTING
      FILE_COUNT = FILE_COUNT
      DIR_COUNT  = DIRCOUNT
    TABLES
      FILE_TABLE = FILE_TABLE
      DIR_TABLE  = DIR_TABLE
    EXCEPTIONS
      CNTL_ERROR = 1
      OTHERS     = 2.
   * TELL HOW MANY FIELDS EXISTS
    WRITE:/ 'no of files in the floder is : '(005), FILE_COUNT.
  SKIP 1.
  WRITE:/ SY-ULINE.
  WRITE:/ SY-ULINE.
   * PROCESS THE DATA TO SEND MAIL WITH ATTACHMENT.
  LOOP AT FILE_TABLE.
    LV_COUNT = SY-TABIX.
    CONCATENATE P_ATTACH '\' FILE_TABLE INTO LV_FILENAME.
    MOVE LV_FILENAME TO L_FILE.
    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        FILENAME                = L_FILE
        FILETYPE                = 'BIN'
      TABLES
        DATA_TAB                = T_MAILHEX
      EXCEPTIONS
        FILE_OPEN_ERROR         = 1
        FILE_READ_ERROR         = 2
        NO_BATCH                = 3
        GUI_REFUSE_FILETRANSFER = 4
        INVALID_TYPE            = 5
        NO_AUTHORITY            = 6
        UNKNOWN_ERROR           = 7
        BAD_DATA_FORMAT         = 8
        HEADER_NOT_ALLOWED      = 9
        SEPARATOR_NOT_ALLOWED   = 10
        HEADER_TOO_LONG         = 11
        UNKNOWN_DP_ERROR        = 12
        ACCESS_DENIED           = 13
        DP_OUT_OF_MEMORY        = 14
        DISK_FULL               = 15
        DP_TIMEOUT              = 16
        OTHERS                  = 17.
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
     * YOU NEED TO MAINTAIN TEXT SYMBOLS AND WANT TO READ THEM IN THE PROGRAM
        READ TEXTPOOL SY-REPID INTO TL_TEXTPOOL LANGUAGE SY-LANGU.
     * MY TEXT SYMBOL IS " T04: You can Add your Body here"
        READ TABLE TL_TEXTPOOL INTO WA_TEXTPOOL WITH KEY ID = 'I'
                                                         KEY = 'T04' BINARY SEARCH.
    IF SY-SUBRC = 0.
      WA_CONTENTS-LINE = WA_TEXTPOOL-ENTRY.
      APPEND WA_CONTENTS TO IT_CONTENTS.
    ENDIF.
     * CREATE THE DOCUMENT WITH CONTENTS
        CREATE OBJECT L_DOCUMENT.
    L_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
        I_TYPE       = 'HTM'
        I_SUBJECT    = L_SUBJECT
        I_LENGTH     = '1000'
        I_LANGUAGE   = SY-LANGU
        I_IMPORTANCE = '1'
        I_TEXT       = IT_CONTENTS ).
     * ATTACH THE FILE, THE ATTACHMENT TYPE SHOULD BE BIN TO ACCEPT ANY KIND OF ATTACHMENT, INCLUDING VIDEOS, AUDIO FILES ETC...
    LV_ATTACH_NAME = FILE_TABLE.
    CALL METHOD L_DOCUMENT->ADD_ATTACHMENT
      EXPORTING
        I_ATTACHMENT_TYPE    = 'BIN'
        I_ATTACHMENT_SUBJECT = LV_ATTACH_NAME
        I_ATT_CONTENT_HEX    = T_MAILHEX.
     * CREATING PERSISTENT OBJECT WILL ALLOW YOU TO SET THE DOCUMENT IN THE MAIL
        W_DOCUMENT = CL_BCS=>CREATE_PERSISTENT( ).
    CALL METHOD W_DOCUMENT->SET_DOCUMENT( L_DOCUMENT ).
     * EMAIL AS GIVEN IN THE SELECTION SCREEN.
    GV_EMAIL = P_EMAIL.
    GR_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( GV_EMAIL ).
    "Add recipient to send request
    CALL METHOD W_DOCUMENT->ADD_RECIPIENT
      EXPORTING
        I_RECIPIENT = GR_RECIPIENT
        I_EXPRESS   = 'X'.
     * SEND THE MAIL
        CALL METHOD W_DOCUMENT->SEND(
          EXPORTING
            I_WITH_ERROR_SCREEN = 'X'
          RECEIVING
            RESULT              = L_RESULT ).
     * YOU CAN VERIFY THE STATUS IN THE LIST, YOU CAN ALSO SUBMIT THIS AS A BACKGROUND JOB.
    IF L_RESULT = 'X'.
      WRITE :/ , 1(9) LV_COUNT, 10(55) FILE_TABLE-PATHNAME.
      WRITE : 56(1) '-', 58(20) 'Mail sent'(003).
      COMMIT WORK.
    ELSE.
      WRITE :/ , 1(9) LV_COUNT, 10(55) FILE_TABLE-PATHNAME.
      WRITE : 56(1) '-', 58(30) 'Error in sending Mail'(004).
      ROLLBACK WORK.
    ENDIF.
    REFRESH IT_CONTENTS[].
  ENDLOOP.

You can Verify the Status of the Mail in SOST and SCOT as shown below respectively. 
  

I Hope this WIKI would help many SAPians in sending mails with any type of attachment using ABAP.


Referance from https://wiki.scn.sap.com/wiki/display/ABAP/Send+Emails+with+Attachments+of+any+Format

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Adbox