Send Email
Send Email
Sending emails through ABAP is an essential tool for automating notifications, generating
reports, or integrating workflows seamlessly within your SAP landscape. As ABAP developers,
mastering this feature can add great value to our applications, whether we’re working on internal
alerts, customer-facing communications, or daily/weekly reporting.
In this post, I’ll walk through a detailed ABAP program for sending emails, focusing not just on
the technical implementation, but also the flexibility of creating both plain text and
HTML-formatted emails. I’ll share insights into real-world use cases, best practices, and a
complete commented code snippet at the end to give you everything you need to get started.
Program Breakdown
1. Variable Declarations and Initialization The first step is declaring and initializing
variables that define the key components of the email. Here we have the sender
(lv_sender), recipient (lv_recipient_email), subject (lv_subject), and the
message (lv_message).
The values are dynamically assigned based on the current system user for the sender
(sy-uname) and hardcoded values for the email content in this example:
lv_sender = sy-uname.
lv_recipient_email = '[email protected]'.
lv_subject = 'test subject 1'.
lv_message = 'test email 1'.
Tip: While hardcoding values for testing is acceptable, in a real-world application, you’ll
likely want to make these inputs dynamic (for example, pulling the recipient from a
database table or workflow trigger).
2. Converting the Email Body to SAP’s Internal Format To send the message as an
email, it needs to be converted into a format SAP can handle. Here, we use
string_to_soli to transform our message body into an internal table format:
Note: If the email body contains HTML formatting, you'll need to specify this when
creating the document. Instead of 'RAW' (used for plain text emails), we set i_type to
'HTML'. This will ensure that the message is processed as HTML content, rendering the
formatting correctly in the recipient’s inbox.
3. Creating the Email Document The next step is creating the email document using
cl_document_bcs. This method takes the email body and subject, associating it with a
document object:
Here, i_type = 'RAW' specifies that the email body is plain text. If you need the body
in HTML format, use i_type = 'HTML' as mentioned earlier.
4. Creating and Configuring the Send Request SAP’s BCS framework requires creating
a send request to process email dispatches. This is where the email document is tied to
the request:
data(o_send_request) = cl_bcs=>create_persistent( ).
o_send_request->set_message_subject( ip_subject = lv_subject ).
o_send_request->set_document( o_document ).
This ensures that the email subject and document are properly linked to the send
request.
5. Setting the Sender and Recipient The email sender is defined as the current SAP user
through cl_sapuser_bcs:
This could easily be expanded to send emails to multiple recipients or include CC/BCC
addresses. Simply call add_recipient multiple times for different recipients.
6. Sending the Email At this point, everything is configured, and we can send the email:
o_send_request->set_send_immediately( abap_true ).
o_send_request->send( i_with_error_screen = abap_true ).
commit work.
7. Error Handling To ensure that any errors during the email sending process are
captured, we wrap the send logic in a TRY...CATCH block:
This way, any exceptions are caught and their text is displayed, making it easier to
troubleshoot issues.
Use Cases and Best Practices
● System Notifications: Automatically send out system alerts (e.g., batch job failures or
system monitoring) directly to the relevant administrators.
● Customer Communication: Automatically send invoices, delivery notes, or order
confirmations from the SAP system to external customers.
● Workflow Integration: Use this approach to notify users of task assignments or
approval requests in workflows.
● Daily or Weekly Reports: Generate and send automated reports to management or
stakeholders without manual intervention.
HTML Emails: Many modern business communications use HTML formatting. For example, you
might include links, images, or formatted tables in your email body. By specifying i_type =
'HTML', you ensure that the email renders correctly in the recipient’s inbox, just like any web
page.
Error Handling: Always implement proper exception handling around email sending operations.
This ensures that failures are caught and logged, allowing you to take corrective action quickly.
Complete Program Code
REPORT z_send_email.
TRY.
" Create document (email) with RAW or HTML type
data(o_document) = cl_document_bcs=>create_document(
i_type = 'HTML' " Use 'RAW' for plain text,
'HTML' for HTML formatted
i_text = it_body_txt " Converted body text
i_subject = conv so_obj_des( lv_subject ) ). " Email subject