Menu

How to Send E-Mails from SAP ERP

2014-08-09       

Sending e-mails from SAP ERP is probably the most common development task (or maybe the second common, after building an ALV report). Because of that, let me show you how to do it – I would’ve appreciated it as an ABAP beginner.

Unsere Literaturempfehlung

SAP – Der technische Einstieg: SAP-Technologien und Konzepte

Sind Sie neu in der SAP-Welt und möchten schnell mitreden? Mit diesem Buch finden Sie sich erfolgreich im SAP-Umfeld zurecht! Sie steigen direkt in die Konzepte und Technologien der SAP-Software ein und lernen die unterschiedlichen Systeme und Prozesse kennen. Schritt für Schritt zeigen die Autoren Ihnen die wichtigsten technischen Aufgaben und die Zusammenhänge zwischen den Objekten. Dank der vielen Beispiele und Abbildungen finden Sie sich schnell im SAP-Umfeld zurecht und können direkt in SAP ERP oder SAP S/4HANA anfangen.

Bei amazon.de ansehen →

SAP ERP considers e-mails a “document”. There are several other types, such as SAPOffice messages (a type of SAP internal mails that can be managed in the transaction SBWP), faxes, X.400 messages and more. All of these documents can be sent (and managed) via a single API that is contained in the function group SOI1.

The correct function module to create an e-mail is SO_NEW_DOCUMENT_SEND_API1. It performs two steps at once: a new document is created in the database, and it is sent to the respective receivers. It’s also possible to do this in two steps; the necessary function modules for this are SO_DOCUMENT_INSERT_API1 and SO_OLD_DOCUMENT_SEND_API1. For now however, let’s focus on the first function module.

Usage of SO_NEW_DOCUMENT_SEND_API1

Let’s look at the IMPORTING parameters of the function module first. The parameter DOCUMENT_DATA must be supplied with a structure of type SODOCCHGI1. It contains document meta information such as an object name, or the object description. This is important: the object description in field  SODOCCHGI1-OBJ_DESCR will become the title of your e-mail. Another important field is PROC_TYPE, but this is described in another article.

The DOCUMENT_TYPE parameter defines the document class. For e-mails, use the default (“RAW”). Apart from RAW, there are also BIN for binary files and SCR for SAPScript documents.

The other two parameters can be supplied, but the default is usually sufficient. If you don’t want to (or can’t) execute a COMMIT WORK after the function call, then supply the COMMIT_WORK parameter with an X.

Let’s look at the TABLES next. If you want to send a plain text e-mail, the most important parameter is OBJECT_CONTENT. It is a structure with a single element – LINE – that contains the actual body of the e-mail. Fill this table with the text that should go into your mail.

The table parameters OBJECT_HEADEROBJECT_PARA and OBJECT_PARB are only relevant for executable documents (again, this is described in another post). However of course, you need to fill the RECEIVERS table, which is a structure of type SOMLRECI1. The important fields are:

And that’s it already! You can now execute the function module and send e-mails to your recipients. Find out on the next page how you can monitor the sending process of your mails.

Monitoring the e-mail send process

After you’ve sent the e-mail from your program, you can make sure it actually leaves the SAP system. For this, there’s the transaction SCOT.

SAPConnect Administration

When you start SCOT, you’re presented with an overview of the different types of messages that are available, as well as the amount of messages being sent.

You can also monitor the progress of each individual message you sent. From the menu bar, select Utilities > Overview of send orders. This will take you to a list of messages that are in the sending queue or have been sent. You can adapt the selection criteria to your liking. This is also a very good way to debug your mail sending program. If your e-mail does not show up in this list, it hasn’t been created (did you forget to COMMIT WORK?).

That’s it for this article! Tomorrow I’ll discuss advanced e-mail functionality like attachments and sending executable documents. For your reference, here’s an example program to send e-mails that you can copy.

Example program for email sending from SAP ERP

 REPORT ztestemail.
 
 DATA lt_objcont TYPE STANDARD TABLE OF solisti1.
 DATA ls_objcont TYPE solisti1.
 DATA lt_receivers TYPE STANDARD TABLE OF somlreci1.
 DATA ls_receivers TYPE somlreci1.
 DATA ls_document_data LIKE sodocchgi1.
 
 * Fill the document header
 ls_document_data-obj_name = 'Test'.
 ls_document_data-obj_descr = 'This is the e-mail title!'.
 
 * Fill the mail contents
 ls_objcont-line = 'This is a test e-mail'.
 APPEND ls_objcont TO lt_objcont.
 ls_objcont-line = 'It has a second line of text'.
 APPEND ls_objcont TO lt_objcont.
 
 * Fill the receiver list
 ls_receivers-receiver = sy-uname. "Send that to myself
 ls_receivers-rec_type = 'B'.
 APPEND ls_receivers TO lt_receivers.
 
 ls_receivers-receiver = 'someotherguy@test.com'.
 ls_receivers-rec_type = 'U'.
 APPEND ls_receivers TO lt_receivers.
 
 * Send the e-mail
 CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
   EXPORTING
     document_type              = 'RAW'
     document_data              = ls_document_data
     put_in_outbox              = 'X'
   TABLES
     object_content             = lt_objcont
     receivers                  = lt_receivers
   EXCEPTIONS
     too_many_receivers         = 1
     document_not_sent          = 2
     operation_no_authorization = 3
     OTHERS                     = 4.
 
 COMMIT WORK.