Recently I had to create a mail process where I used the first time the “new” E-Mail-Templates. The following Blogs helped me do this:
https://weberpatrick.de/e-mail-templates/
https://weberpatrick.de/e-mail-templates-einfaches-beispiel/
https://weberpatrick.de/e-mail-template-daten-per-code-statt-cds/
https://community.sap.com/t5/enterprise-resource-planning-blogs-by-members/e-mail-templates-in-s-4-hana/ba-p/13397719
https://community.sap.com/t5/enterprise-resource-planning-blogs-by-members/e-mail-templates-in-s-4-hana-translations/ba-p/13440792
https://community.sap.com/t5/enterprise-resource-planning-blogs-by-members/e-mail-templates-in-s-4-hana-display-table-in-email-template/ba-p/13546304
In my case it was not possible to fetch the data via CDS View, I therefore had to manually fill the variables. Following a simplified version of my code.
You must create a CDS View, as this is the structure in which the variables defined in the mail text are mapped to, even if you do this manually.
define view entity ZMAIL_MY_TEMPLATE as select from pa0001 {
key pa0001.pernr,
pa0001.ename,
cast ( '' as abap.char( 20 ) ) as custom_field // this way you can add a field, that does not exist in the selected table
}
This is just one way, how you can manually fill the variables. Other ways are possible using the class cl_smtg_email_api
. I choose the method render_w_data
because it returned the mail text, and you manually can do additional changes to the mail text, like adding HTML-Table content, which otherwise gets “escaped” when filled via a replacement variable.
" dynamicaly build table for mail variables
DATA(lt_comp) = VALUE abap_component_tab( ( name = 'ename' type = CAST #( cl_abap_typedescr=>describe_by_name( 'EMNAM' ) ) )
( name = 'pernr' type = CAST #( cl_abap_typedescr=>describe_by_name( 'PERNR_D' ) ) ) ) .
" fill table with concrete data
DATA(lo_struct) = cl_abap_structdescr=>get( lt_comp ).
CREATE DATA mr_data TYPE HANDLE lo_struct.
ASSIGN mr_data->* TO FIELD-SYMBOL(<ls_data>).
<ls_data>-(1) = ename.
<ls_data>-(2) = pernr.
" create instance of eMail API, provide name of Mail template
DATA(lo_email_api) = cl_smtg_email_api=>get_instance( iv_template_id = 'ZMAIL_MY_TEMPLATE' ).
" render E-Mail for CL_BCS class. This replaces all placeholders with real data
lo_email_api->render_w_data( EXPORTING iv_language = sy-langu
ir_data = mr_data
IMPORTING ev_subject = DATA(subject)
ev_body_html = DATA(body_html) ).
TRY.
" create Sender & Receiver
DATA(lo_sender) = cl_cam_address_bcs=>create_internet_address( i_address_string = 'noreply@test.de'
i_address_name = 'Test' ).
DATA(lo_recipient) = cl_cam_address_bcs=>create_internet_address( i_address_string = 'receiver@test.de').
" create mail document
DATA(lo_mail_document) = cl_document_bcs=>create_document( i_type = 'HTM'
i_subject = CONV #( subject )
i_text = cl_bcs_convert=>string_to_soli( body_html ) ).
" create Business Communication Service
DATA(lo_bcs) = cl_bcs=>create_persistent( ).
lo_bcs->set_document( lo_mail_document ).
lo_bcs->set_sender( lo_sender ).
lo_bcs->add_recipient( lo_recipient ).
lo_bcs->send( ).
COMMIT WORK.
CATCH cx_smtg_email_common INTO DATA(ls_cx).
DATA(lv_message) = ls_cx->get_text( ).
MESSAGE s899(id) WITH 'Unable to send message:'(004) lv_message.
ENDTRY.