Technical Articles
Easy to Generate & Maintain Email (ABAP + HTML templates) – easyHtmlEmail
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)
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:
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.
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.
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()
Nitinkumar:
Good explanation of the solution. I know there are multiple ways to generate email using ABAP, so I hope yours is useful to someone.
My only question, having managed various email generation schemes, is how to recognize and respond to error conditions. That might include malformed addresses (bad QA somewhere), or forwarders being offline or unresponsive, or just the plain old "552: mailbox full."
How could return codes be managed after that call?
Hello Jim,
I haven't exposed any such error handling points, will add this point soon.
Thanks a lot for the improvement point.
Workaround:
Hello Jim,
Would you mind sharing your preferred ways of generating E-mail. It would be great to know the experts way.
There are many different, beautiful solutions for the subject of "Sending E-Mail". I really like your work 🙂 Great that you provide a GitHub repository! Hope to see more great work from you in the future!
Thanks Michael for kind words, means a lot 😊.
Also it would be great If you could share some of your favorite solutions for "Sending E-Mail".
I won't let your Hope down 😊.
I think this is a good other solution to create a nice html e-mail. The variables in the HTML document will be filled by a CDS View.
https://blogs.sap.com/2019/10/12/e-mail-templates-in-s4-hana/
Hello Marcel,
Obviously that's a much better solution introduced by SAP, Hence I have mentioned it in additional info. But I think it's introduced only for S4 and later systems.
We don't have such option at least until NW 7.50 ( That's how much I could verify), So my post is mainly for older system which do not have e-mail templates in SE80 and specific classes.
Hi, i'am facing a problem with the charset , the HTML template is not displayed properly , do you have any suggestions for a solution .
Note that i tried to change the charset to ISO-8859-2 but some problem still exist .
Hi Halla,
Could you please share some more info & screenshots of problem?
Thank you for this solution Nitinkumar Gupta. I found it quite smooth and easy to implement; it works great!
Also thank you for the GitHub repository 🙂
Regards,
David
Nice alternative to the Standard SAP Email templates when working on a older system.
Ran into a slight problem with special characters but that was fixed by changing the character set of the html template to ISO 8859-1