SAP MDG · UTILITIES

How do I implement an SAP ABAP email routine that sends a message with an attachment and supports configurable recipients and immediate dispatch?

Use the standard BCS classes to create the mail request, set sender and recipient, build the body with CL_DOCUMENT_BCS, optionally add a PDF attachment, send the request, COMMIT WORK, and optionally trigger RSCONN01 for immediate processing.

Use the standard BCS classes to create the mail request, set sender and recipient, build the body with CL_DOCUMENT_BCS, optionally add a PDF attachment, send the request, COMMIT WORK, and optionally trigger RSCONN01 for immediate processing.

Use the SAP BCS API to create a persistent send request, set the sender and recipient, build the mail document, and optionally attach the generated content when the setup record says attachments are active. The pattern in the source also shows how to read configuration from ZMD_WONOF_SETUP and how to trigger SAPconnect processing immediately when needed.

Process flow

  1. Create or reuse a configuration record in ZMD_WONOF_SETUP.
  2. Store the scenario key in SETUP_NAME and activate the record with IS_ACTIVE = ABAP_TRUE.
  3. Set IS_ATTACMENT to control whether the mail includes an attachment.
  4. Set PROCESS_IMM if SAPconnect should run immediately.
  5. Populate LT_BODY and LT_RECIPIENT from the business context.
  6. Create the BCS send request with CL_BCS=>CREATE_PERSISTENT( ).
  7. Create the document with CL_DOCUMENT_BCS=>CREATE_DOCUMENT( ).
  8. Add the attachment with ADD_ATTACHMENT when configured to do so.

Referenced tables

ObjectPurpose
ZMD_WONOF_SETUPConfiguration table that controls the mail scenario, active flag, attachment flag, and immediate processing flag.

ILLUSTRATIVE ABAP SAMPLE

Source ABAP example

Exact relevant implementation excerpt from the knowledge document.

