Introduction: In this article, I’ll demonstrate, how we can draw an HTML table in an E-mail body by using an E-mail template with all the styles we can do in Microsoft Word and a simple way to do this.

In SAP_BASIS 750 and above releases or in S/4 systems, we have an Email template feature available along with a CDS view functionality.

Please visit the blog https://blogs.sap.com/2019/10/12/e-mail-templates-in-s4-hana/  to get the initial overview of the email template.

Step 1: We can create an Email template in the object navigator by navigating to the Email template option shown as per the image below.

Step 2: Next, simply create Email content (Email Body) in MS Word, along with a table including a header and only one blank row for table data.

Step 3: Now save the document in .htm format as per below.

Step 4: Next open the e-mail template and choose the ‘Load local file’ option.

Step 5: Moving further, identify where the blank table row is in the Body HTML content.

Step 6: Copy cell attributes to have in the code. Remove the blank table row <tr>…..</tr> along with its all cells and maintain a placeholder e.g. IT_FLIGHTS in Email Body.

Step 7: Replace IT_FLIGHTS in the code as per snippet.

DATA(lv_cell_attrib) = ` width=85 valign=top style=’width:77.95pt;border-top:none;border-left:` && `none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;` && `mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;` && `mso-border-alt:solid windowtext .5pt;background:white;mso-background-themecolor:` && `background1;padding:0in 5.4pt 0in 5.4pt’>` && `<p class=MsoNormal align=center style=’text-align:center’><span` && `style=’mso-bidi-font-family:Calibri’><o:p>`. LOOP AT lt_sflights ASSIGNING FIELD-SYMBOL(<fs_sflights>). lv_content = |{ lv_content }<tr>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-carrid }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-connid }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-fldate }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-price }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-planetype }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-paymentsum }</o:p></span></p></td>|. lv_content = |{ lv_content }</tr>|. ENDLOOP. REPLACE ‘IT_FLIGHTS’ WITH lv_content INTO lv_body_html.

 

Step 8: Next trigger an email to see the result. We can clearly see all the details are present in table format in the email body.

Code Snippet: Please refer complete code as below.

PARAMETERS : p_email TYPE ad_smtpadr. “abc@gmail.com CONSTANTS: lc_priority TYPE bcs_docimp VALUE ‘1’, lc_template_id TYPE smtg_tmpl_id VALUE ‘ZTEST’. DATA: lv_content TYPE string. SELECT carrid, connid, fldate, price, planetype, paymentsum FROM sflight INTO TABLE @DATA(lt_sflights) UP TO 10 ROWS. IF sy-subrc EQ 0. DATA(lo_email_api_ref) = cl_smtg_email_api=>get_instance( iv_template_id = lc_template_id ). DATA(lo_send_request_ref) = cl_bcs=>create_persistent( ). DATA(i_cds_key) = VALUE if_smtg_email_template=>ty_gt_data_key( ). ” Get the RawHTML Content and Replace the palceholder with the INV Details in the email lo_email_api_ref->render( EXPORTING iv_language = sy-langu it_data_key = i_cds_key IMPORTING ev_subject = DATA(lv_subject) ev_body_html = DATA(lv_body_html) ). DATA(lv_cell_attrib) = ` width=85 valign=top style=’width:77.95pt;border-top:none;border-left:` && `none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;` && `mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;` && `mso-border-alt:solid windowtext .5pt;background:white;mso-background-themecolor:` && `background1;padding:0in 5.4pt 0in 5.4pt’>` && `<p class=MsoNormal align=center style=’text-align:center’><span` && `style=’mso-bidi-font-family:Calibri’><o:p>`. LOOP AT lt_sflights ASSIGNING FIELD-SYMBOL(<fs_sflights>). lv_content = |{ lv_content }<tr>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-carrid }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-connid }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-fldate }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-price }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-planetype }</o:p></span></p></td>|. lv_content = |{ lv_content }<td{ lv_cell_attrib && <fs_sflights>-paymentsum }</o:p></span></p></td>|. lv_content = |{ lv_content }</tr>|. ENDLOOP. REPLACE ‘IT_FLIGHTS’ WITH lv_content INTO lv_body_html. DATA(lv_body_html_soli) = cl_bcs_convert=>string_to_soli( lv_body_html ). ” Build HTML for Sending DATA(lo_multipart_ref) = NEW cl_gbt_multirelated_service( ). lo_multipart_ref->set_main_html( EXPORTING content = lv_body_html_soli description = ‘flightDetails’ ). *Create & Set the Email Document DATA(lo_doc_bcs_ref) = cl_document_bcs=>create_from_multirelated( EXPORTING i_subject = CONV so_obj_des( lv_subject ) “Set the Email Subject i_importance = lc_priority i_multirel_service = lo_multipart_ref ). lo_send_request_ref->set_document( lo_doc_bcs_ref ). *Populate sender name DATA(lo_sender_ref) = cl_sapuser_bcs=>create( sy-uname ). IF lo_sender_ref IS BOUND. lo_send_request_ref->set_sender( i_sender = lo_sender_ref ). ENDIF. DATA(lo_recipient_ref) = cl_cam_address_bcs=>create_internet_address( p_email ). IF lo_recipient_ref IS BOUND. *Add recipient to send request lo_send_request_ref->add_recipient( EXPORTING i_recipient = lo_recipient_ref i_express = abap_true ). ENDIF. * Send email DATA(lv_sent_to_all) = lo_send_request_ref->send( ). * If e-mail is successful then do commit work IF lv_sent_to_all = abap_true. COMMIT WORK AND WAIT. ENDIF. ENDIF.

This concludes the functionality.

Thank you for reading the article. I truly hope you liked it. Please feel free to share your thoughts and suggestions.

FollowLikeRSS Feed

Alert Moderator

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x