Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

One of the Email attachment content is blank

vinay_pasalkar
Participant
0 Kudos

Hi Experts,

I am using below code to add multiple attachments in an Email. I checked the values of table in debugging mode, the tables are getting populated.

Email was sent However problem is I am getting only header fields in Attachement 1.

I have marked the part in bold where the first attachment is being added.

FORM f_email_body .
  CONSTANTS:
     lc_tab  TYPE c VALUE ',',
     lc_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf,
     lc_cx   TYPE c VALUE 'X'.
*General Data
  DATA: lt_binary_content1 TYPE solix_tab,
        lt_binary_content2 TYPE solix_tab,
        lv_conlengths      TYPE so_obj_len,
        lv_result_content  TYPE string,
        lwa_string1        TYPE string,
        lwa_string2        TYPE string.
* Ref. Class Declaration
  DATA: o_send_request  TYPE REF TO cl_bcs,
        lt_text         TYPE bcsy_text,
        o_document      TYPE REF TO cl_document_bcs,
        lv_sender       TYPE REF TO if_sender_bcs,
        o_bcs_exception TYPE REF TO cx_bcs,
        lv_sent_to_all  TYPE os_boolean.
*Email Details
  DATA: lwa_rec         TYPE ad_smtpadr ,
        lt_contents1    TYPE soli_tab,
        lt_contents2    TYPE soli_tab,
        lv_subject      TYPE so_obj_des,
        lv_time         TYPE string,
        lv_result       TYPE sy-binpt.
* ZIP File Details
  DATA: lv_zipfilehex TYPE xstring,
        lv_read1      TYPE numc4,
        lv_write1     TYPE numc4,
        lv_sub_att1   TYPE so_obj_des,
        lv_sub_att2   TYPE so_obj_des.
* Local variable declaration
  DATA : lv_flag1 TYPE c,
         lv_flag2 TYPE c.

*-- Local data declaration for sending mail
  DATA:
        o_recipient      TYPE REF TO if_recipient_bcs,
        lt_email         TYPE TABLE OF zzemail,
        lwa_email        TYPE zzemail,
        lv_size1         TYPE so_obj_len,
        lv_size2         TYPE so_obj_len.
* COnstants for Email functionality
  CONSTANTS:

        lc_sys1   TYPE sysysid    VALUE 'ECD',
        lc_ce     TYPE c          VALUE 'E',
        lc_id(4)  TYPE c          VALUE 'I088',
        lc_at_typ TYPE so_obj_tp  VALUE 'CSV',
        lc_type   TYPE so_obj_tp  VALUE 'HTM',
        lc_sys2   TYPE sysysid    VALUE 'ECT'.

  IF sy-sysid = lc_sys1 OR sy-sysid = lc_sys2.
    CONCATENATE text-002 '' INTO  lv_subject .
  ELSE.
    CONCATENATE text-003 '' INTO  lv_subject .
  ENDIF.
  REFRESH lt_binary_content1[] .
  REFRESH lt_binary_content2[] .
* FM to convert string to binary
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer          = lv_zipfilehex
      append_to_table = lc_cx
    TABLES
      binary_tab      = lt_binary_content1.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer          = lv_zipfilehex
      append_to_table = lc_cx
    TABLES
      binary_tab      = lt_binary_content2.

*refresh text .
  CLEAR: lv_result_content.

*********************************************************************************
**The body message
*********************************************************************************
  CONCATENATE text-018 '' INTO lv_result_content.

  lv_conlengths = strlen( lv_result_content ).


  CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
    EXPORTING
      text      = lv_result_content
    TABLES
      ftext_tab = lt_text.
  CLEAR:lv_result_content.

  IF sy-sysid = lc_sys1 OR sy-sysid = lc_sys2.
    CONCATENATE text-019 '' INTO lv_sub_att1.
    CONCATENATE text-020 '' INTO lv_sub_att2.
  ELSE.
    CONCATENATE text-021 '' INTO lv_sub_att1.
    CONCATENATE text-022 '' INTO lv_sub_att2.
  ENDIF.

  TRY.
      CLEAR o_send_request .

      o_send_request = cl_bcs=>create_persistent( ).

      CLEAR o_document .
      o_document = cl_document_bcs=>create_document(
      i_type    = lc_type
      i_text    = lt_text
      i_length  = lv_conlengths
      i_subject = lv_subject ).

* Prepare the First attachment for Name Match
      IF gt_name_match IS NOT INITIAL.
        LOOP AT gt_name_match INTO gwa_name_match.
*   check if header is filled or not.
          IF lv_flag1 IS INITIAL.
            lv_flag1 = lc_cx.
            CONCATENATE text-004 text-005 text-006 text-007
                        text-008 text-009 text-010 text-011
                        INTO lwa_string1 SEPARATED BY lc_tab.
            CLEAR: lv_size1.
            lv_size1 = lv_size1 + strlen( lwa_string1 ).
            APPEND lwa_string1 TO lt_contents1.
            CLEAR : lwa_string1.
          ENDIF.
*   Assign System according to the Internal Order
          CONCATENATE   gwa_name_match-date
                        gwa_name_match-time
                        gwa_name_match-lifnr
                        gwa_name_match-name1_sap
                        gwa_name_match-name2_sap
                        gwa_name_match-f_name_ofac
                        gwa_name_match-l_name_ofac
                        gwa_name_match-uid1
                       INTO lwa_string1 SEPARATED BY lc_tab .
          CONCATENATE lc_cret lwa_string1 INTO lwa_string1.

          lv_size1 = lv_size1 + strlen( lwa_string1 ).
          APPEND lwa_string1 TO lt_contents1.
          CLEAR : gwa_name_match, lwa_string1.
        ENDLOOP.
        CLEAR lwa_string1.
      ENDIF.

