SAP ABAP - Sample ABAP Program To Send E-Mail Through SAP System.

This sample program sends an excel sheet to the concerned person in an email format via SAP System.

SAMPLE PROGRAM:

REPORT zsample_send_mail.

TABLES: EKKO.

CLASS: CL_ABAP_CHAR_UTILITIES DEFINITION LOAD.

PARAMETERS: p_email TYPE somlreci1-receiver
DEFAULT 'XXX@YYY.ZZ'.

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_receiver TYPE sy-subrc.

************************************************************************

START-OF-SELECTION.

* RETRIEVE SAMPLE DATA FROM TABLE EXPO
PERFORM DATA_RETRIEVAL.

* POPULATE TABLE WITH DETAILS TO BE ENTERED TO BE ENTERED INTO .XLS FILE
PERFORM BUILD_XLS_DATA_TABLE.

************************************************************************

END-OF-SELECTION.

* POPULATE MESSAGE BODY TEXT
PERFORM POPULATE_EMAIL_MESSAGE_BODY.

* SEND FILE AS .XLS SPREADSHEET
PERFORM SEND_FILE_AS_EMAIL_ATTACHMENT
TABLES it_message
it_attach
USING p_email
'Example .xls documnet attachment'
'XLS'
'filename'
' '
' '
' '
CHANGING gd_error
gd_receiver.

* 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.

DATA : con_cret TYPE c VALUE cl_abap_char_utilities=>horizontal_tab, "OK for non Unicode
con_tab TYPE c VALUE cl_abap_char_utilities=>cr_lf.  "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_receiver.


DATA: ld_error TYPE sy-subrc,
ld_receiver TYPE sy-subrc,
ld_mtitle LIKE sodocchfi1-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.

ld_email = p_email.
ld_mtitle = p_mtitle.
ld_format = p_format.
ld_attdescription = p_attdescription.
ld_attfilename = p_filename.
ld_sender_address = p_sender_address.
ld_sender_address_type = p_sender_addres_type.


* 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 = 'Excel' .
* w_doc_data-sensitivty = 'F'.

* FILL THE DOCUMENT DATA AND GET SIZE OF ATTACHMENT
CLEAR w_doc_data.
READ TABLE it_attach INDEX w_cnt.
w_doc_data-doc_size = ( w_cnt - 1 ) * 255 + STRLEN( it_attach).
w_doc_data-obj_langu = sy_langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = 'Excel'.
* w_doc_data-sensitivty = 'F'.

CLEAR t_attachment.
REFRESH t_attachment.
t_attachment[] = pit_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 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.

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 RECIPIENTS E-MAIL ADDRESS
CLEAR t_receivers.
REFRESH t_receivers.
t_receivers-receiver = ld_email.
t_receivers-rec_type = 'U'.
t_receivers-com_type = 'INT'.
t_receivers-notif_del = 'X'.
t_receivers-notif_ndel = 'X'.
APPEND t_receivers.

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 = 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.

ENDFORM. "send_file_as_email_attachment


*&---------------------------------------------------------------------*
*& 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


MORE PROGRAMS ON REPORT PROGRAMMING:

- Generate PDF from ABAP List Output.

- Upload the financial documents from a legacy system (excel file) into SAP using transaction code FB01.

- Finding User Exits, Badi's & Enhancements For Any Transaction Or Program.

- Upload Excel Sheet Data Into Internal Table Using FM ALSM_EXCEL_TO_INTERNAL_TABLE.

- Uploading Large Data From Excel Sheet To Internal Table Using FM ALSM_EXCEL_TO_INTERNAL_TABLE

- ...Back To Index On Report Programming.



Your suggestions and comments are welcome in this section.

Please mail all your contributions to administrator@abapmadeeasy.com We request you to mention your Name, Designation, Experience & Organization you are working for. Your posts will be verified and posted in this site with your name.


1 comment: