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 :

 

 

7 Comments
You must be Logged on to comment or reply to a post.
    • I guess you can do it in two steps:

       

      1. by embedding something like this in the email

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

       

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

         

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

       

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