1METHOD send_mail_as_attachment. 2************************************************************************ 3*& Program Name : ZCL_WONOF_FRAMEWORK_UTIL-SEND_EMAIL_AS_ATTACHMENT 4*& Date : 02/19/2020 * 5*&---------------------------------------------------------------------* 6*& Description : Method to send email through WONOF tool * 7*& as an attachment * 8*&---------------------------------------------------------------------* * 9************************************************************************ 10 CLASS cl_bcs DEFINITION LOAD. 11 DATA: ldo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL, 12 html TYPE REF TO cl_gui_html_viewer. 13 14 DATA: ldo_document TYPE REF TO cl_document_bcs VALUE IS INITIAL. "document object 15 DATA : lt_text TYPE bcsy_text. "Table for body 16 DATA : ls_text LIKE LINE OF lt_text. "work area for message body 17 DATA: ld_sender TYPE REF TO if_sender_bcs VALUE IS INITIAL. "sender 18 DATA: ld_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL, "recipient 19 ld_sub TYPE char100 , "subject 20 ld_len TYPE i, "length of body 21* ld_body TYPE IBO_T_WF_TXT_BODY, " BODY 22 lt_body TYPE STANDARD TABLE OF so_text255, " BODY 23 lt_body_pdf TYPE STANDARD TABLE OF soli, 24 lt_body_final TYPE bcsy_text, 25 ls_body TYPE char255, 26 ld_body_conv TYPE string, " BODY 27 lt_recipient TYPE tmas_t_string, "Recipient list 28 ld_url TYPE char255. 29 30 "ld_body_conv type IBO_T_WF_TXT_BODY. 31 CONSTANTS: lc_type TYPE so_obj_tp VALUE 'RAW', 32 lc_attach TYPE soodk-objtp VALUE 'PDF', 33 lc_amp TYPE char9 VALUE ' <a href=', 34 lc_line TYPE char4 VALUE '</a>', 35 lc_space TYPE char2 VALUE ' '. 36 37 "CONCATENATE ld_body ld_body_conv into ld_body_conv . 38 39***Attachment data 40 41 DATA :lv_xstring TYPE xstring. 42 DATA: lt_binary_content TYPE solix_tab, 43 l_attsubject TYPE sood-objdes. 44 DATA: ld_subject TYPE so_obj_des. 45 DATA: ld_body_tmp TYPE string, 46 lt_body_tmp TYPE STANDARD TABLE OF so_text255. 47 48 SELECT SINGLE * 49 INTO @DATA(ls_setup) 50 FROM zmd_wonof_setup 51 WHERE setup_name = @id_email_setup_name 52 AND is_active = @abap_true. 53 IF sy-subrc EQ 0. 54 55 56 DATA: ld_send_flag TYPE boolean. 57 58 ENDIF. 59 60 LOOP AT lt_body INTO ls_body. 61* ld_body_tmp = ls_body. 62* APPEND ls_body TO lt_body_pdf. 63* CONCATENATE ld_body_final ld_body_tmp INTO DATA(ld_body_final) IN CHARACTER MODE SEPARATED BY cl_abap_char_utilities=>newline . 64 IF ls_body+0(1) EQ '*' AND ls_body CP '%'. 65 CLEAR ls_body. 66 ELSE. 67 IF ls_body CP '%'. 68 APPEND ls_body TO lt_body_final. 69 CLEAR ls_body. 70 ELSE. 71 APPEND ls_body TO lt_body_final. 72 CLEAR ls_body. 73 ENDIF. 74 ENDIF. 75 ENDLOOP . 76 77** Message body and subject 78 ld_subject = ld_sub+0(50) . 79 80* ld_len = strlen( ld_body_final ). 81 82 LOOP AT lt_recipient INTO DATA(ls_recipient) . 83 ldo_send_request = cl_bcs=>create_persistent( ). 84 85 IF ls_setup-is_attacment = abap_true . 86*Set body 87 IF lt_text IS INITIAL. 88 ls_text-line = TEXT-009. 89 APPEND ls_text TO lt_text. 90 CLEAR ls_text. 91 ENDIF. 92 93 ldo_document = cl_document_bcs=>create_document( "create document 94 i_type = lc_type "Type of document HTM, TXT etc 95 i_text = lt_text "email body internal table 96 i_subject = ld_subject ). "email subject here ld_sub input parameter 97* Pass the document to send request 98 IF ldo_send_request IS BOUND. 99 ldo_send_request->set_document( ldo_document ). 100 101 102**add attachment 103 CLEAR l_attsubject . 104 CONCATENATE TEXT-005 sy-datum INTO l_attsubject. 105* Create Attachment 106 107 TRY. 108 ldo_document->add_attachment( EXPORTING 109 i_attachment_type = lc_attach 110 i_attachment_subject = l_attsubject 111 i_att_content_text = lt_body_final "pdf 112 ). 113 "i_att_content_hex = lt_binary_content ). 114 CATCH cx_document_bcs INTO DATA(lx_document_bcs) ##NEEDED. 115 116 ENDTRY. 117 ENDIF. 118 ELSE . 119 120* IF lt_text IS INITIAL.

The remaining configuration, implementation details, and testing guidance continue from this answer more…

Related questions and keywords

Alternative questions

  • How can I send an email attachment from ABAP using CL_BCS?
  • How do I build a configurable ABAP mail function with attachment handling?
  • What is the standard BCS approach to send an email with body and attachment in ABAP?

Possible questions

  • How do I send an email with attachment in SAP ABAP?
  • How do I use CL_BCS for email sending?
  • How can I configure recipient handling for ABAP email notifications?
  • How do I trigger immediate SAPconnect dispatch after sending mail?
  • How do I add a PDF attachment to an SAP email?

Keywords

CL_BCSCL_DOCUMENT_BCSCL_SAPUSER_BCSCL_CAM_ADDRESS_BCSRSCONN01SAPconnectABAP emailattachmentrecipient listZMD_WONOF_SETUP