Hello All,
Today, I’ll try to demonstrate quite easy way to generate and send email using ABAP.
(It’s my first blog post, please point out the scope of improvements I may need. Thank you)
From start of my ABAP journey, I always felt bad about the way e-mails are generated & managed by ABAP. Recently I got chance to implement a project which also involved generating emails from scratch. I wanted customer facing e-mail to have modern look. Even plain text email using text-element/message class takes lots of efforts and makes the code lengthy, so it was a nightmare to me generate good looking modern email.
While searching web, I found the article demonstrating consumption of HTML template in ABAP:
https://sap4tech.net/html-template-in-abap/. So I utilized this to generate modern e-mails for new project with really less ABAP code.
Since many of us may face similar issue, I wrote really small class zcl_easy_email on top of FM WWW_HTML_MERGER to make the email generation easy in ABAP for others .
Here’s my small contribution to Community:
Github Repository :
easyHtmlEmail
Repo contains report zeasy_email_demo demonstrates the consumption of zcl_easy_email to generate email.
Steps to generate Email with HTML templates:
- Build HTML templates for email & upload it in SAP system in SMW0.
Template must contain Placeholders for dynamic values (highlighted in yellow are placeholders)
![](/legacyfs/online/storage/blog_attachments/2021/10/Master_html-1.png) |
![](/legacyfs/online/storage/blog_attachments/2021/10/Child_html-1.png) |
HTML template - Master
|
HTML template - Child
|
PS: I am not so good in HTML & CSS , HTML templates used in demo are take from
BBBootstrap public templates
I am using a Master & child template to generate a email with reusability in mind. Templates upload in SMW0:
![](/legacyfs/online/storage/blog_attachments/2021/10/SMW0.png)
Template uploaded in SMW0
- Create instance of zcl_easy_email and pass in the template names
DATA : go_EASY_EMAIL TYPE REF TO ZCL_EASY_EMAIL.
CREATE OBJECT go_EASY_EMAIL .
go_EASY_EMAIL->set_subject(
title = 'mail subject '
).
go_EASY_EMAIL->set_template(
EXPORTING
p_template = 'ZTESTEMAIL'
p_mastertemplate = 'ZTESTEMAILMASTER'
).
- Pass in Placeholder & dynamic value pairs
go_EASY_EMAIL->replace_placeholder(
EXPORTING
placeholder_name = '!VBELN!'
replacement_type = 'R'
single_value = ' 23412 '
).
go_EASY_EMAIL->replace_placeholder(
EXPORTING
placeholder_name = '!PRICE!'
replacement_type = 'R'
single_value = ' 1234.90’
).
data : multi_line TYPE
soli_tab,
line TYPE soli.
line = 'LINE 1 : LINE1 </br>'.
APPEND LINE to multi_line.
line = 'LINE 2 : LINE2 </br>'.
APPEND LINE to multi_line.
go_EASY_EMAIL->replace_placeholder(
EXPORTING
placeholder_name = '!LOG!'
replacement_type = ' '
multi_line = multi_line
).
- Build email , add recipients, send the mail
go_EASY_EMAIL->add_email( email = p_email ).
go_EASY_EMAIL->build_body( ).
go_EASY_EMAIL->send_mail( 'X' ).
- Email visible in SOST: place holders replaced with required values.
![](/legacyfs/online/storage/blog_attachments/2021/10/Final_Mail_in_SOST-1.png)
Generated E-mail visible in SOST
Restrictions:
- WWW_HTML_MERGER uses soli_tab to process data, so 255 character is the limit for each line in html template.
- SAP stores these HTML templates as RAW in INDX table, so we need to take care of character encoding while building the templates. In our system (BASIS 701) HTML template was stored in ISO-8859-2 encoding (code page 1401), so while building template encoding was selected asISO-8859-2 in text editor. Incorrect encoding will lead to character conversion issue.
Additional Info: SAP has provide something similar to this in S/4 suite, refer:
E-Mail Templates in S/4 HANA
Please correct me if mistaken at some point, also suggest improvement points
Feel free to clone the repo in sandbox system using abapgit.
I hope the this blog post will be helpful. If you have questions/suggestions/issues about this, please feel free to leave them in the comments section.
If you know some good solution for emails, please mention it in comments below. ( lets keep all email solutions on single page
😜 )
Update:
Added attachment functionality
Ability to handle error & custom action by exporting instance of cl_bsc from method build_mail()