0% found this document useful (0 votes)
91 views6 pages

Send Email

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views6 pages

Send Email

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Automating Email Sending in ABAP: A Comprehensive Guide with HTML Support

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

data: lv_sender type uname,


lv_recipient_email type ad_smtpadr,
lv_subject type string,
lv_message type string.

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:

data(it_body_txt) = cl_document_bcs=>string_to_soli( ip_string = lv_message ).

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.

data(o_document) = cl_document_bcs=>create_document( i_type = 'HTML'


i_text = it_body_txt
i_subject = conv so_obj_des(
lv_subject ) ).

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:

data(o_document) = cl_document_bcs=>create_document( i_type = 'RAW'


i_text = it_body_txt
i_subject = conv so_obj_des(
lv_subject ) ).

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:

data(o_sender) = cl_sapuser_bcs=>create( lv_sender ).


o_send_request->set_sender( o_sender ).

The recipient is set using cl_cam_address_bcs. We specify the recipient email


address and also mark the email as express for immediate delivery:

data(o_recipient) = cl_cam_address_bcs=>create_internet_address( lv_recipient_email ).


o_send_request->add_recipient( i_recipient = o_recipient, i_express = abap_true ).

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.

Setting i_with_error_screen = abap_true ensures that any errors during the


sending process will be displayed. The commit work finalizes the transaction, sending
the email through the SAP system.

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:

catch cx_root into data(e_text).


write:/ e_text->get_text( ).

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

Here’s the entire program, including comments to explain each section:

REPORT z_send_email.

" Variable declarations for email components


data: lv_sender type uname, " Current SAP system user
lv_recipient_email type ad_smtpadr, " Email address of the recipient
lv_subject type string, " Email subject
lv_message type string. " Email body

" Initialize variables


lv_sender = sy-uname. " SAP user
lv_recipient_email = '[email protected]'. " Target email address
lv_subject = 'Test Subject 1'. " Email subject line
lv_message = 'Test email 1'. " Email body content

" Convert message body into a format usable by SAP


data(it_body_txt) = cl_document_bcs=>string_to_soli( ip_string = lv_message ).

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

" Create the send request object


data(o_send_request) = cl_bcs=>create_persistent( ).
o_send_request->set_message_subject( ip_subject = lv_subject ). " Set email subject
o_send_request->set_document( o_document ). " Attach email document

" Set the sender as the current SAP user


data(o_sender) = cl_sapuser_bcs=>create( lv_sender ).
o_send_request->set_sender( o_sender ).

" Set the recipient email address


data(o_recipient) = cl_cam_address_bcs=>create_internet_address( lv_recipient_email
).
o_send_request->add_recipient( i_recipient = o_recipient, i_express = abap_true ). "
Express flag

" Send the email immediately


o_send_request->set_send_immediately( abap_true ).
" Execute the send request
o_send_request->send( i_with_error_screen = abap_true ).

" Commit the transaction


COMMIT WORK.

" Confirm email sent successfully


IF sy-subrc = 0.
WRITE:/ 'Email sent successfully.'.
ENDIF.

CATCH cx_root INTO DATA(e_text).


" Handle errors and display the exception message
WRITE:/ e_text->get_text( ).
ENDTRY.

You might also like