Wednesday 9 October 2019

Send Mail

&---------------------------------------------------------------------*
& Form SENT_MAIL
&---------------------------------------------------------------------*
& Mail Sent incase of errors
&---------------------------------------------------------------------*
FORM sent_mail.
  DATA: lo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL,
        lo_document     TYPE REF TO cl_document_bcs VALUE IS INITIAL, "document object
        lo_sender       TYPE REF TO if_sender_bcs VALUE IS INITIAL, "sender
        lo_recipient    TYPE REF TO if_recipient_bcs VALUE IS INITIAL, "recipient
        lt_text         TYPE bcsy_text, "Table for body
        lt_lines        TYPE TABLE OF tline.

  CONSTANTS: lc_st   TYPE thead-tdid     VALUE 'ST',
             lc_en   TYPE thead-tdspras  VALUE 'EN',
             lc_name TYPE thead-tdname   VALUE 'ZAHLPLANAENDERUNG', "<=change it
             lc_text TYPE thead-tdobject VALUE 'TEXT'.

  lo_send_request = cl_bcs=>create_persistent( ).

  "Read the email body template from SO10 text
  CALL FUNCTION 'READ_TEXT'
    EXPORTING
      id                      = lc_st
      language                = lc_en
      name                    = lc_name
      object                  = lc_text
    TABLES
      lines                   = lt_lines
    EXCEPTIONS
      id                      = 1
      language                = 2
      name                    = 3
      not_found               = 4
      object                  = 5
      reference_check         = 6
      wrong_access_to_archive = 7
      OTHERS                  = 8.
  IF sy-subrc EQ 0.
    "Set email body
    LOOP AT lt_lines ASSIGNING FIELD-SYMBOL(<lfs_lines>).
      APPEND INITIAL LINE TO lt_text ASSIGNING FIELD-SYMBOL(<lfs_text>).
      <lfs_text>-line = <lfs_lines>-tdline.
    ENDLOOP.
  ENDIF.

  "create document
  lo_document = cl_document_bcs=>create_document( i_type = 'TXT' "Type of document - HTM, TXT etc
                                                  i_text =  lt_text "email body
                                                  i_subject = TEXT-016 ). "email subject
  "Pass the document to send request
  lo_send_request->set_document( lo_document ).

  "Set sender to send request
  TRY.
      lo_sender = cl_sapuser_bcs=>create( sy-uname ). "sender is the logged in user
      lo_send_request->set_sender( EXPORTING i_sender = lo_sender ).
    CATCH cx_address_bcs INTO DATA(lv_address_bcs).
  ENDTRY.

  "Set recipient
  lo_recipient = cl_cam_address_bcs=>create_internet_address( 'test@deloitte.com' ). "Recipient email
  TRY.
      lo_send_request->add_recipient( EXPORTING
                                        i_recipient = lo_recipient
                                        i_express = abap_true ).
    CATCH cx_send_req_bcs INTO DATA(lo_send_req_bcs).
  ENDTRY.

  "Send email
  TRY.
      CALL METHOD lo_send_request->set_send_immediately
        EXPORTING
          i_send_immediately = abap_true.

      lo_send_request->send( EXPORTING
                                i_with_error_screen = abap_true ).
      COMMIT WORK.

      IF sy-subrc = 0. "mail sent successfully
        WRITE :/ 'Mail sent successfully'.
      ENDIF.
    CATCH cx_send_req_bcs INTO lo_send_req_bcs.
  ENDTRY.
ENDFORM.

No comments:

Post a Comment

Report to find CDS view of Standard Table

A small change has been made to the original program ( SAP YARD Article ) so that it can also display the common CDS used by multiple table...