Technical Articles
E-Mail Templates in S/4 HANA
E-Mail communication is very common business requirements in day-to-day life. SAP understands that and comes up with very interesting feature in S/4 HANA (cloud and on premise both) – E-Mail Templates. In this article, I will provide a little overview and a demo of E-Mail templates.
What is E-Mail templates ?
With S/4 HANA Output Management, SAP provides E-mail templates to be configured which will be mapped to output types in BRF+. We can maintain HTML and Plain text in different languages in these E-Mail templates and also, can map a CDS view for handling dynamic variables. This feature would save a lot of hardcoding or other custom ways to maintain E-mail content as done in past. Although in S/4 HANA, SAP uses E-Mail templates specifically in output management, still we can use this feature independent of output configurations and we will see in Demo section below for its usage.
Prerequisites:
- Basic Knowledge of CDS views.
- Good knowledge of ABAP.
- Basic understanding of HTML.
How to create E-Mail Template?
There is no specific transaction to create email templates but we can create it as a repository object in SE80 transaction as follow:
- Select the package ( or Local objects ) and right click.
- Select “Create” -> “More” -> “Email Template”
However, we can always view/Edit the existing E-Mail templates from program SMTG_WB_START.
Different Component in E-Mail templates:
Header –
- We need to maintain a name / description for the email template.
- Also, we can maintain a CDS view which should be pre-delivered and can be used to provide dynamic variables in email content (body or subject).
Texts-
- Languages Maintain Email in different languages
- CDS Fields Set of CDS view fields used in Email content
- Email Subject Email subject description
- Body HTML Email body content in HTML
- Body Plain Text Email body content in plain text
Dynamic Variables in E-mail Content:
For maintaining dynamic variables, we would need to create a CDS view which would contain the required data. For each different email variables, we can pass CDS key with Name/Value pair to Email Template API classes and replace the variables with desired content very easily.
- Create a CDS view ZRSCDS_INVOICE_DATA (for demo, I took reference of billing document header and Item tables).
@AbapCatalog.sqlViewName: 'ZRSCDS_INVDATA'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Invoice Data Line Item wise'
//@VDM.viewType:#BASIC
define view ZRSCDS_INVOICE_DATA
as select from vbrk as zzrs_vbrk
join vbrp as zzrs_vbrp
on zzrs_vbrk.vbeln = zzrs_vbrp.vbeln
{
key zzrs_vbrk.vbeln,
key zzrs_vbrp.posnr,
zzrs_vbrk.fkart,
zzrs_vbrk.vbtyp,
@Semantics.currencyCode: true
zzrs_vbrk.waerk,
zzrs_vbrk.vkorg,
zzrs_vbrk.fkdat,
@Semantics.amount.currencyCode: 'waerk'
@DefaultAggregation: #SUM
zzrs_vbrk.netwr,
zzrs_vbrk.kunag as kunag,
zzrs_vbrp.fkimg,
zzrs_vbrp.vrkme,
zzrs_vbrp.meins,
zzrs_vbrp.matnr
}
- Add this CDS view in Email template Header.
- Assign CDS view fields in E-Mail Body and Subject where ever required.
Email Preview-
We can always preview our email template how it will look once sent to customers by clicking on “preview” button as highlighted:
How to call E-Mail Templates ?
Till now, I have created E-mail templates in system. Now, I would like to integrate this in one of the calling programs which send email to customers and email content will be taken from templates. For the demo purpose, I created a simple program where I can pass receiver email address, E-Mail Template, Language and CDS Key ( Billing Document in our case ).
SAP has provided E-Mail template API class which can be instantiated and used to get email content. Steps are as follow:
- Create instance of class CL_SMTG_EMAIL_API.
DATA(lo_email_api) = cl_smtg_email_api=>get_instance( iv_template_id = p_em_id ).
- Create instance of class CL_BCS.
DATA(lo_bcs) = cl_bcs=>create_persistent( ).
- Prepare CDS view Key table with Key Field name and value.
DATA(lt_cds_key) = VALUE ty_gt_data_key( ( name = 'vbeln' value = p_vbeln ) ).
- Integrate E-Mail subject and body with email instance
lo_email_api->render_bcs( io_bcs = lo_bcs iv_language = p_spras it_data_key = lt_cds_key ).
- Set Sender, receiver and send the email.
" Set Email Sender
DATA(lo_sender) = cl_sapuser_bcs=>create( sy-uname ).
lo_bcs->set_sender( i_sender = lo_sender ).
" Set Email Receiver(s)
DATA(lo_recipient) = cl_cam_address_bcs=>create_internet_address( p_rec ).
lo_bcs->add_recipient( EXPORTING i_recipient = lo_recipient ).
" Send Email
lo_bcs->send( ).
Selection-Screen of Demo Program :
Result Email with replacement of dynamic variables :
Hi prabhjot,
Excellent blog, can you please let me know how to embed images to the email template?
Regards
IK
I guess you can do it in two steps:
1. Position the image where you want in the body HTML, and assign it any CID name you want (Content-ID):
2. in the ABAP code, get the email HTML by using the method RENDER instead of RENDER_BCS, and create the document from scratch via BCS by using multipart, one part being the image to which you assign the Content-ID chosen in the first step.
Addendum October 15, 2019:
There's a second option:
You may also hardcode the image inside the body HTML with <img src="data:image/png;base64,HERE-BASE64-OF-IMAGE">. The advantage is that there's no ABAP code and the image is displayed if you preview the email, but the body HTML is difficult to maintain because it's full of base64 characters.
Example:
Thanks Sandra.
I already used option1 in the demo example. and It’s really helpful and good to know about option 2. I will try to integrate it in my applications now.
But in this case, we can't use email templates.
I'm confused about "option 1" and "option 2": are you talking about RENDER_BCS versus RENDER, or are you talking about the two steps of the solution I proposed (which are not options 1 and 2, it's steps 1 and 2)?
In which case can't we use email templates?
I misunderstood that .
I meant I used the step 1 in the demo example. I generated the image URL separately and added the url in HTML directly as <img src = imageURL> .
In that case, I didnt need to use step 2 and that's the real use of E-Mail templates -to customize email body content( plain or HTML ) and call that in applications.
I hope it clarify the confusion.
hi,
Could you, please, tell me how it does what you indicate above? (I meant I used the step 1 in the demo example. I generated the image URL separately and added the url in HTML directly as <img src = imageURL> )
I upload the image to MIME repository but i don´t know how to create the url of the image.
Regards
Thanks Prabhjot for very useful and self-explanatory blog.
can you confirm whether CSS can also be attached?
Thanks Neelesh.
I have used CSS stsylehseet in the HTML in the example in this blog.
I am not sure whether you want to use CSS explicitly as SAP has given option to add HTML files in the body HTML. But we can always integrate CSS in HTML and use here.
When I used class CL_BCS to create HTML mail there was some strange situation: Only inline CSS was ok to use in the body. It wasn't a problem of CL_BCS but of the mail clients like Outlook. Don't know if this is important in this context.
Hi Prabhjot,
Very nice and informative article. Please let me know if there is a way to add CC and BCC recipients in email?
Also, how to add system attachments via this class?
Thanks in advance.
Thanks Raza.
E-Mail templates are only used to configure E-Mail content which is Email Body and Subject.
Sender information with different roles TO,CC,BCC are not covered in this. You need to define or configure these information on your application where you will call email templates.
Same for attachments 🙂
That should be same way as we do with the BCS class, this is only to handle the e-mail body template. Right?
Hi Prabhjot Bhatia,
in the last few Weeks i am developing a Fiori App which uses the new E-Mail Templates, but for certain time i have problems with the text Replacement.
If you do a little research, you will quickly find, that the Replacement of the dynamic variables will happen in the APPLY( )-method of CL_SMTG_EMAIL_RENDERER. In my opinion it doesn't matter whether you call APPLY( ) directly or indirectly via BCS as long as you supply the method correctly.
But i have a big Problem with the Replacement of some Fields.
Most of the Fields are replaced without any problem, except two types: dates and currency amounts. Both have one in common: they have a differen internal format and an external format. Dates can be displayed in different shapes, depending on your systems customizing in table T005X . But every time i want to replace a date, e.g. a date of DDIC-type BLDAT it only displays the internal format like 20191015 instead of 15.10.2019 or 10/15/2019 etc.
Even by debugging and modifying some things i was unable to get the system to replace the variable with the external format.
In the coding at CL_SMTG_EMAIL_RENDERER in his private Method FORMAT_FIELD_VALUE i found following comment: "DATS field is not converted currently as the date format of the *receiver* of the mail template is not knwown... Using the format of the currently logged in user may not be appropriate." But this is exactly what i want to happen.
Is there any possibility to do so or do i have to implement the replacement in my application by myself?
Thanks in advance
Hi Prabhjot,
Thanks for the nice blog.
Can we add table in the template with dynamic variables in the table?
Thanks in advance.
Hi Arun,
Thanks for reading.
Since dynamic values are filled from CDS views, I don't think you can add tables in dynamic variables.
Do you have any real time example where tables are required as dynamic variables?
Thanks and regards,
Prabhjot
Hi Prabhjot Bhatia,
I have a similar query. CDS can also return multiple records therefore can that be added as part of a tabular section in the email. For example, consider that there is a requirement to send the open invoices at end of every month to all customers. Is there any way this can be achieved to get an output like below?
example CDS Structure with association to customer master as _customer
{
key inv_no,
key payer,
amount,
date,
//associations
_customer
}
Regards
Gurpreet
Hi Prabhjot Bhatia,
Thanks for your article.its really helpful.
is it possible to display a table in the Email Template?
as I tried but unable to get the option for that.
Regards,
Shrikant More.
Hey Shrikant,
as far as i know E-Mail-Templates fully support HTML. Did you already tried to use HTML for creating a Table? If you don't know how this works out in HTML there are plenty of good Tutorials out there, i.e.W3Schools where i always look up stuff like that.
And in these tables you are free to use any given data from your CDS View.
I did't tried out but for tables of variable length you could modify the mails HTML-Body with your ABAP-Code and insert another row for each row of your datatable (e.g. internal table).
Regards,
Timo
HI Prabhjot Bhatia ,
by any change can it be integrated with classes or dynamic tables . Use case I am working on SAP MDG and here tables are dynamic so we want to read table we need to class method to get data no other possible way to read .
If that is possible then that will be big assets as email is frequent requirement we have at any project we do.
BR,
Varun
Hi Varun,
If I understand correctly, you want email data dynamically instead of depending on data driven cds fields. If yes, it's possible to integrate those fields and populate the data independent of cds view data.
Thanks,
Prabhjot
Hello Prabhjot,
Thanks for the blog.
How can pass the data to the E-Mail template independent of the CDS. I am calling the template from report program where I need to generate an FIORI URL and append it to a table. Generation of URL is not possible in AMDP, so I have to do it via ABAP.
Essentially E-Mail with table consists of data and URL in a cell on every row.
Thanks,
Sathya
HI Prabhjot Bhatia ,
Correct , so I take example of Material , I would like to send Materiel related field data at email body , but during MDG process this data wont be available at MARA table it would at some temp table which generates dynamically at each system and only way to read is via MDG APIs.
I am sure CDS view , but can CDS view call an standard API and get data or I create rapper api and which can feed to CDS view ?
Regards,
Varun
CDS is a data modelling technique no it can not call any API.
Hello all ( Prabhjot Bhatia )
Someone can help me?
I need to create a Email template and into the body include a list of documents.
So, i should include in the template a TABLE.
I was debugging the std class but, when recover the information from CDS, always is with ONE RECORD ( not getting the list of documents)
Is it possible to send an email with a table with severals records?
thanks in advance
table with several records should ideally be in an attachment not the email body though it is possible using <table> of html. thanks
Hello @Prabhjot Bhatia
Thank you for this helpful blog .
Is this possible in ECC 702 version.
Thank you in advance
No.. It's only available from 7.5 version specifically to be used by SAP in new Output Management in S/4 HANA.
Nice one
Hi Prabhjot,
Excellent blog!
Is it possible to pass data to template variables directly from the invoking code instead of using a CDS view?
Class CL_SMTG_EMAIL_API has a method RENDER_BCS_W_DATA. It looks as this is what I need but there is no documentation about how to use the method.
Do you have any idea?
Thanks in advance
Fabrizio
Thanks..
I have used method RENDER_BCS_W_DATA and I will post a new blog to cover a business scenario. I agree with you regarding documentation for these topics but we also need to understand that these things are quite new and SAP is still doing/releasing OSS notes based on customer's requirements.So these blogposts would be better place for any documentation.
Hello Fabrizio,
I tried to use method RENDER_BCS_W_DATA. My idea was to user these fields also in the email template.
But if the fields are not part of the CDS view, the matches are deleted.
This method can only be used, to fill the values which are part of the CDS view.
Hello Thorsten,
may I ask you to show some more code? I also want to use some data from CDS View, but others should be set by code. To my understanding, this is what you've solved here.
The code you've shown above doesn't have any relation to CDS view anymore.
It would be really great, if you can provide a sample code of how to achieve this.
Patrick
Hello Patrick.
In my exmaple only varialbes are used, which are filled from code. Field CDS view is not filled.
subject mail template
Variables and their values are maintained with an internal table.
This is a central method to replace the variables with the corresponding content from IT_EMAIL_TPL_VARS_CDSKEY:
Maybe this can be adjusted to use CDS views and own variables.
Regards
Thorsten
Thank you Thorsten for this great example.
I was able to get RENDER_BCS_W_DATA running but it's like you said, if a field is not part of CDS view, it gets ignored. To use it, we have to define these code-filled fields as empty fields in CDS View.
Your code looks more clean as you don't need to define dynamic table anymore.
Thank you Prabhjot Bhatia , I was looking for something like this. Great work!!!
regards,
Prasenjit
Hi Prabhjot,
Nice blog on Email Templates!!
i had one question is it a clean core approach since in S/4HANA cloud you dont have option to create custom e-mail template ?
Regards,
Mayank
I'm not an expert but watching this openSAP video on email templates for Flexible Workflow in S/4 Cloud and it does talk about custom templates: https://microlearning.opensap.com/media/How+to+Set+Up+the+E-mail+Notification+for+Flexible+Workflow+-+SAP+S+4HANA+Cloud/1_3yuvoayt
So I'm curious where you got the information that there is no such option.
Hi Jelena Perfiljeva , Prabhjot Bhatia,
May I know if this approach still consider clean core in 2023?
Hello Prabhjot Bhatia,
Excellent Blog.
Could you Please Tell how to add images in the Email Template,
I checked the Solution that you provided in this Blog But Unfortunately I am not able to add Image in the Email Template.
So could you please Explain how to add Image in the Email Template
Regards
Ashok Kumar
Just follow the second option in this comment by Sandra Rossi
Convert your desired image into Base64 (you can use this site or google for any other). Copy the base64 code and use it as sandra used it here:
Just replace the part after 'base64 ,' with your actual base64 code of image
Hi Aditya Waghmare ,
I have tried the same approach for 'Maintain Email Templates' but it doesn't work. Could you please provide any other alternative to display the image in Email Body.
I found one solution where an image can be imported in MIME repository and generated URL can be attached in IMG SRC tag but this URL would be static and have to change in every environment.
Thanks for your response in advance.
With Best Regards,
Pratham Kapoor
Hi Pratham,
You must be doing some mistake as I am able to embed image via Base 64.
Did you add the base64 code this way: <img src = "base_64code">?
You can try and copy this email body in your system and see if it is working for you or not:
Preview of Email:

Email preview of Base64 image
Thanks Aditya Waghmare for your quick response.
Yes, in preview I wasn't able to see the image but when I send the email in Manage Dispute Cases, I can see the image. It resolves my query.
Regards,
Pratham Kapoor
Hi,
someone could post an example with multiple lines in cds wich can use in htlm template?
Hello Prabhjot Bhatia, nice blog!
I have a question regarding adding (multiple) attachments.
How would you do this when working with OPM en Adobe?
Is this also possible in the email template?
Were would I create the code to add additional attachments to the emai?
Kind regards,
Berry Blei
Hi Prabhjot Bhatia,
Excellent Blog.
I have one question, Is it possible to add System fields to the email template for ex : sy-sysid to the email template subject and text?
Thanks,
Prasanth