Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
7.3 kB
1
Indexable
Never
class ZCL_MAILHANDLER definition
  public
  final
  create public .

public section.

  class-methods SEND_MAIL
    importing
      !IP_MAILTITLE type TLINE
      !IP_MAILBODY type BCSY_TEXT optional
      !IP_SENDERADDRESS type COMM_ID_LONG
      !IP_RECEIVERADDRESS type Z_EMAIL
      !IP_ATT_CONTENT_HEX type SOLIX_TAB optional
      !IP_SIZE type SO_OBJ_LEN optional
    exporting
      !EP_SUCCESS type BOOLEAN .
  class-methods GET_TEXT
    importing
      !IP_PERNR type PERNR_D optional
      !IP_REINR type REINR optional
      !IP_BEGDA type BEGDA optional
      !IP_ENDDA type ENDDA optional
      !IP_ENAME type CHAR50 optional
      !IP_FILE_NAME type DOKU_OBJ
    exporting
      !EP_MAILBODY type TLINE_TAB
      !EP_MAILTITLE type TLINE .
protected section.
private section.
ENDCLASS.



CLASS ZCL_MAILHANDLER IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_MAILHANDLER=>GET_TEXT
* +-------------------------------------------------------------------------------------------------+
* | [--->] IP_PERNR                       TYPE        PERNR_D(optional)
* | [--->] IP_REINR                       TYPE        REINR(optional)
* | [--->] IP_BEGDA                       TYPE        BEGDA(optional)
* | [--->] IP_ENDDA                       TYPE        ENDDA(optional)
* | [--->] IP_ENAME                       TYPE        CHAR50(optional)
* | [--->] IP_FILE_NAME                   TYPE        DOKU_OBJ
* | [<---] EP_MAILBODY                    TYPE        TLINE_TAB
* | [<---] EP_MAILTITLE                   TYPE        TLINE
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_text.

    DATA lt_readtable TYPE TABLE OF tline.
    DATA ls_line LIKE LINE OF lt_readtable.

    DATA: lv_pernr TYPE pernr_d,
          lv_reinr TYPE reinr,
          lv_begda TYPE char10,
          lv_endda TYPE char10,
          lv_ename TYPE char50,
          lv_topic TYPE char50.

*ZPKA_TEKSTTESTOWY
    CALL FUNCTION 'DOCU_GET'
      EXPORTING
        id                     = 'DT'
        langu                  = sy-langu
        object                 = ip_file_name
        typ                    = 'E'
        version                = 0
      TABLES
        line                   = lt_readtable.


    " Usuwanie zer poprzedzajacych numer
    lv_pernr = ip_pernr.
    lv_reinr = ip_reinr.
    SHIFT lv_pernr LEFT DELETING LEADING '0'.
    SHIFT lv_reinr LEFT DELETING LEADING '0'.

    " Zmiana formatu wyswietlania daty
    CONCATENATE ip_begda+6(2) ip_begda+4(2) ip_begda(4) INTO lv_begda SEPARATED BY '.'.
    CONCATENATE ip_endda+6(2) ip_endda+4(2) ip_endda(4) INTO lv_endda SEPARATED BY '.'.

    LOOP AT lt_readtable INTO ls_line.

      " Zastapienie na zmienne
      REPLACE ALL OCCURRENCES OF '[ip_pernr]' IN ls_line-tdline WITH lv_pernr.
      REPLACE ALL OCCURRENCES OF '[ip_reinr]' IN ls_line-tdline WITH lv_reinr.
      REPLACE ALL OCCURRENCES OF '[ip_begda]' IN ls_line-tdline WITH lv_begda.
      REPLACE ALL OCCURRENCES OF '[ip_endda]' IN ls_line-tdline WITH lv_endda.
      REPLACE ALL OCCURRENCES OF '[ip_ename]' IN ls_line-tdline WITH ip_ename.

      " Pobranie tytulu jesli pierwsza iteracja
      IF sy-tabix = 1.
        lv_topic = ls_line-tdline.
        " Zapis do tabeli jesli nastepne iteracje
      ELSE.
        MODIFY lt_readtable FROM ls_line INDEX sy-tabix TRANSPORTING tdline.
      ENDIF.
      CLEAR ls_line.

    ENDLOOP.
    DELETE lt_readtable INDEX 1.

    ep_mailbody = lt_readtable.
    ep_mailtitle = lv_topic.

  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_MAILHANDLER=>SEND_MAIL
