Skip to Content
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:

  1. Select the package ( or Local objects ) and right click.
  2. 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-

  1. Languages             Maintain Email in different languages
  2. CDS Fields             Set of CDS view fields used in Email content
  3. Email Subject         Email subject description
  4. Body HTML            Email body content in HTML
  5. 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 :

 

 

15 Comments
You must be Logged on to comment or reply to a post.
    • 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):

      <img src="cid:any-name-you-want">

       

      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.

       

      lo_email_api->render( IMPORTING ev_body_html = DATA(body_html) ).
      
      DATA(body_html_soli) = cl_bcs_convert=>string_to_soli( body_html ).
      
      DATA(multipart) = NEW cl_gbt_multirelated_service( ).
      multipart->set_main_html
          EXPORTING
            content     = body_html_soli
            description = 'descriptionhtml'.
      multipart->add_binary_part
          EXPORTING
            content      = image_png_solix " BYTES OF IMAGE (PNG or other type)
            filename     = 'filename.png'
            extension    = 'PNG'
            description  = 'descriptionpng'
            content_type = 'image/png'
            length       = l_len
            content_id   = 'any-name-you-want'.
      DATA(doc_bcs) = cl_document_bcs=>create_from_multirelated(
              EXPORTING
                i_subject          = 'email subject'
                i_multirel_service = multipart ).
      lo_bcs->set_document( doc_bcs ).
      ...

       

      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:

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
      
      <html>
      <head>
        <meta name="generator" content="HTML Tidy for SAP R/3 (vers 25 March 2009), see www.w3.org">
      
        <title></title>
      </head>
      
      <body>
      Text before icon. Icon "LEFT ARROW": <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAIAAADUsmlHAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAVklEQVR42mN88/IGA7mAiYECMAg1uza+JVMzMTqxayZSJwMDAyNaVBGjc3e9MBabibcTXTOpOlE0wx1DZlSRqp8RM23D3U/QLCY8gUlmIiFSP+PQzJIA5rYeiLeQLUYAAAAASUVORK5CYII=">. Text after icon.
      </body>
      </html>
      
      • 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.

    • Thanks Neelesh.

      I have used CSS stsylehseet in the HTML in the example in this blog.

            <style type="text/css">
                    
      /* Client-specific Styles */
               #outlook a {padding:0;} /* Force Outlook to provide a "view in browser" menu link. */
               body{width:100% !important; -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%; margin:0; padding:0;}
                      
               /* Prevent Webkit and Windows Mobile platforms from changing default font sizes, while not breaking desktop design. */        
               #backgroundTable {margin:0; padding:0; width:100% !important; line-height: 100% !important;}
               img {outline:none; text-decoration:none;border:none; -ms-interpolation-mode: bicubic;}
               a img {border:none;}       
               /*p {margin: 0px 0px !important;}*/
               table td {border-collapse: collapse;}
               table {border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;  }
               a {color: #193c6b;text-decoration:none;} 
               
               /* Our Styles */
               body {font-family: Calibri, Arial, sans-serif;}
      		 .csLink a {color:#666666!important;text-decoration:none!important;}
      		 .csNr a {color:#193c6b!important;text-decoration:none!important;}
      		 .csNr2 a {color:#333333!important;text-decoration:none!important;}
              
               a[x-apple-data-detectors] {
                  color: inherit !important;
                  text-decoration: none !important;
                  font-size: inherit !important;
                  font-family: inherit !important;
                  font-weight: inherit !important;
                  line-height: inherit !important;
              }
                /*IPAD STYLES*/
                @media only screen and (max-width: 640px) {             
                  table.devicewidth, img.gradientbottom  {width: 440px!important;}
                  table.devicewidthinner, table.prow {width: 400px!important;} 
      			img.space20{width:20px!important;}
      			img.imggradient {width:212px!important;}
      			img.bannerimg {width:440px!important;height:73px!important;}
      			.removeTablet{display:none!important;}
      			td.padleft {padding-left:0px!important;}
               }
               
               /*IPHONE STYLES*/
               @media only screen and (max-width: 480px) {
                   table.devicewidth, img.gradientbottom  {width: 320px!important;}
      			 table.devicewidthinner{width: 280px!important;}
                   img.imggradient {width:92px!important;}
      			 img.bannerimg {width:320px!important;height:53px!important;}
      			 .headline{font-size:14px!important;text-align:left!important;}
      			 
      			}			      
                  
            </style>

      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 🙂

     

  • 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 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

    • 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