Difference between revisions of "SAP ABAP EMAIL"

From SapWiki
Line 1: Line 1:
 
  <nowiki>r_subrc = zcl_hcm_class=>send_mail( i_correouser = lt_correos[]
 
  <nowiki>r_subrc = zcl_hcm_class=>send_mail( i_correouser = lt_correos[]
                                    i_text = lt_text[]
+
                                    i_text = lt_text[]
                                    i_subject = l_subject
+
                                    i_subject = l_subject
                                    i_sender = 'Remuneraciones@sapwiki.cl'
+
                                    i_sender = 'Remuneraciones@sapwiki.cl'
                                    i_copy = lt_copy
+
                                    i_copy = lt_copy
                                  ).
+
                                  ).
  
METHOD send_mail.
+
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
*I_SUBJECT    Importing Type SO_OBJ_DES
+
*I_SUBJECT    Importing Type SO_OBJ_DES
*I_MAILHEX    Importing Type SOLIX_TAB      op
+
*I_MAILHEX    Importing Type SOLIX_TAB      op
*I_ATTACH_NAME Importing Type STRING          op
+
*I_ATTACH_NAME Importing Type STRING          op
*I_SENDER      Importing Type AD_SMTPADR      op
+
*I_SENDER      Importing Type AD_SMTPADR      op
*I_COPY        Importing Type BCSY_SMTPA      op
+
*I_COPY        Importing Type BCSY_SMTPA      op
*R_SUBRC      Returning Type SYST_SUBRC
+
*R_SUBRC      Returning Type SYST_SUBRC
**********************************************************************
+
**********************************************************************
    DATA: send_request  TYPE REF TO cl_bcs,
+
    DATA: send_request  TYPE REF TO cl_bcs,
          text          TYPE bcsy_text,
+
          text          TYPE bcsy_text,
          binary_content TYPE solix_tab,
+
          binary_content TYPE solix_tab,
          document      TYPE REF TO cl_document_bcs,
+
          document      TYPE REF TO cl_document_bcs,
          sender        TYPE REF TO cl_sapuser_bcs,
+
          sender        TYPE REF TO cl_sapuser_bcs,
          recipient      TYPE REF TO if_recipient_bcs,
+
          recipient      TYPE REF TO if_recipient_bcs,
          bcs_exception  TYPE REF TO cx_bcs,
+
          bcs_exception  TYPE REF TO cx_bcs,
          sent_to_all    TYPE os_boolean,
+
          sent_to_all    TYPE os_boolean,
          document_data  TYPE sofolenti1,
+
          document_data  TYPE sofolenti1,
          doc_id        TYPE sofolenti1-doc_id,
+
          doc_id        TYPE sofolenti1-doc_id,
          cont_text      TYPE soli_tab,
+
          cont_text      TYPE soli_tab,
          attachment    TYPE REF TO if_document_bcs,
+
          attachment    TYPE REF TO if_document_bcs,
          lv_attach_name TYPE sood-objdes,
+
          lv_attach_name TYPE sood-objdes,
          l_sender      TYPE REF TO if_sender_bcs,
+
          l_sender      TYPE REF TO if_sender_bcs,
          l_message      TYPE string,
+
          l_message      TYPE string,
          l_correouser  TYPE ad_smtpadr,
+
          l_correouser  TYPE ad_smtpadr,
          l_address_name TYPE ad_smtpadr.
+
          l_address_name TYPE ad_smtpadr.
    DATA: ld_addr        TYPE adr6-smtp_addr,
+
    DATA: ld_addr        TYPE adr6-smtp_addr,
          internet_sender TYPE REF TO if_sender_bcs.
+
          internet_sender TYPE REF TO if_sender_bcs.
+
 
    text[] = i_text[].
+
    text[] = i_text[].
+
 
    TRY.
+
    TRY.
*    -------- create persistent send request ------------------------
+
*    -------- create persistent send request ------------------------
        send_request = cl_bcs=>create_persistent( ).
+
        send_request = cl_bcs=>create_persistent( ).
+
 
*    -------- create and set document with attachment ---------------
+
*    -------- create and set document with attachment ---------------
*    create document from internal table with text
+
*    create document from internal table with text
+
 
+
 
        document = cl_document_bcs=>create_document(
+
        document = cl_document_bcs=>create_document(
                      i_type    = 'HTM'
+
                      i_type    = 'HTM'
                      i_text    = i_text
+
                      i_text    = i_text
*                    i_length  = '12'
+
*                    i_length  = '12'
                      i_subject = i_subject ).
+
                      i_subject = i_subject ).
+
 
* add attachment
+
* add attachment
        IF i_attach_name <> space.
+
        IF i_attach_name <> space.
          lv_attach_name = i_attach_name.
+
          lv_attach_name = i_attach_name.
          CALL METHOD document->add_attachment
+
          CALL METHOD document->add_attachment
            EXPORTING
+
            EXPORTING
              i_attachment_type    = 'BIN'
+
              i_attachment_type    = 'BIN'
              i_attachment_subject = lv_attach_name
+
              i_attachment_subject = lv_attach_name
              i_att_content_hex    = i_mailhex.
+
              i_att_content_hex    = i_mailhex.
        ENDIF.
+
        ENDIF.
+
 
*    add document to send request
+
*    add document to send request
        CALL METHOD send_request->set_document( document ).
+
        CALL METHOD send_request->set_document( document ).
+
 
