Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[ABAP] Read E-Mail-Template

Addition to https://nocin.eu/abap-e-mail-templates-in-s-4hana/

I had to create an E-Mail template where I did not need a CDS view or any variable at all. I simply needed the defined mail template without any rendering of class cl_smtg_email_api.
Solution: Reading the plain mail template can be achieved using class cl_smtg_email_template and method get_tmpl_cont:

DATA(ls_mail_content) = cl_smtg_email_template=>get( p_mailtx )->get_tmpl_cont( iv_langu = 'D' ).

Full sample:

 PARAMETERS: p_mailtx TYPE smtg_tmpl_id OBLIGATORY.
   
 TRY.
        " get mail template
        DATA(ls_mail_content) = cl_smtg_email_template=>get( p_mailtx )->get_tmpl_cont( iv_langu = 'D' ).

        " optional: dynamic manipulation of html email body

        " create mail document
        DATA(lo_mail_document) = cl_document_bcs=>create_document( i_type    = 'HTM'
                                                                   i_subject = conv #( ls_mail_content-subject )
                                                                   i_text    = cl_bcs_convert=>string_to_soli( ls_mail_content-body_html ) ).

        " create Sender & Receiver
        DATA(lo_sender)    = cl_cam_address_bcs=>create_internet_address( i_address_string = 'noreply@example.com'
                                                                          i_address_name   = 'Test' ).

        DATA(lo_recipient) = cl_cam_address_bcs=>create_internet_address( i_address_string = 'max.mustermann@example.com').

        " 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 e899(id) WITH 'Unable to send message:'(004) lv_message.
    ENDTRY.