* Prepare the second attachment for Address match
      IF gt_addr_match IS NOT INITIAL.
        LOOP AT gt_addr_match INTO gwa_addr_match.
*     check if header is filled or not.
          IF lv_flag2 IS INITIAL.
            lv_flag2 = lc_cx.
            CONCATENATE text-004 text-005 text-006 text-007
                        text-008 text-009 text-010 text-011
                        text-012 text-013 text-014 text-015
                        text-016 text-017
                        INTO lwa_string2 SEPARATED BY lc_tab.
            CLEAR: lv_size2.
            lv_size2 = lv_size2 + strlen( lwa_string2 ).
            APPEND lwa_string2 TO lt_contents2.
            CLEAR : lwa_string2.
          ENDIF.
*     Assign System according to the Internal Order
          CONCATENATE   gwa_addr_match-date
                        gwa_addr_match-time
                        gwa_addr_match-lifnr
                        gwa_addr_match-name1_sap
                        gwa_addr_match-name2_sap
                        gwa_name_match-f_name_ofac
                        gwa_addr_match-l_name_ofac
                        gwa_addr_match-uid1
                        gwa_addr_match-city1_sap
                        gwa_addr_match-post_code1
                        gwa_addr_match-addr_sap
                        gwa_addr_match-address_ofac
                        gwa_addr_match-city_ofac
                        gwa_addr_match-country_ofac
                       INTO lwa_string2 SEPARATED BY lc_tab .
          CONCATENATE lc_cret lwa_string2 INTO lwa_string2.

          lv_size2 = lv_size2 + strlen( lwa_string2 ).
          APPEND lwa_string2 TO lt_contents2.
          CLEAR : gwa_addr_match, lwa_string2.
        ENDLOOP.
        CLEAR lwa_string2.
      ENDIF.

      SELECT * FROM zzemail
          INTO TABLE lt_email
          WHERE zzprog_id = lc_id.
      IF sy-subrc IS INITIAL AND lt_email IS NOT INITIAL .
*-- Add the recipients to the Send mail
        LOOP AT lt_email INTO lwa_email.
          lwa_rec = lwa_email-zzemail.

*          CHECK NOT lv_rcv_email IS INITIAL.
          o_recipient = cl_cam_address_bcs=>create_internet_address(
                                                        lwa_rec ).
          CALL METHOD o_send_request->add_recipient
            EXPORTING
              i_recipient = o_recipient
              i_express   = lc_cx.
          CLEAR : lwa_email.
        ENDLOOP.
      ENDIF.
* Add attachments for Name & adress match

* This is part where the first attachment is being added


     IF lt_contents1 IS NOT INITIAL.
        CALL METHOD o_document->add_attachment
          EXPORTING
            i_attachment_size    = lv_size1
            i_attachment_type    = lc_at_typ
            i_attachment_subject = lv_sub_att1 "Attachment name
            i_att_content_hex    = lt_binary_content1
            i_att_content_text   = lt_contents1." SOLI_TAB
      ENDIF.

     IF lt_contents2 IS NOT INITIAL.
        CALL METHOD o_document->add_attachment
          EXPORTING
            i_attachment_size    = lv_size2
            i_attachment_type    = lc_at_typ
            i_attachment_subject = lv_sub_att2 "Attachment name
            i_att_content_hex    = lt_binary_content2
            i_att_content_text   = lt_contents2." SOLI_TAB
      ENDIF.

*     add document to send request
      CALL METHOD o_send_request->set_document( o_document ).
*     To send email immediately
      CALL METHOD o_send_request->set_send_immediately( lc_cx ).

*
      CLEAR lv_sender .
      lv_sender = cl_cam_address_bcs=>create_internet_address(  lwa_rec ).
      CALL METHOD o_send_request->set_sender
        EXPORTING
          i_sender = lv_sender.


      CALL METHOD o_send_request->set_status_attributes
        EXPORTING
          i_requested_status = lc_ce
          i_status_mail      = lc_ce.
***-- Send Email
      CALL METHOD o_send_request->send(
        EXPORTING
          i_with_error_screen = lc_cx
        RECEIVING
          result              = lv_sent_to_all ).
* Email has been sent successfully
      IF lv_sent_to_all = lc_cx.
* Commit work
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
      ENDIF.

    CATCH cx_bcs INTO o_bcs_exception.
  ENDTRY.
  CLEAR: lv_flag1, lv_flag2.
ENDFORM.                    " F_EMAIL_BODY

Could you please let me know what can be the issue.

1 ACCEPTED SOLUTION

iftah_peretz
Active Contributor
0 Kudos

Hi Vinay,

Try the following before the code you bolded:

DATA LinesOfT TYPE i.

LinesOfT = lines( l_success ).

lv_size1 = LinesOfT* 255.

And the rest the same. I would also replace the line:

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

with:

COMMIT WORK.

Let me know if this helped you and if not we'll try something else.

Best,

Iftah.

3 REPLIES 3

iftah_peretz
Active Contributor
0 Kudos

Hi Vinay,

Try the following before the code you bolded:

DATA LinesOfT TYPE i.

LinesOfT = lines( l_success ).

lv_size1 = LinesOfT* 255.

And the rest the same. I would also replace the line:

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

with:

COMMIT WORK.

Let me know if this helped you and if not we'll try something else.

Best,

Iftah.

0 Kudos

Thanks. This works fine.

Could you please tell me what was the issue here..

0 Kudos

Dear Vinay,

I think the issue was that you gave the size of a single line as the size of the table (the attachment in this case), thus presenting only the header, as far as the method is concerned this is a text file with a number of lines - you specified the size as one line size.

Make sure you mark the response as answered so that other people can benefit from it.
Best,

Iftah.