Wednesday, July 19, 2017

Send email with high importance from ABAP

There is an easy to grasp function module EFG_GEN_SEND_EMAIL, which is a kind of wrapper over CL_BCS class. Unfortunately EFG_GEN_SEND_EMAIL does not support email high priority sign (nice red exclamation mark within Outlook) in any input parameter.

I have a made a custom clone of this FM supporting high importance sign. I converted the coding into static method of a custom repository class. Please, no flame war for usage of static method ;-), there is no need to have an instance.


Original call of FM:


    CALL FUNCTION 'EFG_GEN_SEND_EMAIL'
      EXPORTING
        i_title                = iv_title
        i_sender               = iv_sender
        i_recipient            = ''
        i_flg_send_immediately = 'X'
      TABLES
        i_tab_lines            = t_mail_text
        i_tab_recipients       = t_recipients
      EXCEPTIONS
        not_qualified          = 1
        failed                 = 2
        OTHERS                 = 3.
    IF sy-subrc NE 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.


Call of custom method (almost the same):


    TRY.
        CALL METHOD /yournamespace/cl_email=>send_email
          EXPORTING
            iv_title          = iv_title
            iv_sender         = iv_sender
            iv_high_priority  = abap_true
            iv_recipient      = ''
          CHANGING
            ct_tab_lines      = t_mail_text
            ct_tab_recipients = t_recipients.
      CATCH cx_root.
        MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDTRY.



Description of the custom repository class 


There is one static method with following input parameters:



The complete method source code:


METHOD send_email.

  DATA: lt_recipients     TYPE STANDARD TABLE OF string,
        lv_string         TYPE string,
        lo_ref_send_req   TYPE REF TO cl_bcs,
        lt_text           TYPE bcsy_text,
        lo_ref_doc        TYPE REF TO cl_document_bcs,
        lo_ref_recipient  TYPE REF TO if_recipient_bcs,
        lo_ref_sender     TYPE REF TO if_sender_bcs,
        lx_rex_bcs        TYPE REF TO cx_bcs,
        lx_rex_addr       TYPE REF TO cx_address_bcs,
        lv_smtp_addr      TYPE adr6-smtp_addr,
        lv_subject        TYPE so_obj_des,
        lt_soli           TYPE soli_tab,
        ls_soli           TYPE soli,
        lv_uname          TYPE uname,
        lv_importance     TYPE bcs_docimp.

  IF iv_sender IS INITIAL.
    RAISE EXCEPTION TYPE cx_bcs
      EXPORTING
        msgty = 'E'
        msgid = '00'
        msgno = 007
        msgv1 = 'Sender'.
  ENDIF.

  IF iv_title IS INITIAL.
    RAISE EXCEPTION TYPE cx_bcs
      EXPORTING
        msgty = 'E'
        msgid = '00'
        msgno = 007
        msgv1 = 'Title'.
  ENDIF.

  IF ct_tab_lines IS INITIAL.
    RAISE EXCEPTION TYPE cx_bcs
      EXPORTING
        msgty = 'E'
        msgid = '00'
        msgno = 007
        msgv1 = 'Content'.
  ENDIF.

  IF ( ct_tab_recipients[] IS INITIAL ) AND ( iv_recipient IS INITIAL ).
    RAISE EXCEPTION TYPE cx_bcs
      EXPORTING
        msgty = 'E'
        msgid = '00'
        msgno = 007
        msgv1 = 'Recipient'.
  ENDIF.

  IF NOT iv_sender CS '@'.
    cv_flg_sender_is_uname = 'X'.
  ENDIF.

  lt_recipients = ct_tab_recipients[].
  APPEND iv_recipient TO lt_recipients.

  DELETE lt_recipients
    WHERE table_line IS INITIAL.

  SORT lt_recipients BY table_line.
  DELETE ADJACENT DUPLICATES FROM lt_recipients.

  TRY.
      lo_ref_send_req = cl_bcs=>create_persistent( ).

