*&---------------------------------------------------------------------* *& Report Name : /EUTIN/DEMO_SEND_MAIL *& Title : Demo: Send eMail with Attachment *& Developer : Michael Eutin - 01.05.2019 *& Development Class : /EUTIN/TOOLS *& Description : *& Send eMail with attachment *& *&---------------------------------------------------------------------* *& CHANGE HISTORY *&---------------------------------------------------------------------* *& Date Description *&---------------------------------------------------------------------* *& *& *& *&---------------------------------------------------------------------* REPORT /eutin/demo_send_mail NO STANDARD PAGE HEADING. *----------------------------------------------------------------------* *--- Global Data Declarations *----------------------------------------------------------------------* CLASS cl_abap_char_utilities DEFINITION LOAD. TYPES: BEGIN OF ts_t100, arbgb TYPE t100-arbgb, msgnr TYPE t100-msgnr, text TYPE t100-text, END OF ts_t100, tt_message TYPE STANDARD TABLE OF solisti1 WITH DEFAULT KEY, tt_attach TYPE STANDARD TABLE OF solisti1 WITH DEFAULT KEY. *----------------------------------------------------------------------* *--- Selection Screen (Dynpro 1000) *----------------------------------------------------------------------* PARAMETERS: p_email TYPE somlreci1-receiver. *----------------------------------------------------------------------* *--- Event: START-OF-SELECTION *----------------------------------------------------------------------* START-OF-SELECTION. PERFORM main. *----------------------------------------------------------------------* *--- Event: END-OF-SELECTION *----------------------------------------------------------------------* END-OF-SELECTION. RETURN. *----------------------------------------------------------------------* *--- Subroutines *----------------------------------------------------------------------* *&---------------------------------------------------------------------* *& Form MAIN *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM main. DATA: lt_t100 TYPE STANDARD TABLE OF ts_t100 WITH DEFAULT KEY, lt_message TYPE tt_message, lt_attach TYPE tt_attach, ls_message LIKE LINE OF lt_message, ls_attach LIKE LINE OF lt_attach, lv_error TYPE sysubrc, lv_reciever TYPE sysubrc. FIELD-SYMBOLS: LIKE LINE OF lt_t100. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CLEAR: lt_t100[], lt_attach[], lt_message[], ls_attach, lv_error, lv_reciever. *--- get defined messages of message class ZV from database table T100 SELECT arbgb msgnr text INTO TABLE lt_t100 FROM t100 "#EC CI_SGLSELECT *--- ATC message: Access to single record buffered table T100 cannot use buffer *--- Finding can be suppressed with pseudo comment "#EC CI_GENBUFF or "#EC CI_SGLSELECT WHERE sprsl EQ sy-langu AND arbgb EQ 'S#'. IF sy-subrc NE 0. CLEAR lt_t100[]. ENDIF. *--- populate eMail message body. ls_message = 'With the attached file, all messages of message class S# are provided.'(001). APPEND ls_message TO lt_message. *--- populate data table / attachment *--- write header line containing the column headers CONCATENATE 'ARBGB' 'MSGNR' 'TEXT' INTO ls_attach SEPARATED BY cl_abap_char_utilities=>horizontal_tab. CONCATENATE ls_attach cl_abap_char_utilities=>cr_lf INTO ls_attach. APPEND ls_attach TO lt_attach. *--- write content LOOP AT lt_t100 ASSIGNING . CONCATENATE -arbgb -msgnr -text INTO ls_attach SEPARATED BY cl_abap_char_utilities=>horizontal_tab. CONCATENATE ls_attach cl_abap_char_utilities=>cr_lf INTO ls_attach. APPEND ls_attach TO lt_attach. ENDLOOP. *--- send attachment by eMail PERFORM send_file_as_email_attachment USING lt_message[] lt_attach[] p_email 'Example .txt file as attachment'(002) 'txt'(003) 'File Name'(004) space CHANGING lv_error lv_reciever. COMMIT WORK AND WAIT. * WAIT UP TO 2 SECONDS. * SUBMIT rsconn01 WITH mode = 'INT' WITH output = 'X' AND RETURN. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ENDFORM. " MAIN *&---------------------------------------------------------------------* *& Form SEND_FILE_AS_EMAIL_ATTACHMENT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->IT_MESSAGE[] text * -->IT_ATTACH[] text * -->IV_EMAIL text * -->IV_MTITLE text * -->IV_FORMAT text * -->IV_FILENAME text * -->IV_ATTDESCRIPTION text * <--EV_ERROR text * <--EV_RECIEVER text *----------------------------------------------------------------------* FORM send_file_as_email_attachment USING it_message TYPE tt_message it_attach TYPE tt_attach iv_email TYPE any iv_mtitle TYPE any iv_format TYPE any iv_filename TYPE any iv_attdescription TYPE any CHANGING ev_error TYPE sysubrc ev_receiver TYPE sysubrc. DATA: lt_packing_list TYPE STANDARD TABLE OF sopcklsti1 WITH DEFAULT KEY, lt_receivers TYPE STANDARD TABLE OF somlreci1 WITH DEFAULT KEY, ls_packing_list LIKE LINE OF lt_packing_list, ls_receivers LIKE LINE OF lt_receivers, ls_doc_data TYPE sodocchgi1, lv_mtitle TYPE sodocchgi1-obj_descr, lv_email TYPE somlreci1-receiver, lv_format TYPE so_obj_tp, lv_attdescription TYPE so_obj_nam, lv_attfilename TYPE so_obj_des, lv_sent_all TYPE sonv-flag, lv_index TYPE i, lv_length TYPE i. FIELD-SYMBOLS: LIKE LINE OF it_attach, LIKE LINE OF lt_receivers. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CLEAR: ev_error, ev_receiver. *--- convert field values lv_email = iv_email. lv_mtitle = iv_mtitle. lv_format = iv_format. lv_attdescription = iv_attdescription. lv_attfilename = iv_filename. *--- attachment: compute total size of the attachment in byte size CLEAR ls_doc_data. DESCRIBE TABLE it_attach LINES lv_index. READ TABLE it_attach ASSIGNING INDEX lv_index. IF sy-subrc EQ 0. MESSAGE i094(s#) DISPLAY LIKE 'W'. "Keine Daten vorhanden RETURN. ENDIF. DESCRIBE FIELD LENGTH lv_length IN BYTE MODE. ls_doc_data-doc_size = ( lv_index - 1 ) * lv_length + strlen( ). ls_doc_data-obj_langu = sy-langu. ls_doc_data-obj_name = 'SAPRPT'. ls_doc_data-obj_descr = lv_mtitle. ls_doc_data-sensitivty = 'F'. *--- prepare content of the email: message body and attachment CLEAR: lt_packing_list[], ls_packing_list. ls_packing_list-transf_bin = space. ls_packing_list-head_start = 1. ls_packing_list-head_num = 0. ls_packing_list-body_start = 1. ls_packing_list-doc_type = 'RAW'. APPEND ls_packing_list TO lt_packing_list. ls_packing_list-transf_bin = 'X'. ls_packing_list-head_start = 1. ls_packing_list-head_num = 1. ls_packing_list-body_start = 1. ls_packing_list-body_num = lv_index. ls_packing_list-doc_type = lv_format. ls_packing_list-obj_descr = lv_attdescription. ls_packing_list-obj_name = lv_attfilename. ls_packing_list-doc_size = ls_packing_list-body_num * lv_length. APPEND ls_packing_list TO lt_packing_list. *--- prepare email receivers CLEAR: lt_receivers[], ls_receivers. ls_receivers-receiver = lv_email. ls_receivers-rec_type = 'U'. ls_receivers-com_type = 'INT'. ls_receivers-notif_del = 'X'. ls_receivers-notif_ndel = 'X'. APPEND ls_receivers TO lt_receivers. *--- send eMail CLEAR lv_sent_all. CALL FUNCTION 'SO_DOCUMENT_SEND_API1' EXPORTING document_data = ls_doc_data put_in_outbox = 'X' * SENDER_ADDRESS = SY-UNAME * SENDER_ADDRESS_TYPE = 'B' commit_work = 'X' * IP_ENCRYPT = * IP_SIGN = * IV_VSI_PROFILE = IMPORTING sent_to_all = lv_sent_all * NEW_OBJECT_ID = * SENDER_ID = TABLES packing_list = lt_packing_list[] * OBJECT_HEADER = contents_bin = it_attach[] contents_txt = it_message[] * CONTENTS_HEX = * OBJECT_PARA = * OBJECT_PARB = receivers = lt_receivers[] * ET_VSI_ERROR = EXCEPTIONS too_many_receivers = 1 document_not_sent = 2 document_type_not_exist = 3 operation_no_authorization = 4 parameter_error = 5 x_error = 6 enqueue_error = 7 OTHERS = 8. ev_error = sy-subrc. LOOP AT lt_receivers ASSIGNING . IF -retrn_code GT ev_receiver. ev_receiver = -retrn_code. ENDIF. ENDLOOP. IF lv_sent_all IS NOT INITIAL. RETURN. ENDIF. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ENDFORM. "SEND_FILE_AS_EMAIL_ATTACHMENT **CLASS cl_abap_char_utilities DEFINITION LOAD. ** **CONSTANTS: ** con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab, ** con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf. ** **TYPES: ** BEGIN OF ts_emp_dat, ** arbgb TYPE t100-arbgb, ** msgnr TYPE t100-msgnr, ** text TYPE t100-text, ** END OF ts_emp_dat, ** tt_document_data TYPE sodocchgi1, ** tt_packing_list TYPE sopcklsti1, ** tt_attachment TYPE solisti1, ** tt_body_msg TYPE solisti1, ** tt_receivers TYPE somlreci1. ** ** **START-OF-SELECTION. ** PERFORM send_email. ** ** **END-OF-SELECTION. ** RETURN. ** ** ***&---------------------------------------------------------------------* ***& Form SEND_EMAIL ***&---------------------------------------------------------------------* *** text ***----------------------------------------------------------------------* **FORM send_email. ** ** DATA: ** lt_emp_data TYPE STANDARD TABLE OF ts_emp_dat WITH DEFAULT KEY, ** lt_document_data TYPE STANDARD TABLE OF tt_document_data WITH DEFAULT KEY, ** lt_packing_list TYPE STANDARD TABLE OF tt_packing_list WITH DEFAULT KEY, ** lt_attachment TYPE STANDARD TABLE OF tt_attachment WITH DEFAULT KEY, ** lt_body_msg TYPE STANDARD TABLE OF tt_body_msg WITH DEFAULT KEY, ** lt_receivers TYPE STANDARD TABLE OF tt_receivers WITH DEFAULT KEY, ** ls_document_data LIKE LINE OF lt_document_data, ** ls_packing_list LIKE LINE OF lt_packing_list, ** ls_attachment LIKE LINE OF lt_attachment, ** ls_body_msg LIKE LINE OF lt_body_msg, ** ls_receivers LIKE LINE OF lt_receivers, ** lv_sent_to_all TYPE sonv-flag, ** lv_tab_lines TYPE i. ** ** FIELD-SYMBOLS: ** TYPE ts_emp_dat. ** *** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ** CLEAR: ** lt_emp_data[], lt_document_data[], lt_packing_list[], lt_attachment[], ** lt_body_msg[], lt_receivers[], ls_document_data, ls_packing_list, ** ls_attachment, ls_body_msg, ls_receivers, lv_sent_to_all, lv_tab_lines. ** ** SELECT arbgb msgnr text INTO TABLE lt_emp_data FROM t100 UP TO 150 ROWS ** WHERE arbgb LIKE 'ZV%'. ** IF sy-subrc NE 0. ** CLEAR lt_emp_data[]. ** ENDIF. ** ** CONCATENATE 'ARBGB' 'MSGNR' 'TEXT' ** INTO ls_attachment SEPARATED BY con_tab. ** CONCATENATE con_cret ls_attachment ** INTO ls_attachment. ** APPEND ls_attachment TO lt_attachment. ** LOOP AT lt_emp_data ASSIGNING . ** CONCATENATE -arbgb -msgnr -text ** INTO ls_attachment SEPARATED BY con_tab. ** CONCATENATE con_cret ls_attachment ** INTO ls_attachment. ** APPEND ls_attachment TO lt_attachment. ** ENDLOOP. ** CLEAR ls_attachment. ** ** DEFINE build_body_of_mail. ** ls_body_msg = &1. ** append ls_body_msg to lt_body_msg. ** END-OF-DEFINITION. ** build_body_of_mail space. ** build_body_of_mail 'Hi,'. ** build_body_of_mail space. ** build_body_of_mail 'I am fine. How are you? How are you doing?'. ** build_body_of_mail 'This program has been created to send simple mail'. ** build_body_of_mail 'with subject, body with address of the sender.'. ** build_body_of_mail space. ** build_body_of_mail 'Regards,'. ** build_body_of_mail space. ** build_body_of_mail 'Somebody,'. ** build_body_of_mail 'SAP ABAP Developer'. ** CLEAR ls_body_msg. ** ** DESCRIBE TABLE lt_body_msg LINES lv_tab_lines. ** ls_packing_list-head_start = 1. ** ls_packing_list-head_num = 0. ** ls_packing_list-body_start = 1. ** ls_packing_list-body_num = lv_tab_lines. ** ls_packing_list-doc_type = 'RAW'. ** APPEND ls_packing_list TO lt_packing_list. ** CLEAR ls_packing_list. ** ls_packing_list-transf_bin = 'X'. ** ls_packing_list-head_start = 1. ** ls_packing_list-head_num = 1. ** ls_packing_list-body_start = 1. ** DESCRIBE TABLE lt_attachment LINES ls_packing_list-body_num. ** ls_packing_list-doc_type = 'TXT'. ** ls_packing_list-obj_descr = 'Text Attachment'. ** ls_packing_list-obj_name = 'TXT_ATTACHMENT'. ** ls_packing_list-doc_size = ls_packing_list-body_num * 255. ** APPEND ls_packing_list TO lt_packing_list. ** CLEAR ls_packing_list. ** ** ls_document_data-obj_name = 'DEMO_E-MAIL'. ** ls_document_data-obj_descr = 'Demo eMail-Versand mit Anlage über SAP ABAP'. ** ls_document_data-obj_langu = sy-langu. ** READ TABLE lt_attachment INTO ls_attachment INDEX lv_tab_lines. ** ls_document_data-doc_size = ( lv_tab_lines - 1 ) * 255 + strlen( ls_attachment ). ** ** ls_receivers-rec_type = 'U'. "Internet address ** ls_receivers-receiver = 'michael.eutin.ext@bauermedia.com'. ** ls_receivers-com_type = 'INT'. ** ls_receivers-notif_del = 'X'. ** ls_receivers-notif_ndel = 'X'. ** APPEND ls_receivers TO lt_receivers . ** CLEAR ls_receivers. ** ** CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' ** EXPORTING ** document_data = ls_document_data ** put_in_outbox = 'X' ** commit_work = 'X' ** IMPORTING ** sent_to_all = lv_sent_to_all ** TABLES ** packing_list = lt_packing_list[] ** contents_bin = lt_attachment[] ** contents_txt = lt_body_msg[] ** receivers = lt_receivers[] ** EXCEPTIONS ** too_many_receivers = 1 ** document_not_sent = 2 ** document_type_not_exist = 3 ** operation_no_authorization = 4 ** parameter_error = 5 ** x_error = 6 ** enqueue_error = 7 ** OTHERS = 8. ** IF sy-subrc EQ 0 . ** MESSAGE'Mail has been successfully sent...' TYPE 'S'. *** ELSE. *** WAIT UP TO 2 SECONDS. *** "This program starts the SAPconnect send process. *** SUBMIT rsconn01 WITH mode = 'INT' *** WITH output = 'X' *** AND RETURN. ** ENDIF. *** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ** **ENDFORM. "SEND_EMAIL *--- The End!