E-Mail Templates, einfaches Beispiel

Bei der Definition des E-Mail Templates wurde eine CDS View hinterlegt und innerhalb des Mail Textes auf die Spalten zugegriffen. Die Datenbeschaffung selbst erfolgt im einfachsten Fall ebenfalls über die CDS View.

Zunächst werden die erforderlichen BCS Objekte und die E-Mail API instantiiert

    DATA(lo_bcs) = cl_bcs=>create_persistent( ). " Create instance of BCS class

    " create instance of eMail API, provide name of Mail template
    DATA(lo_email_api) = cl_smtg_email_api=>get_instance( iv_template_id = 'ZMAIL_TEMPLATE1'  ).

Anschließend wird das E-Mail Dokument mittels Methode render_bcs vorbereitet. Hierbei werden auch die Schlüsselfelder der CDS View mit Werten versorgt. Die Methode kümmert sich um die Datenbeschaffung und das Ersetzen der Platzhalter.

  " Build key for CDS view
     DATA(lt_cds_key) = VALUE if_smtg_email_template=>ty_gt_data_key( ( name = 'prueflos' value = '000000758871' ) ).

  " Render E-Mail for CL_BCS class. This replaces all placeholders with real data
     lo_email_api->render_bcs( io_bcs = lo_bcs iv_language = sy-langu it_data_key = lt_cds_key ).

Nun werden noch der Empfänger

          lr_recipient =
             cl_cam_address_bcs=>create_internet_address(
                                 'XY@Z.com' ).
          IF lr_recipient IS BOUND.
            ls_recip-recipient = lr_recipient.
            lo_bcs->add_recipient( EXPORTING i_recipient = lr_recipient ).
          ENDIF.

Sowie der Absender definiert

      " Define sender
      DATA(lo_sender) = cl_sapuser_bcs=>create( sy-uname ).
      lo_bcs->set_sender( i_sender = lo_sender ).

Und schon kann die Mail versendet werden

      " Send Email
      lo_bcs->send( ).

      IF sy-subrc IS INITIAL.
        COMMIT WORK. " Commit to really send the message
        FREE lo_bcs.
      ENDIF.

Vollständiges Programm

*&---------------------------------------------------------------------*
*& Report z_mail_template2
*&---------------------------------------------------------------------*
*& Send E-Mail based on Template by using data from CDS View
*&
*& E-Mail Template must be defined with relation to an existing CDS view.
*&---------------------------------------------------------------------*
REPORT z_mail_template2.



DATA: gv_subrc_send_email TYPE sy-subrc.

DATA: ls_recip          TYPE bcss_re3,
      lr_recipient      TYPE REF TO if_recipient_bcs,
      lo_email_document TYPE REF TO cl_document_bcs.


TRY.
    DATA(lo_bcs) = cl_bcs=>create_persistent( ). " Create instance of BCS class

    " create instance of eMail API, provide name of Mail template
    DATA(lo_email_api) = cl_smtg_email_api=>get_instance( iv_template_id = 'ZMAIL_TEMPLATE1'  ).

     " Build key for CDS view
     DATA(lt_cds_key) = VALUE if_smtg_email_template=>ty_gt_data_key( ( name = 'prueflos' value = '000000758871' ) ).
    TRY.

        " Render E-Mail for CL_BCS class. This replaces all placeholders with real data
        lo_email_api->render_bcs( io_bcs = lo_bcs iv_language = sy-langu it_data_key = lt_cds_key ).

      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.
        FREE lo_bcs.
        gv_subrc_send_email = 1.
    ENDTRY.

    IF gv_subrc_send_email = 0.

      TRY.
          " Define recipient
          CLEAR: lr_recipient.
          lr_recipient =
             cl_cam_address_bcs=>create_internet_address(
                                 'XY@Z.com' ).
          IF lr_recipient IS BOUND.
            ls_recip-recipient = lr_recipient.
            lo_bcs->add_recipient( EXPORTING i_recipient = lr_recipient ).
          ENDIF.
        CATCH cx_address_bcs INTO DATA(lr_bcs_exception).

      ENDTRY.

      " Define sender
      DATA(lo_sender) = cl_sapuser_bcs=>create( sy-uname ).
      lo_bcs->set_sender( i_sender = lo_sender ).

      " Send Email
      lo_bcs->send( ).

      IF sy-subrc IS INITIAL.
        COMMIT WORK. " Commit to really send the message
        FREE lo_bcs.
      ENDIF.

    ELSEIF gv_subrc_send_email = 4.
      MESSAGE s167(qm).
    ENDIF.

  CATCH cx_send_req_bcs INTO DATA(lr_send_req_bcs_excp).
*     possible error types of CX_SEND_REQ_BCS
*    (without those inherited from cx_bcs):
*    ALLREADY_RELEASED   RECIPIENT_EXISTS  DOCUMENT_NOT_EXISTS
*    DOCUMENT_NOT_SENT   NOT_SUBMITED      FOREIGN_LOCK
*    TOO_MANY_RECEIVERS  NO_INSTANCE       CANCELLED
*    NOT_RELEASED        MIME_PROBLEMS     NO_RECIPIENTS
*    SENDER_PROBLEMS


    DATA(lv_diagnosis) = lr_send_req_bcs_excp->get_text( ).
    MESSAGE s899(id) WITH
    'Unable to send message:'(004) lv_diagnosis.
    FREE lo_bcs.

*   possible bcs errors
  CATCH cx_bcs INTO DATA(lr_bcs).
*     possible error types of cx_bcs:
*     INTERNAL_ERROR     OS_EXCEPTION     X_ERROR
*     PARAMETER_ERROR    INVALID_VALUE    CREATION_FAILED
*     NO_AUTHORIZATION   LOCKED           CAST_ERROR

    lv_diagnosis = lr_bcs->get_text( ).
    MESSAGE s899(id) WITH
    'Unable to send message:'(004) lv_diagnosis.
    FREE lo_bcs.

  CATCH cx_smtg_email_common INTO DATA(lr_common).
    lv_diagnosis = lr_common->get_text( ).
    MESSAGE s899(id) WITH 'Unable to send message:'(004) lv_diagnosis .

  CATCH cx_os_object_not_found INTO DATA(lr_object_not_found).
    lv_diagnosis = lr_object_not_found->get_text( ).

ENDTRY.