Several times everybody must have been come cross the requirement that from SAP a file has to be created as per the required template and send to Third part system to process over there.
For this scenario, if the any centralized file system is there where SAP and Third party system have been connected , the file can be sent to File system and from there the third part can pick file to process in the system.
Unfortunately Centralized file system is not there in that case the file can be sent via email to third party team. By using old Function Module SO_DOCUMENT_SEND_API1 itself easily the excel file can be sent via email as attachment.
Check below code and try yourself.
REPORT sy-repid.
DATA: li_attach TYPE TABLE OF solisti1,
li_message TYPE TABLE OF solisti1,
li_receivers TYPE TABLE OF somlreci1,
lw_attach TYPE solisti1,
lw_message TYPE solisti1.
DATA: ld_mtitle TYPE sodocchgi1-obj_descr,
ld_format TYPE so_obj_tp ,
ld_attdescription TYPE so_obj_nam ,
ld_attfilename TYPE so_obj_des ,
ld_sender_address TYPE soextreci1-receiver,
ld_sender_address_type TYPE soextreci1-adr_typ.
DATA: t_packing_list TYPE sopcklsti1 OCCURS 0 WITH HEADER LINE,
t_receivers TYPE somlreci1 OCCURS 0 WITH HEADER LINE,
t_attachment TYPE solisti1 OCCURS 0 WITH HEADER LINE,
w_cnt TYPE i,
w_doc_data TYPE sodocchgi1.
CONSTANTS:
con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.
*-------Populate Headings data for Excel File-------*
CONCATENATE 'Employee Number'
'Employee Name'
'Employee Address'
INTO lw_attach SEPARATED BY con_tab.
CONCATENATE con_cret lw_attach INTO lw_attach.
APPEND lw_attach TO li_attach.
CLEAR : lw_attach.
*----Populate Actual data for Excel file--------*
CONCATENATE '123421'
'Somu'
'Bangalore'
INTO lw_attach SEPARATED BY con_tab.
CONCATENATE con_cret lw_attach INTO lw_attach.
APPEND lw_attach TO li_attach.
CLEAR : lw_attach.
CONCATENATE '123423'
'Sasi'
'Bangalore'
INTO lw_attach SEPARATED BY con_tab.
CONCATENATE con_cret lw_attach INTO lw_attach.
APPEND lw_attach TO li_attach.
CLEAR : lw_attach.
REFRESH li_message.
CLEAR : lw_message.
lw_message = 'Email Body'.
APPEND lw_message TO li_message.
CLEAR : lw_message.
lw_message = 'Thanks & Regards'.
APPEND lw_message TO li_message.
CLEAR : lw_message.
lw_message = 'Raju C D'.
APPEND lw_message TO li_message.
CLEAR : lw_message.
* Email Sending
ld_mtitle = 'Employee Details'.
ld_format = 'XLS'.
ld_attfilename = 'ATTACHMENT'.
ld_attdescription = 'Emp_Details'.
* Fill the document data.
w_doc_data-doc_size = 1.
* Populate the subject/generic message attributes
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle .
w_doc_data-sensitivty = 'F'.
* Fill the document data and get size of attachment
CLEAR w_doc_data.
READ TABLE li_attach INTO lw_attach INDEX w_cnt.
w_doc_data-doc_size =
( w_cnt - 1 ) * 255 + STRLEN( lw_attach ).
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle.
w_doc_data-sensitivty = 'F'.
CLEAR t_attachment.
REFRESH t_attachment.
t_attachment[] = li_attach[].
* Describe the body of the message
CLEAR t_packing_list.
REFRESH t_packing_list.
t_packing_list-transf_bin = space.
t_packing_list-head_start = 1.
t_packing_list-head_num = 0.
t_packing_list-body_start = 1.
DESCRIBE TABLE li_message LINES t_packing_list-body_num.
t_packing_list-doc_type = 'RAW'.
APPEND t_packing_list.
* Create attachment notification
t_packing_list-transf_bin = 'X'.
t_packing_list-head_start = 1.
t_packing_list-head_num = 1.
t_packing_list-body_start = 1.
DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.
t_packing_list-doc_type = ld_format.
t_packing_list-obj_descr = ld_attdescription.
t_packing_list-obj_name = ld_attfilename.
t_packing_list-doc_size = t_packing_list-body_num * 255.
APPEND t_packing_list.
* Add the recipients email address
CLEAR t_receivers.
REFRESH t_receivers.
t_receivers-receiver = 'rajucd81@gmail.com'.
t_receivers-rec_type = 'U'.
t_receivers-com_type = 'INT'.
t_receivers-notif_del = 'X'.
t_receivers-notif_ndel = 'X'.
APPEND t_receivers.
li_receivers[] = t_receivers[].
* FM to sent Email with Attachment
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = w_doc_data
put_in_outbox = 'X'
sender_address = ld_sender_address
sender_address_type = ld_sender_address_type
commit_work = 'X'
* IMPORTING
* sent_to_all = w_sent_all
TABLES
packing_list = t_packing_list
contents_bin = t_attachment
contents_txt = li_message
receivers = t_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.
WRITE:' Email has been sent successfully'.
ENDIF.