*     sender
      TRY.
          IF cv_flg_sender_is_uname IS INITIAL.

            lv_smtp_addr       = iv_sender.
            lo_ref_sender      = cl_cam_address_bcs=>create_internet_address(
              i_address_string = lv_smtp_addr
              i_address_name   = lv_smtp_addr
            ).
          ELSE.
            lv_uname = iv_sender.
            lo_ref_sender = cl_sapuser_bcs=>create( i_user =  lv_uname ).
          ENDIF.

        CATCH cx_address_bcs INTO lx_rex_addr.
          RAISE EXCEPTION lx_rex_addr.
      ENDTRY.

      lo_ref_send_req->set_sender( lo_ref_sender ).

*      recipient (e-mail address)
      LOOP AT lt_recipients INTO lv_smtp_addr.
        lo_ref_recipient = cl_cam_address_bcs=>create_internet_address( lv_smtp_addr ).
*       add recipient with its respective attributes to send request
        lo_ref_send_req->add_recipient( lo_ref_recipient ).
      ENDLOOP.

*     document
      APPEND LINES OF ct_tab_lines TO lt_soli.
      lv_subject  = iv_title.

      IF iv_high_priority EQ abap_true.
        lv_importance = '1'.
      ENDIF.

      lo_ref_doc = cl_document_bcs=>create_document(
        i_type       = 'RAW'
        i_text       = lt_soli
        i_length     = '100'
        i_subject    = lv_subject
        i_importance = lv_importance
      ).

*     add document to send request
      lo_ref_send_req->set_document( lo_ref_doc ).

      IF NOT iv_flg_send_immediately IS INITIAL.
        lo_ref_send_req->set_send_immediately( 'X' ).
      ENDIF.

      lo_ref_send_req->set_status_attributes( 'N' ).
      lv_string = iv_title.
      lo_ref_send_req->set_message_subject( lv_string ).
      lo_ref_send_req->send( ).

      IF NOT iv_flg_commit IS INITIAL.
        TRY.
            COMMIT WORK.
          CATCH cx_root.
        ENDTRY.
      ENDIF.

    CATCH cx_bcs INTO lx_rex_bcs.
      RAISE EXCEPTION lx_rex_bcs.
    CATCH cx_root.
  ENDTRY.

ENDMETHOD.



The usage is very easy, you have to always provide some email body, title - subject, sender and recipient. You can do that as follows within some arbitrary class. Play with it a you want..



CLASS lcl_ctrl_email DEFINITION.

  PUBLIC SECTION.
    METHODS:
      send_email
        IMPORTING
          iv_title           TYPE clike
          iv_sender          TYPE clike.

  PRIVATE SECTION.
    DATA: t_mail_text        TYPE soli_tab,
          t_recipients       TYPE STANDARD TABLE OF ad_smtpadr.

    METHODS:

      generate_content,
      get_recipients.

ENDCLASS.                    "lcl_ctrl_email DEFINITION


CLASS lcl_ctrl_email IMPLEMENTATION.
  METHOD generate_content.

    DATA: ls_mail_text TYPE soli.

    ls_mail_text = 'Hello,'.
    APPEND ls_mail_text TO t_mail_text.

    ls_mail_text = ''.
    APPEND ls_mail_text TO t_mail_text.

    ls_mail_text = 'this is an email content example...'.
    APPEND ls_mail_text TO t_mail_text.

    ls_mail_text = ''.
    APPEND ls_mail_text TO t_mail_text.

    ls_mail_text = 'Regards,'.
    APPEND ls_mail_text TO t_mail_text.

    ls_mail_text = 'Your team'.
    APPEND ls_mail_text TO t_mail_text.

  ENDMETHOD.                    "generate_content.

  METHOD get_recipients.

    SELECT email FROM /yournamespace/email_addr
      INTO TABLE t_recipients.

  ENDMETHOD.                    "get_recipients

  METHOD send_email.

    generate_content( ).
    get_recipients( ).

    TRY.
        CALL METHOD /yournamespace/cl_email=>send_email
          EXPORTING
            iv_title          = iv_title
            iv_sender         = iv_sender
            iv_high_priority  = abap_true
            iv_recipient      = ''
          CHANGING
            ct_tab_lines      = t_mail_text
            ct_tab_recipients = t_recipients.
      CATCH cx_root.
        MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDTRY.

  ENDMETHOD.                    "send_email

ENDCLASS.                    "lcl_ctrl_email IMPLEMENTATION



Source:  https://archive.sap.com/discussions/thread/1953259