SAP/ABAP

[Mail] 엑셀첨부 메일전송(2/2)

성공보다는 가치를.. 2024. 3. 22. 14:31
728x90
파일첨부 후 메일전송

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_MAIL_SEND=>SEND_MAIL
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_SUBJECT                     TYPE        SO_OBJ_DES(optional)
* | [--->] IV_SENDER                      TYPE        AD_SMTPADR(optional)
* | [--->] IT_BODY                        TYPE        SOLI_TAB(optional)
* | [--->] IT_RECEIVER                    TYPE        PIQT_MAILADDR(optional)
* | [--->] IT_RECEIVER_CC                 TYPE        PIQT_MAILADDR(optional)
* | [--->] IO_DATA                        TYPE        TY_T_REF_DATA(optional)
* | [<---] EV_MESG                        TYPE        CHAR200
* | [<---] EV_STAT                        TYPE        ZEQR_STAT
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD SEND_MAIL.

    CHECK IV_SUBJECT  IS NOT INITIAL AND
          IV_SENDER   IS NOT INITIAL AND
          IT_RECEIVER IS NOT INITIAL.

    TRY.
        "__Create send request
        DATA(LO_SEND_REQUEST) = CL_BCS=>CREATE_PERSISTENT( ).

        "__Set up document object
        DATA(LO_DOCUMENT) = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
                              I_TYPE = 'RAW'
                              I_TEXT = IT_BODY
                              I_SUBJECT = IV_SUBJECT ).

        "//__ 첨부가 있을 경우
        LOOP AT IO_DATA INTO DATA(LO_DATA).
          CHECK LO_DATA-DATA IS BOUND.

          DATA(LV_XSTRING) = NEW ZCL_MAIL_SEND( )->ITAB_TO_XSTRING( IR_DATA_REF = LO_DATA-DATA ).
          DATA(LV_XLS_FILE) = |/tmp/Temp{ SY-TABIX }.xlsx|.

          OPEN DATASET LV_XLS_FILE FOR OUTPUT IN BINARY MODE.

          IF SY-SUBRC EQ 0.
            TRANSFER LV_XSTRING TO LV_XLS_FILE.
            CLOSE DATASET LV_XLS_FILE.
          ENDIF.

          "__Add attachment
          LO_DOCUMENT->ADD_ATTACHMENT(
              I_ATTACHMENT_TYPE    = 'xls'
              I_ATTACHMENT_SIZE    = CONV #( XSTRLEN( LV_XSTRING ) )
              I_ATTACHMENT_SUBJECT = LO_DATA-FNAME
              I_ATTACHMENT_HEADER  = VALUE #( ( LINE = |{ LO_DATA-FNAME }.XLSX | ) )
          I_ATT_CONTENT_HEX    = CL_BCS_CONVERT=>XSTRING_TO_SOLIX( LV_XSTRING ) ).

        ENDLOOP.

        "__Add document to send request
        LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).

        "__Set sender
        LO_SEND_REQUEST->SET_SENDER(
          CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
            I_ADDRESS_STRING = CONV #( IV_SENDER )    ) ).


        "__Set Recipient
        LOOP AT IT_RECEIVER INTO DATA(LS_RECEIVER).
          LO_SEND_REQUEST->ADD_RECIPIENT(
            I_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                            I_ADDRESS_STRING = CONV #( LS_RECEIVER )
                          )
            I_EXPRESS   = ABAP_TRUE ).
        ENDLOOP.

        "__Set Recipient :: CC
        LOOP AT IT_RECEIVER_CC INTO DATA(LS_RECEIVER_CC).
          LO_SEND_REQUEST->ADD_RECIPIENT(
            I_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                            I_ADDRESS_STRING = CONV #( LS_RECEIVER_CC )
                          )
            I_EXPRESS   = ABAP_TRUE
            I_COPY = ABAP_TRUE ).
        ENDLOOP.

        "__Send Email
        DATA(LV_SENT_MAIL) = LO_SEND_REQUEST->SEND( ).
        COMMIT WORK.

      CATCH CX_SEND_REQ_BCS INTO DATA(LX_REQ_BSC).
        "__Error handling
        EV_MESG = LX_REQ_BSC->GET_LONGTEXT( ).
        EV_STAT = 'E'.
      CATCH CX_DOCUMENT_BCS INTO DATA(LX_DOC_BCS).
        "__Error handling
        EV_MESG = LX_DOC_BCS->GET_LONGTEXT( ).
        EV_STAT = 'E'.
      CATCH CX_ADDRESS_BCS  INTO DATA(LX_ADD_BCS).
        "__Error handling
        EV_MESG = LX_ADD_BCS->GET_LONGTEXT( ).
        EV_STAT = 'E'.
    ENDTRY.

    EV_STAT = COND #( WHEN LV_SENT_MAIL IS NOT INITIAL THEN 'S' ELSE 'E' ).

  ENDMETHOD.

 

출처 : https://discoveringabap.com/2021/11/20/abap-code-samples-send-xslx-attachment-using-cl_bcs/

728x90