*    --------- add recipient (e-mail address) -----------------------
+
*    --------- add recipient (e-mail address) -----------------------
*    create recipient
+
*    create recipient
        LOOP AT i_correouser INTO l_correouser.
+
        LOOP AT i_correouser INTO l_correouser.
          l_address_name = l_correouser.
+
          l_address_name = l_correouser.
          recipient = cl_cam_address_bcs=>create_internet_address(
+
          recipient = cl_cam_address_bcs=>create_internet_address(
                                            l_address_name ).
+
                                            l_address_name ).
+
 
*    add recipient with its respective attributes to send request
+
*    add recipient with its respective attributes to send request
          CALL METHOD send_request->add_recipient
+
          CALL METHOD send_request->add_recipient
            EXPORTING
+
            EXPORTING
              i_recipient = recipient
+
              i_recipient = recipient
              i_express  = 'X'.
+
              i_express  = 'X'.
        ENDLOOP.
+
        ENDLOOP.
+
 
        LOOP AT i_copy INTO l_correouser.
+
        LOOP AT i_copy INTO l_correouser.
          l_address_name = l_correouser.
+
          l_address_name = l_correouser.
          recipient = cl_cam_address_bcs=>create_internet_address(
+
          recipient = cl_cam_address_bcs=>create_internet_address(
                                            l_address_name ).
+
                                            l_address_name ).
+
 
*    add recipient with its respective attributes to send request
+
*    add recipient with its respective attributes to send request
          CALL METHOD send_request->add_recipient
+
          CALL METHOD send_request->add_recipient
            EXPORTING
+
            EXPORTING
              i_recipient = recipient
+
              i_recipient = recipient
              i_express  = 'X'
+
              i_express  = 'X'
              i_copy      = abap_true.
+
              i_copy      = abap_true.
        ENDLOOP.
+
        ENDLOOP.
+
 
** Crear 'Enviado por:"
+
** Crear 'Enviado por:"
*  IF p_from IS NOT INITIAL.
+
*  IF p_from IS NOT INITIAL.
*        CALL METHOD cl_cam_address_bcs=>create_internet_address
+
*        CALL METHOD cl_cam_address_bcs=>create_internet_address
*      EXPORTING
+
*      EXPORTING
*        i_address_string = p_from
+
*        i_address_string = p_from
*        i_address_name  = p_from_name
+
*        i_address_name  = p_from_name
*      RECEIVING
+
*      RECEIVING
*        result          = l_sender.
+
*        result          = l_sender.
*
+
*
*    send_request->set_sender( l_sender ).
+
*    send_request->set_sender( l_sender ).
*    CLEAR l_sender.
+
*    CLEAR l_sender.
*  ENDIF.
+
*  ENDIF.
+
 
*    --------- set sender -------------------------------------------
+
*    --------- set sender -------------------------------------------
*    note: this is necessary only if you want to set the sender
+
*    note: this is necessary only if you want to set the sender
*          different from actual user (SY-UNAME). Otherwise sender is
+
*          different from actual user (SY-UNAME). Otherwise sender is
*          set automatically with actual user.
+
*          set automatically with actual user.
+
 
        IF i_sender IS NOT INITIAL.
+
        IF i_sender IS NOT INITIAL.
          ld_addr = i_sender. "'Remuneraciones@sapwiki.cl'.
+
          ld_addr = i_sender. "'Remuneraciones@sapwiki.cl'.
          internet_sender = cl_cam_address_bcs=>create_internet_address(
+
          internet_sender = cl_cam_address_bcs=>create_internet_address(
                                                  i_address_string = ld_addr  ).
+
                                                i_address_string = ld_addr  ).
+
 
          CALL METHOD send_request->set_sender
+
          CALL METHOD send_request->set_sender
            EXPORTING
+
            EXPORTING
              i_sender = internet_sender.
+
              i_sender = internet_sender.
        ENDIF.
+
        ENDIF.
*    ---------- send document ---------------------------------------
+
*    ---------- send document ---------------------------------------
        send_request->send_request->set_link_to_outbox( 'X' ).
+
        send_request->send_request->set_link_to_outbox( 'X' ).
+
 
        CALL METHOD send_request->send(
+
        CALL METHOD send_request->send(
          EXPORTING
+
          EXPORTING
            i_with_error_screen = 'X'
+
            i_with_error_screen = 'X'
          RECEIVING
+
          RECEIVING
            result              = sent_to_all ).
+
            result              = sent_to_all ).
        IF sent_to_all = 'X'.
+
        IF sent_to_all = 'X'.
*        WRITE text-003.
+
*        WRITE text-003.
        ENDIF.
+
        ENDIF.
+
 
        COMMIT WORK.
+
        COMMIT WORK.
        r_subrc = 0.
+
        r_subrc = 0.
* -----------------------------------------------------------
+
* -----------------------------------------------------------
* *                    exception handling
+
* *                    exception handling
* -----------------------------------------------------------
+
* -----------------------------------------------------------
* * replace this very rudimentary exception handling
+
* * replace this very rudimentary exception handling
* * with your own one !!!
+
* * with your own one !!!
* -----------------------------------------------------------
+
* -----------------------------------------------------------
      CATCH cx_bcs INTO bcs_exception.
+
      CATCH cx_bcs INTO bcs_exception.
        r_subrc = 4.
+
        r_subrc = 4.
        EXIT.
+
        EXIT.
+
 
    ENDTRY.
+
    ENDTRY.
  ENDMETHOD.</nowiki>
+
  ENDMETHOD.</nowiki>

Revision as of 20:49, 4 April 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.