* +-------------------------------------------------------------------------------------------------+
* | [--->] IP_MAILTITLE                   TYPE        TLINE
* | [--->] IP_MAILBODY                    TYPE        BCSY_TEXT(optional)
* | [--->] IP_SENDERADDRESS               TYPE        COMM_ID_LONG
* | [--->] IP_RECEIVERADDRESS             TYPE        Z_EMAIL
* | [--->] IP_ATT_CONTENT_HEX             TYPE        SOLIX_TAB(optional)
* | [--->] IP_SIZE                        TYPE        SO_OBJ_LEN(optional)
* | [<---] EP_SUCCESS                     TYPE        BOOLEAN
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD send_mail.

    DATA:
      gv_sent_to_all   TYPE os_boolean,
      gv_email         TYPE adr6-smtp_addr,
      gv_subject       TYPE so_obj_des,
      gv_text          TYPE bcsy_text,
      gr_send_request  TYPE REF TO cl_bcs,
      gr_bcs_exception TYPE REF TO cx_bcs,
      gr_recipient     TYPE REF TO if_recipient_bcs,
      gr_sender        TYPE REF TO cl_cam_address_bcs,
      gr_document      TYPE REF TO cl_document_bcs,
      temp_receivers   LIKE LINE OF ip_receiveraddress,
      lx_document_bcs  TYPE REF TO cx_document_bcs.

    DATA ls_line TYPE tline.

    TRY.
        "Create send request
        gr_send_request = cl_bcs=>create_persistent( ).

        "Email FROM
        gr_sender    = cl_cam_address_bcs=>create_internet_address( ip_senderaddress ).
        "Add sender to send request
        CALL METHOD gr_send_request->set_sender
          EXPORTING
            i_sender = gr_sender.

        "Email TO
        LOOP AT ip_receiveraddress INTO temp_receivers.
          "gv_email = 'frodo.baggins@outlook.com'.
          gr_recipient = cl_cam_address_bcs=>create_internet_address( temp_receivers ).
          "Add recipient to send request
          CALL METHOD gr_send_request->add_recipient
            EXPORTING
              i_recipient = gr_recipient
              i_express   = 'X'.
        ENDLOOP.

        "Email TITLE
        gv_subject = ip_mailtitle.


        gr_document = cl_document_bcs=>create_document(
                        i_type    = 'RAW'
                        i_text    = ip_mailbody
                        i_length  = '12'
                        i_subject = gv_subject ).

    if ip_size > 0.
        TRY.
            gr_document->add_attachment(
              EXPORTING
                i_attachment_type = 'XLS'
                i_attachment_subject = 'Form'
                i_attachment_size    = ip_size
                i_att_content_hex = ip_att_content_hex ).
        ENDTRY.
     ENDIF.

        gr_send_request->set_document( gr_document ).
        CALL METHOD gr_send_request->set_document( gr_document ).

        "Send email
        CALL METHOD gr_send_request->send(
          EXPORTING
            i_with_error_screen = 'X'
          RECEIVING
            result              = gv_sent_to_all ).
        IF gv_sent_to_all = 'X'.
          ep_success = abap_true.
        ELSE.
          ep_success = abap_false.
        ENDIF.

        "Commit to send email
        COMMIT WORK.

        "Exception handling
      CATCH cx_bcs INTO gr_bcs_exception.
        WRITE:
          'Error!',
          'Error type:',
          gr_bcs_exception->error_type.
    ENDTRY.

  ENDMETHOD.
ENDCLASS.