Read excel file with images from AL11 Directory and send as Email attachment
There are scenarios sometime user has uploaded excel file with images in application server which needs to be read and send it through email to supplier or customer.
In this case we can use the DATASET read the file from application server and using class CL_BCS we can send the excel file as attachment.
User uploads the file to application server with images in it. That excel file we have to read and attached to email.
Step1 : Know the file path or directory path in which the file is stored.
DATA : lv_path TYPE eps2filnam. lv_path = 'usr/sap/folder/filewithimage.xls'.
Step2: Use OPEN DATASET keyword to open the file, use READ DATASET to read the file content and CLOSE DATASET to close the open file. Store each line item into one string variable using concatenate.
DATA : ls_read_file TYPE string, ls_all_data TYPE string. OPEN DATASET lv_path FOR INPUT IN BINARY MODE. IF sy-subrc IS INITIAL. DO. READ DATASET lv_path INTO ls_read_file. IF sy-subrc IS NOT INITIAL. EXIT. ELSE. CONCATENATE ls_all_data ls_read_file INTO ls_all_data. ENDIF. ENDDO. ENDIF. CLOSE DATASET lv_path.
Step3 : Now we have all data in one string variable and ready to convert data from string to text using CL_BCS_CONVERT.
DATA : lt_file_data TYPE soli_tab. IF ls_all_data IS NOT INITIAL. lt_file_data = cl_bcs_convert=>string_to_soli( iv_string = ls_all_data ). ENDIF.
Step4 : we have all data which is converted to compatible format and ready to send as email excel attachment.
DATA : lo_document TYPE REF TO cl_document_bcs VALUE IS INITIAL, lo_document_exception TYPE REF TO cx_document_bcs VALUE IS INITIAL, lo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL, lo_sender TYPE REF TO if_sender_bcs VALUE IS INITIAL, lo_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL, lv_recipient TYPE adr6-smtp_addr VALUE IS INITIAL, lv_send_to_all TYPE char1 VALUE IS INITIAL. CREATE OBJECT lo_document. TRY. lo_document = cl_document_bcs=>create_document( i_type = 'RAW' i_subject = 'Email subject' i_text = 'Email body'). CATCH cx_document_bcs INTO lo_document_exception. lo_document_exception->get_text( ). ENDTRY. IF lt_file_data IS NOT INITIAL. TRY. lo_document->add_attachment( EXPORTING i_attachment_type = 'xls' " Document Class for Attachment i_attachment_subject = 'File name' " Attachment Title i_att_content_text = lt_file_data ). " Content (Text-Like) CATCH cx_document_bcs INTO lo_document_exception. lo_document_exception->get_text( ). ENDTRY. "pass the docuement to send request lo_send_request->set_document( lo_document ). "Create sender lo_sender = cl_sapuser_bcs=>create( su-uname ). "set sender lo_send_request = cl_bcs=>create_persistent( ). "assign sender lo_send_request->set_sender( i_sender = lo_sender ). "Create recipient lv_recipient = 'firstname.lastname@example.org'. " recipient address "assign recipient lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_recipient ). "set recipient lo_send_request->add_recipient( EXPORTING i_recipient = lo_recipient " Recipient of Message i_express = 'X' ). " Send As Express Message "send email lo_send_request->send( i_with_error_screen = 'X' RECEIVING result = lv_send_to_all ). COMMIT WORK. ENDIF.
Step5 : Now check SOST tcode where email suppose to be triggered with having one attachment of excel file with images as it is available in AL11 to it.
I hope this blog post will help you and get the idea how to read the excel file with images in it from application server (AL11) and same file use as attachment to your email.
Please like and share feedback or thoughts in comment.