Skip to Content
Author's profile photo zafar karnalkar

Email using CL_BCS with multiple attachment in any format

Introduction:

      The requirement is to send the email from SAP with multiple attachments; the attachment can be in any

format like PDF, DOC, DOCX, XLS, XLSX etc. also user can write some text line which should come in the

email body.

Selection screen. :

selection screen.JPG

Email in Inbox :

Email in Inbox.JPG

we can see the 4 attachments in email.

For the source code refer the attachment

*Note: I have use separate forms for each attachment that can be done with single form also with ‘DO’ or

‘loop’ logic.

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Rainer Hübenthal
      Rainer Hübenthal

      Thanks so far. As you are sending HTML Emails, do you know how to send this as a multipart message containing both the HTML part and a corresponding text part?

      Author's profile photo zafar karnalkar
      zafar karnalkar
      Blog Post Author

      HI,

      Till now I have use only one format HTML or TXT, I have not try multipart, If I found any think related to this I will update.

      Regards,

      Zafar

      Author's profile photo Former Member
      Former Member

      Hello Zafar,

      Next time please consider posting the "code snippet" content as a WiKi and not as a blog.

      BR,

      Suhas

      Author's profile photo Daniel Luiz Vieira da Silva
      Daniel Luiz Vieira da Silva

      Hi, where is the attachment?
      Thanks!

      Author's profile photo Ajinkya Ket
      Ajinkya Ket

      Hello Zafar,

      Where is code attachment?

      Can you please post the code?

       

      Thanks.

      Author's profile photo deepika jain
      deepika jain

      Hello Zafar,

      Where is code attachment?

      Author's profile photo zafar karnalkar
      zafar karnalkar
      Blog Post Author

      Hi,

      I thing attachment got removed.

      Find the code below:.

      *&---------------------------------------------------------------------*
      *& Report Z_EMAIL_EXAMPLE
      *&
      *&---------------------------------------------------------------------*
      *&
      *&
      *&---------------------------------------------------------------------*
      REPORT Z_EMAIL_EXAMPLE.
      TABLES : somlreci1,ADR6.
      "Data declaration for file names
      DATA : GV_FILE1 TYPE STRING,
      GV_FILE2 TYPE STRING,
      GV_FILE3 TYPE STRING,
      GV_FILE4 TYPE STRING.
      "Data declaration for file content after file upload
      DATA : IT_BIN_B1 TYPE STANDARD TABLE OF SOLIX,
      IT_BIN_B2 TYPE STANDARD TABLE OF SOLIX,
      IT_BIN_B3 TYPE STANDARD TABLE OF SOLIX,
      IT_BIN_B4 TYPE STANDARD TABLE OF SOLIX.
      "For the font in Email Body
      DATA : GV_FONTA11 TYPE STRING VALUE '<P style="font-family:arial;fontsize:
      15;">'.
      "Class data declaration
      DATA: go_send_request type ref to cl_bcs,
      go_document type ref to cl_document_bcs,
      lo_sender TYPE REF TO cl_sapuser_bcs,
      lo_senderint TYPE REF TO cl_cam_address_bcs,
      lo_recipient type ref to if_recipient_bcs,
      lo_bcs_exception type ref to cx_bcs,
      GV_SUB TYPE STRING,
      lv_sent_to_all type os_boolean.
      "Internal Table for HTML email body
      DATA : IT_CONTENTS TYPE STANDARD TABLE OF solisti1,
      wa_contents TYPE solisti1.
      "for four character file extensions
      DATA : lt_att_head type soli_tab,
      lv_text_line type soli,
      lv_filename type string,
      EV_EXTENSION type string,
      EV_FILENAME TYPE STRING.
      "For attachment subjects and attachment type.
      DATA : subj_B1 TYPE so_obj_des,
      att_B1 TYPE soodk-objtp,
      subj_B2 TYPE so_obj_des,
      att_B2 TYPE soodk-objtp,
      subj_B3 TYPE so_obj_des,
      att_B3 TYPE soodk-objtp,
      subj_B4 TYPE so_obj_des,
      att_B4 TYPE soodk-objtp.
      SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
      SELECT-OPTIONS: S_RCVRS FOR ADR6-smtp_addr NO INTERVALS.
      SELECTION-SCREEN END OF BLOCK B1.
      SELECTION-SCREEN : BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.
      PARAMETERS : P_B1 TYPE LOCALFILE,
      P_B2 TYPE LOCALFILE,
      P_B3 TYPE LOCALFILE,
      P_B4 TYPE LOCALFILE.
      SELECTION-SCREEN END OF BLOCK B2.
      SELECTION-SCREEN : BEGIN OF BLOCK B3 WITH FRAME TITLE TEXT-003.
      PARAMETERS : P_L1 TYPE CHAR100 lower case,
      P_L2 TYPE CHAR100 lower case,
      P_L3 TYPE CHAR100 lower case,
      P_L4 TYPE CHAR100 lower case,
      P_L5 TYPE CHAR100 lower case,
      P_L6 TYPE CHAR100 lower case.
      SELECTION-SCREEN END OF BLOCK B3.
      AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_B1.
      "select the 1st brochure
      PERFORM SELECT_B1.
      AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_B2.
      "select the 2nd brochure
      PERFORM SELECT_B2.
      AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_B3.
      "select the 3rd brochure
      PERFORM SELECT_B3.
      AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_B4.
      "select the 4th brochure
      PERFORM SELECT_B4.
      START-OF-SELECTION.
      IF NOT GV_FILE1 IS INITIAL.
      PERFORM UPLOAD_FILE1.
      ENDIF.
      IF NOT GV_FILE2 IS INITIAL.
      PERFORM UPLOAD_FILE2.
      ENDIF.
      IF NOT GV_FILE3 IS INITIAL.
      PERFORM UPLOAD_FILE3.
      ENDIF.
      IF NOT GV_FILE4 IS INITIAL.
      PERFORM UPLOAD_FILE4.
      ENDIF.
      PERFORM HTML_BODY.
      PERFORM SEND_EMAIL.
      FORM SELECT_B1.
      "form to select the brochure
      CALL FUNCTION 'F4_FILENAME'
      EXPORTING
      program_name = syst-cprog
      dynpro_number = syst-dynnr
      * FIELD_NAME = 'FILENAME'
      IMPORTING
      file_name = P_B1.
      CLEAR : GV_FILE1.
      GV_FILE1 = P_B1.
      ENDFORM.
      FORM SELECT_B2.
      "form to select the brochure
      CALL FUNCTION 'F4_FILENAME'
      EXPORTING
      program_name = syst-cprog
      dynpro_number = syst-dynnr
      * FIELD_NAME = 'FILENAME'
      IMPORTING
      file_name = P_B2.
      CLEAR : GV_FILE2.
      GV_FILE2 = P_B2.
      ENDFORM.
      FORM SELECT_B3.
      "form to select the brochure
      CALL FUNCTION 'F4_FILENAME'
      EXPORTING
      program_name = syst-cprog
      dynpro_number = syst-dynnr
      * FIELD_NAME = 'FILENAME'
      IMPORTING
      file_name = P_B3.
      CLEAR : GV_FILE3.
      GV_FILE3 = P_B3.
      ENDFORM.
      FORM SELECT_B4.
      "form to select the brochure
      CALL FUNCTION 'F4_FILENAME'
      EXPORTING
      program_name = syst-cprog
      dynpro_number = syst-dynnr
      * FIELD_NAME = 'FILENAME'
      IMPORTING
      file_name = P_B4.
      CLEAR : GV_FILE4.
      GV_FILE4 = P_B4.
      ENDFORM.
      FORM UPLOAD_FILE1.
      *-- Upload file 1 and store the data in binary format in it_bin_b1
      CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
      filename = GV_FILE1
      filetype = 'BIN'
      TABLES
      data_tab = IT_BIN_B1
      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 I000(8I) WITH 'Error while uploading 1st file1'.
      ENDIF.
      ENDFORM.
      FORM UPLOAD_FILE2.
      *-- Upload file 2 and store the data in binary format in it_bin_b2
      CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
      filename = GV_FILE2
      filetype = 'BIN'
      TABLES
      data_tab = IT_BIN_B2
      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 I000(8I) WITH 'Error while uploading 1st file2'.
      ENDIF.
      ENDFORM.
      FORM UPLOAD_FILE3.
      *-- Upload file 3 and store the data in binary format in it_bin_b3
      CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
      filename = GV_FILE3
      filetype = 'BIN'
      TABLES
      data_tab = IT_BIN_B3
      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 I000(8I) WITH 'Error while uploading 1st file3'.
      ENDIF.
      ENDFORM.
      FORM UPLOAD_FILE4.
      *-- Upload file 4 and store the data in binary format in it_bin_b4
      CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
      filename = GV_FILE4
      filetype = 'BIN'
      TABLES
      data_tab = IT_BIN_B4
      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 I000(8I) WITH 'Error while uploading 1st file4'.
      ENDIF.
      ENDFORM.
      FORM HTML_NEWLINE.
      CLEAR : WA_CONTENTS.
      WA_CONTENTS-LINE = '<br>'.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDFORM.
      FORM HTML_BODY.
      CLEAR : WA_CONTENTS,IT_CONTENTS,IT_CONTENTS[].
      "HTML opening tag
      WA_CONTENTS-LINE = '<HTML> <BODY>'.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      "text line 1
      IF NOT P_L1 IS INITIAL.
      CONCATENATE GV_FONTA11 P_L1 '</P>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDIF.
      IF NOT P_L2 IS INITIAL.
      CONCATENATE GV_FONTA11 P_L2 '</P>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDIF.
      IF NOT P_L3 IS INITIAL.
      CONCATENATE GV_FONTA11 P_L3 '</P>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDIF.
      IF NOT P_L4 IS INITIAL.
      CONCATENATE GV_FONTA11 P_L4 '</P>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDIF.
      IF NOT P_L4 IS INITIAL.
      CONCATENATE GV_FONTA11 P_L4 '</P>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDIF.
      IF NOT P_L5 IS INITIAL.
      CONCATENATE GV_FONTA11 P_L5 '</P>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDIF.
      IF NOT P_L6 IS INITIAL.
      CONCATENATE GV_FONTA11 P_L6 '</P>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDIF.
      WA_CONTENTS-LINE = '</BODY> </HTML>'.
      APPEND WA_CONTENTS TO IT_CONTENTS.
      CLEAR : WA_CONTENTS.
      ENDFORM.
      FORM SEND_EMAIL.
      TRY.
      * -------- create persistent send request ------------------------
      go_send_request = cl_bcs=>create_persistent( ).
      go_document = cl_document_bcs=>create_document(
      i_type = 'HTM'
      i_text = IT_Contents
      * i_length = '12'
      i_subject = '' ).
      "For email subject more than 50 Char
      GV_SUB = 'Email from SAP with multiple attachment in any format and HTML Email Body !'.
      CALL METHOD go_send_request->set_message_subject
      EXPORTING
      ip_subject = gv_sub.
      "attachmentment
      PERFORM EMAIL_ATTACHMENTS.
      * add document to send request
      CALL METHOD go_send_request->set_document( go_document ).
      * add recipient with its respective attributes to send request
      LOOP AT S_RCVRS.
      lo_recipient = cl_cam_address_bcs=>create_internet_address(
      S_RCVRS-LOW ).
      CALL METHOD go_send_request->add_recipient
      EXPORTING
      i_recipient = lo_recipient
      i_express = 'X'.
      CLEAR : S_RCVRS.
      ENDLOOP.
      * ---------- send document ---------------------------------------
      CALL METHOD go_send_request->set_send_immediately
      EXPORTING
      i_send_immediately = 'X'.
      CALL METHOD go_send_request->send(
      EXPORTING
      i_with_error_screen = 'X'
      RECEIVING
      result = lv_sent_to_all ).
      IF lv_sent_to_all = 'X'.
      Message I000(8I) with 'Email send succesfully'.
      ELSEIF lv_sent_to_all IS INITIAL.
      message i000(8i) with 'Email not send'.
      ENDIF.
      COMMIT WORK.
      CATCH cx_bcs INTO lo_bcs_exception.
      ENDTRY.
      ENDFORM.
      FORM EMAIL_ATTACHMENTS.
      "file 1 attachment
      IF NOT IT_BIN_B1[] IS INITIAL.
      CLEAR : EV_FILENAME, EV_EXTENSION, lv_filename, lv_text_line, lt_att_head[].
      "attachment subject
      CALL FUNCTION 'CRM_EMAIL_SPLIT_FILENAME'
      EXPORTING
      iv_path = GV_FILE1
      IMPORTING
      EV_FILENAME = EV_FILENAME
      EV_EXTENSION = EV_EXTENSION
      * EV_MIMETYPE =
      .
      SUBJ_B1 = EV_FILENAME.
      ATT_B1 = EV_EXTENSION.
      concatenate SUBJ_B1 '.' EV_EXTENSION into lv_filename.
      concatenate '&SO_FILENAME=' lv_filename into lv_text_line.
      append lv_text_line to lt_att_head.
      "end of subject atachment
      TRY.
      *-- Add Attachment to the Document
      CALL METHOD go_document->add_attachment
      EXPORTING
      i_attachment_type = ATT_B1 "l_att_type
      i_attachment_subject = SUBJ_B1
      i_att_content_hex = IT_BIN_B1[]
      i_attachment_header = lt_att_head.
      CATCH cx_document_bcs.
      ENDTRY.
      ENDIF.
      "file 2 attachment
      IF NOT IT_BIN_B2[] IS INITIAL.
      CLEAR : EV_FILENAME, EV_EXTENSION, lv_filename, lv_text_line, lt_att_head[].
      "attachment subject
      CALL FUNCTION 'CRM_EMAIL_SPLIT_FILENAME'
      EXPORTING
      iv_path = GV_FILE2
      IMPORTING
      EV_FILENAME = EV_FILENAME
      EV_EXTENSION = EV_EXTENSION
      * EV_MIMETYPE =
      .
      SUBJ_B2 = EV_FILENAME.
      ATT_B2 = EV_EXTENSION.
      concatenate SUBJ_B2 '.' EV_EXTENSION into lv_filename.
      concatenate '&SO_FILENAME=' lv_filename into lv_text_line.
      append lv_text_line to lt_att_head.
      "end of subject atachment
      TRY.
      *-- Add Attachment to the Document
      CALL METHOD go_document->add_attachment
      EXPORTING
      i_attachment_type = ATT_B2 "l_att_type
      i_attachment_subject = SUBJ_B2
      i_att_content_hex = IT_BIN_B2[]
      i_attachment_header = lt_att_head.
      CATCH cx_document_bcs.
      ENDTRY.
      ENDIF.
      "file 3 attachment
      IF NOT IT_BIN_B3[] IS INITIAL.
      CLEAR : EV_FILENAME, EV_EXTENSION, lv_filename, lv_text_line, lt_att_head[].
      "attachment subject
      CALL FUNCTION 'CRM_EMAIL_SPLIT_FILENAME'
      EXPORTING
      iv_path = GV_FILE3
      IMPORTING
      EV_FILENAME = EV_FILENAME
      EV_EXTENSION = EV_EXTENSION
      * EV_MIMETYPE =
      .
      SUBJ_B3 = EV_FILENAME.
      ATT_B3 = EV_EXTENSION.
      concatenate SUBJ_B3 '.' EV_EXTENSION into lv_filename.
      concatenate '&SO_FILENAME=' lv_filename into lv_text_line.
      append lv_text_line to lt_att_head.
      "end of subject atachment
      TRY.
      *-- Add Attachment to the Document
      CALL METHOD go_document->add_attachment
      EXPORTING
      i_attachment_type = ATT_B3 "l_att_type
      i_attachment_subject = SUBJ_B3
      i_att_content_hex = IT_BIN_B3[]
      i_attachment_header = lt_att_head.
      CATCH cx_document_bcs.
      ENDTRY.
      ENDIF.
      "file 4 attachment
      IF NOT IT_BIN_B4[] IS INITIAL.
      CLEAR : EV_FILENAME, EV_EXTENSION, lv_filename, lv_text_line, lt_att_head[].
      "attachment subject
      CALL FUNCTION 'CRM_EMAIL_SPLIT_FILENAME'
      EXPORTING
      iv_path = GV_FILE4
      IMPORTING
      EV_FILENAME = EV_FILENAME
      EV_EXTENSION = EV_EXTENSION
      * EV_MIMETYPE =
      .
      SUBJ_B4 = EV_FILENAME.
      ATT_B4 = EV_EXTENSION.
      concatenate SUBJ_B4 '.' EV_EXTENSION into lv_filename.
      concatenate '&SO_FILENAME=' lv_filename into lv_text_line.
      append lv_text_line to lt_att_head.
      "end of subject atachment
      TRY.
      *-- Add Attachment to the Document
      CALL METHOD go_document->add_attachment
      EXPORTING
      i_attachment_type = ATT_B4 "l_att_type
      i_attachment_subject = SUBJ_B4
      i_att_content_hex = IT_BIN_B4[]
      i_attachment_header = lt_att_head.
      CATCH cx_document_bcs.
      ENDTRY.
      ENDIF.
      ENDFORM.