Posted: Tue Nov 11, 2008 10:16 pm Post subject: Send Excel to external email from within ABAP program
Send external email from within ABAP program
The code below demonstrates how to send an email to an external email address([email protected]), where the data is stored within a .xls attachment.
Code:
*&---------------------------------------------------------------------*
*& Report ZEMAIL_ATTACH *
*& *
*&---------------------------------------------------------------------*
*& Example of sending external email via SAPCONNECT *
*& *
*&---------------------------------------------------------------------*
REPORT ZEMAIL_ATTACH .
TABLES: ekko.
PARAMETERS: p_email TYPE somlreci1-receiver
DEFAULT '[email protected]'.
TYPES: BEGIN OF t_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,
END OF t_ekpo.
DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0,
wa_ekpo TYPE t_ekpo.
TYPES: BEGIN OF t_charekpo,
ebeln(10) TYPE c,
ebelp(5) TYPE c,
aedat(8) TYPE c,
matnr(18) TYPE c,
END OF t_charekpo.
DATA: wa_charekpo TYPE t_charekpo.
DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
WITH HEADER LINE.
DATA: 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,
t_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE,
w_cnt TYPE i,
w_sent_all(1) TYPE c,
w_doc_data LIKE sodocchgi1,
gd_error TYPE sy-subrc,
gd_reciever TYPE sy-subrc.
************************************************************************
*START_OF_SELECTION
START-OF-SELECTION.
* Retrieve sample data from table ekpo
PERFORM data_retrieval.
* Populate table with detaisl to be entered into .xls file
PERFORM build_xls_data_table.
************************************************************************
*END-OF-SELECTION
END-OF-SELECTION.
* Populate message body text
perform populate_email_message_body.
* Send file by email as .xls speadsheet
PERFORM send_file_as_email_attachment
tables it_message
it_attach
using p_email
'Example .xls documnet attachment'
'XLS'
'filename'
' '
' '
' '
changing gd_error
gd_reciever.
* Instructs mail send program for SAPCONNECT to send email(rsconn01)
PERFORM initiate_mail_execute_program.
*&---------------------------------------------------------------------*
*& Form DATA_RETRIEVAL
*&---------------------------------------------------------------------*
* Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
SELECT ebeln ebelp aedat matnr
UP TO 10 ROWS
FROM ekpo
INTO TABLE it_ekpo.
ENDFORM. " DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*& Form BUILD_XLS_DATA_TABLE
*&---------------------------------------------------------------------*
* Build data table for .xls document
*----------------------------------------------------------------------*
FORM build_xls_data_table.
CONSTANTS: 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 thnen you will
*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 con_cret it_attach 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 con_cret it_attach INTO it_attach.
APPEND it_attach.
ENDLOOP.
ENDFORM. " BUILD_XLS_DATA_TABLE
*&---------------------------------------------------------------------*
*& Form SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------*
* Send email
*----------------------------------------------------------------------*
FORM send_file_as_email_attachment tables pit_message
pit_attach
using p_email
p_mtitle
p_format
p_filename
p_attdescription
p_sender_address
p_sender_addres_type
changing p_error
p_reciever.
DATA: ld_error TYPE sy-subrc,
ld_reciever TYPE sy-subrc,
ld_mtitle LIKE sodocchgi1-obj_descr,
ld_email LIKE somlreci1-receiver,
ld_format TYPE so_obj_tp ,
ld_attdescription TYPE so_obj_nam ,
ld_attfilename TYPE so_obj_des ,
ld_sender_address LIKE soextreci1-receiver,
ld_sender_address_type LIKE soextreci1-adr_typ,
ld_receiver LIKE sy-subrc.
*&---------------------------------------------------------------------*
*& Form INITIATE_MAIL_EXECUTE_PROGRAM
*&---------------------------------------------------------------------*
* Instructs mail send program for SAPCONNECT to send email.
*----------------------------------------------------------------------*
FORM initiate_mail_execute_program.
WAIT UP TO 2 SECONDS.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.
ENDFORM. " INITIATE_MAIL_EXECUTE_PROGRAM
*&---------------------------------------------------------------------*
*& Form POPULATE_EMAIL_MESSAGE_BODY
*&---------------------------------------------------------------------*
* Populate message body text
*----------------------------------------------------------------------*
form populate_email_message_body.
REFRESH it_message.
it_message = 'Please find attached a list test ekpo records'.
APPEND it_message.
endform. " POPULATE_EMAIL_MESSAGE_BODY
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.