
Introduction:
Sending a single attachment via email in SAP ABAP is straightforward, thanks to built-in functionality. However, sending multiple attachments is more complex, involving challenges like proper encoding, and maintaining the email structure. In this blog, we’ll explore the steps to overcome these complexities and successfully send multiple attachments in a single email. By the end, you’ll have a clear understanding of how to handle multiple files and ensure they’re correctly delivered.
Data Declarations:
DATA: lo_send_request TYPE REF TO cl_bcs. "VALUE IS INITIAL.
CLASS cl_bcs DEFINITION LOAD.
DATA: lo_document TYPE REF TO cl_document_bcs . "VALUE IS INITIAL. "document object
DATA : it_text TYPE bcsy_text. "Table for body
DATA : wa_text LIKE LINE OF it_text. "work area for message body
DATA: lo_sender TYPE REF TO if_sender_bcs. "VALUE IS INITIAL. "sender
DATA: lo_recipient TYPE REF TO if_recipient_bcs." VALUE IS INITIAL. "recipi
PARAMETERS p_mtart TYPE mara-mtart. "material type input
PARAMETERS p_CARRID TYPE Sflight-carrid. " Airline code input
PARAMETERS : p_email TYPE UNAME. "Email input
PARAMETERS: p_sub TYPE char50. "email subject
The selection screen gathers user input for:
DATA : it_mara TYPE TABLE OF mara,
wa_mara TYPE mara.
DATA : it_sflight TYPE TABLE OF Sflight,
wa_sflight TYPE Sflight.
DATA : lv_string TYPE string,
lv_string_flight TYPE string,
lv_data_string TYPE string,
lv_data_string_flight TYPE string,
lv_xstring TYPE xstring,
lv_xstring_flight TYPE xstring.
DATA: lit_binary_content TYPE solix_tab,
lit_binary_content_flight TYPE solix_tab,
l_attsubject TYPE sood-objdes.
Creating Email Object (lo_send_request)
lo_send_request = cl_bcs=>create_persistent( ).
This line creates a persistent email object using the class cl_bcs. The create_persistent method initializes the object that will be used to send the email.
Message Body Creation :
wa_text-line = '<HTML><BODY>'.
APPEND wa_text TO it_text.
wa_text-line = 'Dear Recepient,'.
APPEND wa_text TO it_text.
wa_text-line = '<br></br>'.
APPEND wa_text TO it_text.
wa_text-line = 'Please find the Flight Details And Material Data as requested in the attachment.'.
APPEND wa_text TO it_text.
wa_text-line = '<br></br>'.
APPEND wa_text TO it_text.
wa_text-line = 'Thanks & Regards , Sender'.
APPEND wa_text TO it_text.
wa_text-line = '</BODY></HTML>'.
APPEND wa_text TO it_text.
CLEAR wa_text.
Create Document for Email:
lo_document = cl_document_bcs=>create_document(
EXPORTING "create document
i_type = 'HTM' "Type of document HTM, TXT etc
i_text = it_text "email body internal table
i_subject = p_sub ). "email subject here p_sub input parameter
* Pass the document to send request
lo_send_request->set_document( lo_document ).
This block creates a document object for the email using cl_document_bcs. It specifies that the document type is HTML (i_type = 'HTM'), assigns the email body (i_text = it_text), and sets the email subject (i_subject = p_sub).
Attach Material Data (from MARA table):
SELECT * FROM mara INTO TABLE it_mara UP TO 100 ROWS
WHERE mtart = p_mtart.
LOOP AT it_mara INTO wa_mara.
CONCATENATE wa_mara-matnr wa_mara-mtart wa_mara-meins wa_mara-mbrsh wa_mara-matkl INTO lv_string SEPARATED BY
cl_abap_char_utilities=>horizontal_tab.
CONCATENATE lv_data_string lv_string INTO lv_data_string SEPARATED BY cl_abap_char_utilities=>newline.
ENDLOOP.
Convert String to XSTRING and Binary:
CALL FUNCTION 'HR_KR_STRING_TO_XSTRING'
EXPORTING
unicode_string = lv_data_string
* OUT_LEN =
IMPORTING
xstring_stream = lv_xstring
EXCEPTIONS
invalid_codepage = 1
invalid_string = 2
OTHERS = 3.
IF sy-subrc <> 0.
IF sy-subrc = 1 .
ELSEIF sy-subrc = 2 .
WRITE:/ 'invalid string ' .
ENDIF.
ENDIF.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xstring
TABLES
binary_tab = lit_binary_content.
The function SCMS_XSTRING_TO_BINARY is used to convert the xstring (lv_xstring) into binary content (lit_binary_content), which can be used as an attachment.
Add Attachment to Email:
add attachment
CLEAR l_attsubject .
CONCATENATE 'MaterialData_Excel' sy-datum INTO l_attsubject.
* Create Attachment
TRY.
lo_document->add_attachment( EXPORTING
i_attachment_type = 'XLS'
i_attachment_subject = l_attsubject
i_att_content_hex = lit_binary_content ).
CATCH cx_document_bcs INTO DATA(lx_document_bcs).
ENDTRY.
Set Sender for the Email:
TRY.
lo_sender = cl_sapuser_bcs=>create( sy-uname ). "sender is the logged in user
* Set sender to send request
lo_send_request->set_sender(
EXPORTING
i_sender = lo_sender ).
ENDTRY.
Attach Flight Data (from Sflight table):
SELECT * FROM sflight INTO TABLE it_sflight UP TO 100 ROWS
WHERE carrid = p_carrid.
LOOP AT it_sflight INTO wa_sflight.
CONCATENATE wa_sflight-carrid wa_sflight-connid wa_sflight-currency wa_sflight-fldate wa_sflight-planetype INTO lv_string_flight SEPARATED BY
cl_abap_char_utilities=>horizontal_tab.
CONCATENATE lv_data_string_flight lv_string_flight INTO lv_data_string_flight SEPARATED BY cl_abap_char_utilities=>newline.
ENDLOOP.
Convert Flight Data to XSTRING and Binary:
CALL FUNCTION 'HR_KR_STRING_TO_XSTRING'
EXPORTING
* codepage_to = '8300'
unicode_string = lv_data_string_flight
* OUT_LEN =
IMPORTING
xstring_stream = lv_xstring_flight
EXCEPTIONS
invalid_codepage = 1
invalid_string = 2
OTHERS = 3.
IF sy-subrc <> 0.
IF sy-subrc = 1 .
ELSEIF sy-subrc = 2 .
WRITE:/ 'invalid string ' .
ENDIF.
ENDIF.
***Xstring to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xstring_flight
TABLES
binary_tab = lit_binary_content_flight.
This process is identical to the material data conversion. The same functions (HR_KR_STRING_TO_XSTRING and SCMS_XSTRING_TO_BINARY) are used to convert the flight data into binary format.
Add Flight Data Attachment:
*add attachment
CLEAR l_attsubject .
CONCATENATE 'Flight_data' sy-datum INTO l_attsubject.
* Create Attachment
TRY.
lo_document->add_attachment( EXPORTING
i_attachment_type = 'XLS'
i_attachment_subject = l_attsubject
i_att_content_hex = lit_binary_content_flight ).
CATCH cx_document_bcs INTO DATA(lx_document_bcs_flight).
ENDTRY.
TRY.
lo_sender = cl_sapuser_bcs=>create( sy-uname ). "sender is the logged in user
lo_send_request->set_sender(
EXPORTING
i_sender = lo_sender ).
ENDTRY.
Here are defining the sender name , using SY-UNAME we are defining that the user who has logged in he is sender .
Set Recipient for the Email:
DATA(lo_bcs) = cl_bcs=>create_persistent( ).
DATA(lo_user) = cl_sapuser_bcs=>create( i_user = 'HANAUSER15').
lo_bcs->add_recipient( EXPORTING i_recipient = lo_user ).
TRY.
lo_send_request->add_recipient(
EXPORTING
i_recipient = lo_user
i_express = 'X' ).
ENDTRY.
A recipient is added to the email using cl_sapuser_bcs. Here, the recipient is hardcoded as HANAUSER15, but it can be replaced with p_email (email parameter) for dynamic recipient selection.
Send the Email:
TRY.
CALL METHOD lo_send_request->set_send_immediately
EXPORTING
i_send_immediately = p_send. "here selection screen input p_send
ENDTRY.
TRY.
** Send email
lo_send_request->send(
EXPORTING
i_with_error_screen = 'X' ).
COMMIT WORK.
IF sy-subrc = 0. "mail sent successfully
WRITE 'Mail sent successfully'.
ENDIF.
ENDTRY.
After activating and executing the code we can see the input screen
After executing
After executing this we can see the mail in T-CODE SBWP
Once we open SBWP t-code () we will get a pop up notification
In the inbox we can see the mails that we have received
When we open the Mail we can see that we have 2 attachments with the E-MAIL
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
20 | |
8 | |
3 | |
3 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 |