Send Internal table data as an Excel attachment in MAIL
*&---------------------------------------------------------------------*
*& Report ZVB_EXCEL_MAIL
*&---------------------------------------------------------------------*
REPORT zvb_excel_mail.
TABLES: ekko.
TYPES: BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,
END OF ty_ekpo.
*For Excel
TYPES: BEGIN OF ty_charekpo,
ebeln TYPE char10,
ebelp TYPE char5,
aedat TYPE char8,
matnr TYPE char18,
END OF ty_charekpo.
DATA: it_ekpo TYPE STANDARD TABLE OF ty_ekpo INITIAL SIZE 0.
DATA: wa_charekpo TYPE ty_charekpo.
DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0 WITH HEADER LINE,
it_attach TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0 WITH HEADER LINE.
DATA: t_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
t_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,
t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
t_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,
w_cnt TYPE i,
w_sent_all(1) TYPE c,
w_doc_data LIKE sodocchgi1.
PARAMETERS: p_email TYPE somlreci1-receiver DEFAULT 'abc@gmail.com'.
START-OF-SELECTION.
* Fetch data from ekpo
PERFORM fetch_data.
*Populate table with details to be entered into .xls file
PERFORM build_xls_data_table.
*Send file by email as .xls speadsheet
PERFORM send_file_as_email_attachment.
FORM fetch_data.
SELECT ebeln ebelp aedat matnr
UP TO 10 ROWS
FROM ekpo
INTO TABLE it_ekpo.
ENDFORM.
FORM build_xls_data_table.
CONSTANTS:con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.
* con_cret TYPE x VALUE '0D', ”OK for non Unicode
* con_tab TYPE x VALUE '09′. ”OK for non Unicode
*If you have Unicode check active in program attributes then you
*need to declare constants as follows
*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.
CONCATENATE 'EBELN' 'EBELP' 'AEDAT' 'MATNR' INTO it_attach SEPARATED BY con_tab.
CONCATENATE it_attach con_tab INTO it_attach.
CONCATENATE it_attach con_cret INTO it_attach.
APPEND it_attach.
LOOP AT it_ekpo INTO wa_charekpo.
CONCATENATE wa_charekpo-ebeln wa_charekpo-ebelp
wa_charekpo-aedat wa_charekpo-matnr
INTO it_attach SEPARATED BY con_tab.
CONCATENATE it_attach con_tab INTO it_attach.
CONCATENATE it_attach con_cret INTO it_attach.
APPEND it_attach.
ENDLOOP.
ENDFORM.
FORM send_file_as_email_attachment.
DATA: ld_error TYPE sy-subrc,
ld_reciever TYPE sy-subrc,
ld_sender_address LIKE soextreci1-receiver,
ld_receiver LIKE sy-subrc.
* Fill the document data.
CLEAR w_doc_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 = 'PO'.
w_doc_data-obj_descr = 'Purchase Order Details' .
w_doc_data-sensitivty = 'F'.
* Fill the document data and get size of attachment
READ TABLE it_attach INDEX w_cnt.
w_doc_data-doc_size = ( w_cnt - 1 ) * 255 + strlen( it_attach ).
* Mail Body
REFRESH it_message.
it_message = 'Dear User,'.
APPEND it_message.
it_message = 'Please find attached a list test ekpo records'.
APPEND it_message.
* 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 it_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.
t_attachment[] = it_attach[].
DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.
t_packing_list-doc_type = 'XLS'.
t_packing_list-obj_descr = 'PO Excel'.
t_packing_list-obj_name = 'PO Excel'.
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 = p_email.
t_receivers-rec_type = 'U'.
t_receivers-com_type = 'INT'.
*t_receivers-notif_del = 'X'.
*t_receivers-notif_ndel = 'X'.
APPEND t_receivers.
ld_sender_address = sy-uname.
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = w_doc_data
put_in_outbox = 'X'
sender_address = ld_sender_address
sender_address_type = 'B'
commit_work = 'X'
IMPORTING
sent_to_all = w_sent_all
TABLES
packing_list = t_packing_list
contents_bin = t_attachment
contents_txt = it_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.
** Populate zerror return code
ld_error = sy-subrc.
* Populate zreceiver return code
LOOP AT t_receivers.
ld_receiver = t_receivers-retrn_code.
ENDLOOP.
IF ld_error = 0.
WAIT UP TO 2 SECONDS.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.
ENDIF.
ENDFORM.
Output:
Go to T-code 'SOST' and check
Comments
Post a Comment