Pages

Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Friday, September 5, 2014

Sending an email attachment with more than 255 characters

Note: Sample code in program BCS_EXAMPLE_7
  1.  Put everything in a string 

      DATA: lv_string TYPE string, "temporary string
            lv_fkimg TYPE char17, "quantity
            lv_unit TYPE char14, "unit price
            lv_lines TYPE sy-tabix. "lines


      FIELD-SYMBOLS <lfs_alv> TYPE gy_inv_alv. "for my table

      CONSTANTS:lc_tab TYPE char2 

                      VALUE cl_abap_char_utilities=>horizontal_tab, "tab
                lc_cret TYPE char2 
                      VALUE cl_abap_char_utilities=>cr_lf. "new line

    *Get the number of records
      DESCRIBE TABLE pt_inv LINES lv_lines.


    *Get the data from internal table
      LOOP AT pt_inv ASSIGNING <lfs_alv>.
        CLEAR: lv_string,
               lv_fkimg,
               lv_unit,
               lv_gross.

    *Convert numeric to character
        MOVE <lfs_alv>-fkimg TO lv_fkimg. "quantity
        MOVE <lfs_alv>-unitprice TO lv_unit. "unit price


    *Put negative sign in front
        CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
          CHANGING
            value = lv_unit.

    *Save everything in a temporary string
        CONCATENATE <lfs_alv>-vkorg
                    <lfs_alv>-kunwe
                    <lfs_alv>-ship_name
                    <lfs_alv>-we_street
                    <lfs_alv>-we_street2
                    <lfs_alv>-we_city
                    <lfs_alv>-we_country
                    <lfs_alv>-we_postcode
                    <lfs_alv>-we_telnum
                    <lfs_alv>-matnr
                    <lfs_alv>-arktx
                    <lfs_alv>-fkdat
                    <lfs_alv>-vbeln
                    lv_fkimg
                    lv_unit
          INTO lv_string RESPECTING BLANKS.

    *Add the temporary string and new line to the final string
        IF sy-tabix NE lv_lines.
          CONCATENATE pv_string lv_string lc_cret INTO pv_string.
        ELSE.

    *For the last record, don't add new line
          CONCATENATE pv_string lv_string INTO pv_string.
        ENDIF.
      ENDLOOP.

  2. Convert string to binary table and attach to email

      DATA: lt_attach TYPE solix_tab, "binary table
            lv_subject TYPE so_obj_des, "subject
            send_request       TYPE REF TO cl_bcs,
            lt_text            TYPE bcsy_text, "body msg
            document           TYPE REF TO cl_document_bcs, "doc
            sender             TYPE REF TO cl_sapuser_bcs, "sender
            recipient          TYPE REF TO if_recipient_bcs, "recipient
            bcs_exception      TYPE REF TO cx_bcs,
            sent_to_all        TYPE os_boolean.

      FIELD-SYMBOLS <lfs_email> TYPE LINE OF gy_t_email. "for email recipients


    *Convert string to binary table
      TRY.

          cl_bcs_convert=>string_to_solix(
            EXPORTING
              iv_string   = pv_string "final string
              iv_codepage = '4103'  "suitable for MS Excel, leave empty
              iv_add_bom  = 'X'     "for other doc types
            IMPORTING
              et_solix  = lt_attach ).

        CATCH cx_bcs.

          MESSAGE e445(so).
      ENDTRY.

    *Create the email
      TRY.
    *Create persistent send request
        send_request = cl_bcs=>create_persistent( ).

    *Subject
        lv_subject = 'Weekly Report'.

    *Body of email
        APPEND 'Please find the attached weekly sales report'(090) TO lt_text.

    *Create Document
        document = cl_document_bcs=>create_document(
                        i_type    = 'RAW'
                        i_text    = lt_text
                        i_subject = lv_subject ).

    *Add attachment to document
        CALL METHOD document->add_attachment
          EXPORTING
            i_attachment_type    = 'XLS'
            i_attachment_subject = lv_subject
            i_att_content_hex    = lt_attach.

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

    *Set sender
        sender = cl_sapuser_bcs=>create( sy-uname ).
        CALL METHOD send_request->set_sender
          EXPORTING
            i_sender = sender.

    *Add recipient (e-mail address) from table
        LOOP AT pt_email ASSIGNING <lfs_email>.
          recipient = cl_cam_address_bcs=>create_internet_address(
                                            <lfs_email>-low ).

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

    *Send document
        CALL METHOD send_request->send(
          EXPORTING
            i_with_error_screen = 'X'
          RECEIVING
            result              = sent_to_all ).
        IF sent_to_all = 'X'.
          MESSAGE s000 WITH 'Mail has been Successfully Sent.'(085).
        ENDIF.

        COMMIT WORK.
      ENDTRY.