Application Development and Automation 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: 
Read only

Email internal table as zip file

Former Member
2,298

Hello everyone

I spent several days to have a full example on how to convert an internal table has ZIP file than send it by email.

I found several example to upload file from the OS and than convert it to ZIP to save it after. But nothing from the current internal email.

So, for the benefit of everybody who that can interrest, here is the code I did. Feel free to optimize it and post the new version here.

===========================================

Moderator message - Please respect the 2,500 character maximum when posting. Post only the relevant portions of code

==========================================

Regards

dstj

Edited by: Rob Burbank on Feb 12, 2010 2:09 PM

Hello everyone

I spent several days to have a full example on how to convert an internal table has ZIP file than send it by email.

I found several example to upload file from the OS and than convert it to ZIP to save it after. But nothing from the current internal email.

So, for the benefit of everybody who that can interrest, here is the code I did. Feel free to optimize it and post the new version here.

===========================================

Moderator message - Please respect the 2,500 character maximum when posting. Post only the relevant portions of code

==========================================

Regards

dstj

Edited by: Rob Burbank on Feb 12, 2010 2:09 PM

8 REPLIES 8
Read only

Former Member
0 Likes
1,738

Hello everyone.

Based on my understanding, My post has been removed, by the moderators, because it was to long.

I hope the moderators will accept if I am posting it in several sections. I spent a lot of time in research to have it work properly. I just which to share what I found.

Declaration section:

*&---------------------------------------------------------------------*
*& Report  ZTABLE_TO_ZIP_SEND_EMAIL
*&              Send email with attachment in ZIP file format
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTABLE_TO_ZIP_SEND_EMAIL.

CLASS cl_bcs DEFINITION LOAD.
DATA: lo_send_request   TYPE REF TO cl_bcs VALUE IS INITIAL,
      lo_sender         TYPE REF TO if_sender_bcs    VALUE IS INITIAL,
      lo_recipient      TYPE REF TO if_recipient_bcs VALUE IS INITIAL,
      lo_document       TYPE REF TO cl_document_bcs VALUE IS INITIAL,
      lx_document_bcs   TYPE REF TO cx_document_bcs VALUE IS INITIAL,
      lv_sent_to_all(1) TYPE c VALUE IS INITIAL.

DATA: lt_recipients  TYPE isu_adr6_tab,
      ls_recipients  LIKE LINE OF lt_recipients,
      l_subject      TYPE so_obj_des,
      lt_body        TYPE bcsy_text,
      ls_body        LIKE LINE OF lt_body,
      l_attsubject   TYPE sood-objdes.

Read only

0 Likes
1,738

Email initialization section:

* --- New email structure ---
lo_send_request = cl_bcs=>create_persistent( ).

* --- Set Email sender ---
lo_sender = cl_sapuser_bcs=>create( sy-uname ).
lo_send_request->set_sender( EXPORTING i_sender = lo_sender ).

* --- For test reason fill up recipients table ---
CLEAR ls_recipients.
ls_recipients-smtp_addr = 'EMAIL_ADDRESS'.
APPEND ls_recipients TO lt_recipients.
* --- Recipients test data end ---

* --- Recipients ---
LOOP AT lt_recipients INTO ls_recipients.
  lo_recipient = cl_cam_address_bcs=>create_internet_address( ls_recipients-smtp_addr ).
  lo_send_request->add_recipient( EXPORTING
                                    i_recipient = lo_recipient
                                    i_express = 'X' ).
ENDLOOP.

* --- Email Subject ---
l_subject = 'EMAIL SUBJECT'.

* --- Test Body data ---
ls_body-line = 'Body section'.
APPEND ls_body TO lt_body.

* --- Subject AND Body structure ---
lo_document = cl_document_bcs=>create_document( i_type = 'RAW'
                                                i_subject = l_subject
                                                i_text = lt_body ).

Read only

0 Likes
1,738

Converting a field from Internal Table into XString

For this example: The USERID of SAP user is the list we will compress.

* --- Test internal table to attach
DATA: lt_data                  TYPE string OCCURS 0 WITH HEADER LINE,
      lt_att_comm_content_hex  TYPE solix_tab.
SELECT name1 INTO TABLE lt_data FROM usr03.
DATA: l_string TYPE string,
      l_xstring TYPE xstring,
      l_xstring2 TYPE xstring.
CLEAR l_string.
LOOP AT lt_data.
  CONCATENATE l_string lt_data INTO l_string SEPARATED BY cl_abap_char_utilities=>cr_lf.
ENDLOOP.

********************************************************
* --- SOMEWHERE HERE SHOULD HAVE THE ZIP CONVERSION ---
********************************************************

