Send Email From SAP Using ABAP

Jimbo's picture

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.

http://ih1.redbubble.net/image.209546867.7830/mp,550x550,matte,ffffff,t.3u1.jpg|https://s-media-cache-ak0.pinimg.com/564x/13/57/fa/1357fa4bcfa5e14a19200488ebb5660e.jpg|http://render.fineartamerica.com/images/rendered/default/metal-print/6.750/8.000/break/images-medium-5/1956-canada-mountain-goat-stamp-bill-owen.jpg|http://st2.depositphotos.com/4741067/7987/i/450/depositphotos_79870372-Mountain-goat-on-postage-stamp.jpg|http://ih1.redbubble.net/image.209546867.7830/mp,550x550,matte,ffffff,t.3u1.jpg|http://media4.allnumis.com/1189/120-2014-mountain-goat_1189_131688425c71d67L.jpg|http://2.bp.blogspot.com/-H8ULXgV5a2U/VNo56iLy79I/AAAAAAAAHpM/Sijnjk-pnAc/s1600/266dogmails.jpg|http://previews.123rf.com/images/sam11111962/sam111119621601/sam11111962160100427/50554888-the-image-of-a-mountain-goat-postage-stamp-brown-price-25-Stock-Photo.jpg
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.

https://lh3.googleusercontent.com/-Oqv0TQ29P4Y/UWBZBBYl6GI/AAAAAAAAO_I/BM2rf1oB9wgtiI-mEpBiOasCM--eYAQwQ/w506-h635/production.jpg|http://agilescout.com/wp-content/uploads/2012/05/i-dont-test-my-code.jpg|https://img1.steemit.com/0x0/http://s2.quickmeme.com/img/70/70710c8aff156b84becbd522bea259023fb64be7369ec8c3bc9638f348284ab1.jpg|http://s2.quickmeme.com/img/52/528d72dc89bb766235fd755fc8366f658b414fecf10bd7987927cc947d7e8fbd.jpg|https://cdn.meme.am/instances/500x/65206829.jpg|https://cdn.meme.am/instances/500x/67536464.jpg|http://s2.quickmeme.com/img/dc/dc61e85ea1112972b36dd86fd1894da1dad31538c0b8b37ab1b613c8a020dd99.jpg|https://i.imgflip.com/kb7y2.jpg|https://lh3.googleusercontent.com/-AttLlUO5hC8/UmfbQrO98XI/AAAAAAAAAJ8/-I1un4EKgoQ/w506-h632/8054.image_thumb_35C6E986.png|https://cdn.meme.am/instances/500x/9689481.jpg|http://s2.quickmeme.com/img/36/36a6014e51de57d8abf88656a93766ed46f299b0d7bbe58b06eb93a7f86edb76.jpgWhere 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.

http://ih1.redbubble.net/image.209546867.7830/mp,550x550,matte,ffffff,t.3u1.jpg|https://s-media-cache-ak0.pinimg.com/564x/13/57/fa/1357fa4bcfa5e14a19200488ebb5660e.jpg|http://render.fineartamerica.com/images/rendered/default/metal-print/6.750/8.000/break/images-medium-5/1956-canada-mountain-goat-stamp-bill-owen.jpg|http://st2.depositphotos.com/4741067/7987/i/450/depositphotos_79870372-Mountain-goat-on-postage-stamp.jpg|http://ih1.redbubble.net/image.209546867.7830/mp,550x550,matte,ffffff,t.3u1.jpg|http://media4.allnumis.com/1189/120-2014-mountain-goat_1189_131688425c71d67L.jpg|http://2.bp.blogspot.com/-H8ULXgV5a2U/VNo56iLy79I/AAAAAAAAHpM/Sijnjk-pnAc/s1600/266dogmails.jpg|http://previews.123rf.com/images/sam11111962/sam111119621601/sam11111962160100427/50554888-the-image-of-a-mountain-goat-postage-stamp-brown-price-25-Stock-Photo.jpg

Programming Language: 
ABAP