Send Email From SAP Using ABAP
Many times throughout a career in Legacy Data Migration, the convenience of automatically sending output to an email recipient will be strong enough to give it a try. Updating data or a log file on a network share automatically during an Access macro or LSMW conversion step is perfect for most data, but sometimes, email is the only solution.
Naturally, this code is couched in a form
and included in Jimbo's LMSW Toolbox for convenience, but it requires a table to be passed in so this is also an explanation of how to pass a table into a form. For whatever reason, the tables
declaration must be declared before declaring any using
parameters.
The first parameter passed to the form is an internal table called LT_MAILTXT and is listed by necessity before the other parameters. The structure of the table is not declared here, but it is declared later by the calling program and must be of the type SOLI--that code is further down on the page in the Calling the Snippet section.
Once all of the necessary parameters have been declared and populated, the SO_NEW_DOCUMENT_SEND_API1
function is called using the parameters passed into the form and those generated therein. There are many exception handlers thrown by this function, but in a well-managed system where the function is only called with valid parameters, there is little reason to handle errors.
form SendEmail tables LT_MAILTXT using lvRecipient lvSubject. DATA: LT_MAILSUBJECT TYPE SODOCCHGI1, LT_MAILRECIPIENTS TYPE STANDARD TABLE OF SOMLREC90 WITH HEADER LINE. "Recipients LT_MAILRECIPIENTS-REC_TYPE = 'U'. LT_MAILRECIPIENTS-RECEIVER = lvRecipient. APPEND LT_MAILRECIPIENTS . CLEAR LT_MAILRECIPIENTS . "Subject. LT_MAILSUBJECT-OBJ_NAME = 'TEST'. LT_MAILSUBJECT-OBJ_LANGU = SY-LANGU. LT_MAILSUBJECT-OBJ_DESCR = lvSubject. "Send Mail CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1' EXPORTING DOCUMENT_DATA = LT_MAILSUBJECT DOCUMENT_TYPE = 'RAW' PUT_IN_OUTBOX = ' ' COMMIT_WORK = 'X' TABLES OBJECT_CONTENT = LT_MAILTXT RECEIVERS = LT_MAILRECIPIENTS EXCEPTIONS others = 1. IF SY-SUBRC EQ 0. COMMIT WORK. " Push mail out from SAP outbox SUBMIT RSCONN01 WITH MODE = 'INT' AND RETURN. ENDIF. endform.
Calling the Snippet
Now all that is left is to call the form from a program. Start by declaring the variables to be passed into the form.
parameters: p_email type string default 'recipient@server.com'. data: lSubject type string, it_MAILTXT TYPE STANDARD TABLE OF SOLI WITH HEADER line.
This example comes from a tool that generates delta files to be passed to a travel agency. The manager asked to receive the output from the program each day and, until this snippet was implemented, it was a manual process.
* Write out the message.... select single LTX from t247 into lSubject where MNR eq sy-datum+4(2) and SPRAS eq 'E'. "Name of month. concatenate sy-datum+6(2) lSubject into lSubject separated by space. it_MailTxt = 'Hi Steve,'. append it_MailTxt. it_MailTxt = ''. append it_MailTxt. concatenate 'Here are the deltas for' lSubject into it_MailTxt separated by space. concatenate it_MailTxt '.' into it_MailTxt. "Add a period. append it_MailTxt. it_MailTxt = ''. append it_MailTxt. it_MailTxt = 'Best regards,'. append it_MailTxt. it_MailTxt = 'Jim'. append it_MailTxt. it_MailTxt = ''. append it_MailTxt. concatenate 'Deltas for' lSubject into lSubject separated by space.
A handful of other lines are added to mirror the delta report that is sent to the travel agency. The logic is simple enough.
loop at it_delta into it_MailTxt. append it_MailTxt. endloop.
Finally, the form is called like this. Remember that when calling a form, the tables must be passed before other parameters.
perform SendEmail tables it_MailTxt using p_email lSubject.
Where to test
While developing this code, testing in a development system produced no emails. Only after it was transported to a production system did emails start to flow.
Credit where it is due
This snippet was written with some help from an answer submitted by Swarna Ramya in 2007. This snippet is little more than his brilliant code wrapped in a form and called by passing a few parameters.