* --- Convert internal table into XSTRING ---
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
  EXPORTING
    text           = l_string
*   MIMETYPE       = ' '
*   ENCODING       =
 IMPORTING
   buffer         = l_xstring
* EXCEPTIONS
*   FAILED         = 1
*   OTHERS         = 2
          .

Read only

0 Likes
1,738

Convert into ZIP format.

For this example, the zip file will contain only 1 file.

********************************************************
* --- SOMEWHERE HERE SHOULD HAVE THE ZIP CONVERSION ---
********************************************************
DATA: L_ZIPPER TYPE REF TO cl_abap_zip,
      FILENAME type string,
      zip      type xstring,
      filelenght type i.
"-------------------------------------------------------------------------------
"   ZIP the file
"-------------------------------------------------------------------------------
" create our zipper object

filename = 'TEST.TXT'.
CREATE OBJECT L_ZIPPER.
"add file to zip
CALL METHOD L_ZIPPER->ADD
  EXPORTING
    NAME    = filename
    CONTENT = l_xstring.

"save zip
CALL METHOD L_ZIPPER->SAVE
  RECEIVING     ZIP    = zip     .



***************************************

Read only

0 Likes
1,738

Attach the ZIP file to the email structure and send the email.

* --- Convert Xstring into Binary ---
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer                = zip
*   APPEND_TO_TABLE       = ' '
* IMPORTING
*   OUTPUT_LENGTH         =
  TABLES
    binary_tab            = lt_att_comm_content_hex
          .
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO ---
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ---
ENDIF.


* --- Attachment file name ---
l_attsubject = 'Attachment.zip'.

* --- Create Attachment ---
TRY.
    lo_document->add_attachment( EXPORTING
                                    i_attachment_type = 'ZIP'
                                    i_attachment_subject = l_attsubject
                                    i_att_content_hex = lt_att_comm_content_hex ).
  CATCH cx_document_bcs INTO lx_document_bcs.
ENDTRY.

* --- Email sending section ---
* --- Add attachment to send request ---
lo_send_request->set_document( lo_document ).

* --- Send email ---
lo_send_request->send( EXPORTING i_with_error_screen = 'X'
                       RECEIVING result = lv_sent_to_all ).

COMMIT WORK.

MESSAGE 'Mail has been sent successfully!!!' TYPE 'S'.

This is completing the program.

I hope that can help.

Have fun

dstj

Edited by: dstj on Feb 23, 2010 9:29 PM

Read only

Former Member
0 Likes
1,738

Hello Daniel,

Thank you very much for this post, it is quite helpful. However, I have followed your template very closely and my file will not send. I am not an advaned ABAP programmer, but have a good knowledge of programming in general...here's a bit of my code related to this...

*-Create table body

  LOOP AT (internal table) INTO (structure)
  (Fill lv_line with one line of string based on structure)

    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text = lv_line
      IMPORTING
        buffer = lv_xline

...

    CONCATENATE lv_xcontent lv_xline INTO lv_xcontent IN BYTE MODE.
  ENDLOOP.

*-Create zip xstring
  CREATE OBJECT lo_zipper.
  CALL METHOD lo_zipper->add
    EXPORTING
      name = 'AHReport.zip'
      content = lv_xcontent.
  CALL METHOD lo_zipper->save
    RECEIVING
      zip = lv_xzipcontent.

*-Convert zip xstring to binary
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer = lv_xzipcontent
    TABLES
      binary_tab = lt_data.

*-    Add attachment     

      TRY.
        CALL METHOD l_document->add_attachment
            EXPORTING
              i_attachment_type = 'ZIP'

              i_attachment_subject = 'Report'
              i_att_content_hex = lt_data.
        CATCH cx_document_bcs.
      ENDTRY.

Any help would be very much appreciated, ty.

Read only

0 Likes
1,738

Hello David

Based on my observation, you've included the conversion into Xstring into your first loop.

I am not sure that s a good idea.

1. Create the string from the itab by separate the record by cr_lf

LOOP AT lt_data.

  CONCATENATE l_string lt_data INTO l_string SEPARATED BY cl_abap_char_utilities=>cr_lf.

ENDLOOP.

2. When the entire string is builed, convert it in xstring.

If it is still not working, validate that the structure to build the email is respected.

. Subject

. Body

. Recipients

.etc

Remember to include the "COMMIT WORK" comment to your code.

If still not working, validate if you can send an email without any attachment.

Then an email with an attachment that is not zipped.

Just to narrow the problems.

I hope that could help.

Regards

Daniel

Read only

0 Likes
1,738

That answer was extremely helpful, thank you (I will try to find out how to flag it as a helpful answer now)!