Difference between revisions of "SAP ABAP EMAIL"

From SapWiki
Line 1: Line 1:
  METHOD send_mail.
+
r_subrc = zcl_hcm_class=>send_mail( i_correouser = lt_correos[]
 +
                                    i_text = lt_text[]
 +
                                    i_subject = l_subject
 +
                                    i_sender = 'Remuneraciones@sapwiki.cl'
 +
                                    i_copy = lt_copy
 +
                                  ).
 +
 
 +
METHOD send_mail.
 
  *I_CORREOUSER  Importing Type BCSY_SMTPA
 
  *I_CORREOUSER  Importing Type BCSY_SMTPA
 
  *I_TEXT        Importing Type BCSY_TEXT
 
  *I_TEXT        Importing Type BCSY_TEXT

Revision as of 18:13, 26 March 2020

r_subrc = zcl_hcm_class=>send_mail( i_correouser = lt_correos[]
                                    i_text = lt_text[]
                                    i_subject = l_subject
                                    i_sender = 'Remuneraciones@sapwiki.cl'
                                    i_copy = lt_copy
                                  ).
METHOD send_mail.
*I_CORREOUSER  Importing Type BCSY_SMTPA
*I_TEXT        Importing Type BCSY_TEXT
*I_SUBJECT     Importing Type SO_OBJ_DES
*I_MAILHEX     Importing Type SOLIX_TAB       op
*I_ATTACH_NAME Importing Type STRING          op
*I_SENDER      Importing Type AD_SMTPADR      op
*I_COPY        Importing Type BCSY_SMTPA      op
*R_SUBRC       Returning Type SYST_SUBRC
**********************************************************************
    DATA: send_request   TYPE REF TO cl_bcs,
          text           TYPE bcsy_text,
          binary_content TYPE solix_tab,
          document       TYPE REF TO cl_document_bcs,
          sender         TYPE REF TO cl_sapuser_bcs,
          recipient      TYPE REF TO if_recipient_bcs,
          bcs_exception  TYPE REF TO cx_bcs,
          sent_to_all    TYPE os_boolean,
          document_data  TYPE sofolenti1,
          doc_id         TYPE sofolenti1-doc_id,
          cont_text      TYPE soli_tab,
          attachment     TYPE REF TO if_document_bcs,
          lv_attach_name TYPE sood-objdes,
          l_sender       TYPE REF TO if_sender_bcs,
          l_message      TYPE string,
          l_correouser   TYPE ad_smtpadr,
          l_address_name TYPE ad_smtpadr.
    DATA: ld_addr         TYPE adr6-smtp_addr,
          internet_sender TYPE REF TO if_sender_bcs.

    text[] = i_text[].

    TRY.
*     -------- create persistent send request ------------------------
        send_request = cl_bcs=>create_persistent( ).

*     -------- create and set document with attachment ---------------
*     create document from internal table with text


        document = cl_document_bcs=>create_document(
                      i_type    = 'HTM'
                      i_text    = i_text
*                    i_length  = '12'
                      i_subject = i_subject ).

* add attachment
        IF i_attach_name <> space.
          lv_attach_name = i_attach_name.
          CALL METHOD document->add_attachment
            EXPORTING
              i_attachment_type    = 'BIN'
              i_attachment_subject = lv_attach_name
              i_att_content_hex    = i_mailhex.
        ENDIF.

*     add document to send request
        CALL METHOD send_request->set_document( document ).

*     --------- add recipient (e-mail address) -----------------------
*     create recipient
        LOOP AT i_correouser INTO l_correouser.
          l_address_name = l_correouser.
          recipient = cl_cam_address_bcs=>create_internet_address(
                                            l_address_name ).

*     add recipient with its respective attributes to send request
          CALL METHOD send_request->add_recipient
            EXPORTING
              i_recipient = recipient
              i_express   = 'X'.
        ENDLOOP.

        LOOP AT i_copy INTO l_correouser.
          l_address_name = l_correouser.
          recipient = cl_cam_address_bcs=>create_internet_address(
                                            l_address_name ).

*     add recipient with its respective attributes to send request
          CALL METHOD send_request->add_recipient
            EXPORTING
              i_recipient = recipient
              i_express   = 'X'
              i_copy      = abap_true.
        ENDLOOP.

** Crear 'Enviado por:"
*  IF p_from IS NOT INITIAL.
*        CALL METHOD cl_cam_address_bcs=>create_internet_address
*      EXPORTING
*        i_address_string = p_from
*        i_address_name   = p_from_name
*      RECEIVING
*        result           = l_sender.
*
*    send_request->set_sender( l_sender ).
*    CLEAR l_sender.
*  ENDIF.

*     --------- set sender -------------------------------------------
*     note: this is necessary only if you want to set the sender
*           different from actual user (SY-UNAME). Otherwise sender is
*           set automatically with actual user.

        IF i_sender IS NOT INITIAL.
          ld_addr = i_sender. "'Remuneraciones@sapwiki.cl'.
          internet_sender = cl_cam_address_bcs=>create_internet_address(
                                                 i_address_string = ld_addr  ).

          CALL METHOD send_request->set_sender
            EXPORTING
              i_sender = internet_sender.
        ENDIF.
*     ---------- send document ---------------------------------------
        send_request->send_request->set_link_to_outbox( 'X' ).

        CALL METHOD send_request->send(
          EXPORTING
            i_with_error_screen = 'X'
          RECEIVING
            result              = sent_to_all ).
        IF sent_to_all = 'X'.
*        WRITE text-003.
        ENDIF.

        COMMIT WORK.
        r_subrc = 0.
* -----------------------------------------------------------
* *                     exception handling
* -----------------------------------------------------------
* * replace this very rudimentary exception handling
* * with your own one !!!
* -----------------------------------------------------------
      CATCH cx_bcs INTO bcs_exception.
        r_subrc = 4.
        EXIT.

    ENDTRY.
  ENDMETHOD.