How to send email with excel file attachment from itab in ABAP
To send email with excel file from ABAP its neccesary to use SO_NEW_DOCUMENT_ATT_SEND_API1 this function module its availabel on SAP R3 and SAP hana, this will depends of your SAP version.
Convert ITAB internal table to excel and send in email
To do this, we will select information from SPFLI table and will save on an internal table, after this, We will copy this information to lt_attachment table, this table will our excel file and then We will send this excel to an email. Please see source below
REPORT Z_TESTING. DATA: lt_mailrecipients TYPE STANDARD TABLE OF somlrec90 WITH HEADER LINE, lt_mailtxt TYPE STANDARD TABLE OF soli, l_mailtxt TYPE soli, lt_attachment TYPE STANDARD TABLE OF solisti1 WITH HEADER LINE, lt_mailsubject TYPE sodocchgi1, lt_packing_list TYPE STANDARD TABLE OF sopcklsti1 WITH HEADER LINE, gv_cnt TYPE i, wa_spfli TYPE spfli. lt_mailrecipients-rec_type = 'U'. lt_mailrecipients-com_type = 'INT'. lt_mailrecipients-receiver = 'your-email@gmail.com'. APPEND lt_mailrecipients . CLEAR lt_mailrecipients . l_mailtxt-line = 'Good mornings'. APPEND l_mailtxt TO lt_mailtxt. CLEAR l_mailtxt. l_mailtxt-line = 'This its the departure'. APPEND l_mailtxt TO lt_mailtxt. CLEAR l_mailtxt. l_mailtxt-line = 'Gracías'. APPEND l_mailtxt TO lt_mailtxt. CLEAR l_mailtxt. * Fill header CLASS cl_abap_char_utilities DEFINITION LOAD. CONCATENATE 'Please check follow ' 'information' INTO lt_attachment SEPARATED BY cl_abap_char_utilities=>horizontal_tab. APPEND lt_attachment. CLEAR lt_attachment. CONCATENATE cl_abap_char_utilities=>newline lt_attachment INTO lt_attachment. APPEND lt_attachment. CLEAR lt_attachment. * APPEND lt_attachment. CLEAR lt_attachment. CONCATENATE 'ID' 'City from' 'Cty to' INTO lt_attachment SEPARATED BY cl_abap_char_utilities=>horizontal_tab. APPEND lt_attachment. CLEAR lt_attachment. SELECT * FROM SPFLI INTO TABLE @data(t_spfli) UP TO 10 ROWS. * Fill information LOOP AT t_spfli INTO wa_spfli. CONCATENATE wa_spfli-carrid wa_spfli-cityfrom wa_spfli-cityto INTO lt_attachment SEPARATED BY space. * CONCATENATE wa_spfli-carrid wa_spfli-cityfrom wa_spfli-cityto INTO lt_attachment SEPARATED BY cl_abap_char_utilities=>horizontal_tab. CONCATENATE cl_abap_char_utilities=>newline lt_attachment INTO lt_attachment. APPEND lt_attachment. CLEAR lt_attachment. ENDLOOP. lt_packing_list-transf_bin = SPACE. lt_packing_list-head_start = 1. lt_packing_list-head_num = 0. lt_packing_list-body_start = 1. lt_packing_list-body_num = LINES( lt_mailtxt ). lt_packing_list-doc_type = 'RAW'. APPEND lt_packing_list. CLEAR lt_packing_list. lt_packing_list-transf_bin = 'X'. lt_packing_list-head_start = 1. lt_packing_list-head_num = 1. lt_packing_list-body_start = 1. lt_packing_list-body_num = LINES( lt_attachment ). lt_packing_list-doc_type = 'XLS'. " You can give RAW incase if you want just a txt file. lt_packing_list-obj_name = 'data.xls'. lt_packing_list-obj_descr = 'data.xls'. lt_packing_list-doc_size = lt_packing_list-body_num * 255. APPEND lt_packing_list. CLEAR lt_packing_list. lt_mailsubject-obj_name = 'MAILATTCH'. lt_mailsubject-obj_langu = sy-langu. lt_mailsubject-obj_descr = 'You have got mail'. lt_mailsubject-sensitivty = 'F'. gv_cnt = LINES( lt_attachment ). lt_mailsubject-doc_size = ( gv_cnt - 1 ) * 255 + STRLEN( lt_attachment ). CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' EXPORTING document_data = lt_mailsubject TABLES packing_list = lt_packing_list contents_bin = lt_attachment contents_txt = lt_mailtxt receivers = lt_mailrecipients 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. COMMIT WORK. SUBMIT rsconn01 WITH MODE = 'INT' AND RETURN. ENDIF.
After execute this code you will recive and email with and excel file sending from abap. You can limit who send email with password on your program, to do this its neccesary has a Field type password in abap.
After you execute this source code, you will receive an email with and excel file attached into email, its very important that your SAP server has configured SMTP to send emails and the ports opened.
How to verify if my ABAP program sent and email
After you execute this programs you will recive and email with excel attached from ABAP program, if you not recive this email its neccesary to check whats happed, to do this please go to Tcode: SOST, this the code shows you all emails sending from SAP.
Fr4nc
Leave a Reply