Tutorial Name

Send email with zip attachment

Tutorial Description

Send email with ZIP attachment file in SAP ABAP, email with ZIP functionality using CL_BCS class

Tutorial Area Advanced ABAP
Prerequisites ABAP
Learning Level Intermediate
Estimated Time to learn 60
Save to Learning List Save to your Learning List
+ -

Step1:Resources used to send email with ZIP attachment Normal Top^

MOst of the time we may need to send email with attachment file using SAP ABAP, in the below tutorial you will be able to learn sending email with zip attachment in SAP ABAP.

Before going into the full details, please go through the tutorial Send email with attachment in SAP ABAP .

Email is a concept of sending binary data to a mail server (SMTP), the mail server recieves the data and send it to the respective recipient.

To send attachment, we need to convert data into binary format.

To send data in an internal table (data) we need to convert internal table data into binary format.

Requirement: Send the list of materials with basic details to the specified email id as a ZIP attachment for a material type. Requirement analysis: Get the list of materials for a material type(MTART) from MARA table and send the material details to the specified email id.

Resources used for sending email with attachment in SAP

CL_BCS (class) To send email using SAP ABAP
CL_ABAP_ZIP (class) ZIP a File in ABAP
HR_KR_STRING_TO_XSTRING (Function Module) Used to convert string to xstring
SCMS_XSTRING_TO_BINARY (Function Module) Used to convert xstring into binary data

Step2:Design selection-screen for email id and material type input Important Top^

Send email wil attachment in SAP ABAP

" class="redactor_content" style="width: 80%; height: 300px" required data-validation-required-message= "Please enter Step Text">

Go to SE38, provide a program name ZSAPN_SEND_EMAIL, create, provide short text and save it in a local object.

Design selection-screen for material type, email id, subject, send immediately flag inputs.

PARAMETERS P_MTART TYPE MARA-MTART. "material type input
PARAMETERS : P_EMAIL TYPE ADR6-SMTP_ADDR. "Email input
PARAMETERS: P_SUB TYPE CHAR50. "email subject
PARAMETERS : P_SEND AS CHECKBOX. "send immediately flag

Send email wil attachment in SAP ABAP

Step3:Set email document for CL_BCS Important Top^

Declare CL_BCS class and create object for the class Cl_BCS.

*Prepare Mail Object
DATA:  LO_SEND_REQUEST TYPE REF TO CL_BCS VALUE IS INITIAL.
CLASS CL_BCS DEFINITION LOAD.
LO_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

Technically email is a binary document, we need to add document to the email object.Document contains parameters like email type (HTM, TXT etc), email subject, email body.

EMAIL BODY: We can send email body of 255 characters per line, email body might be more than 255 characters, so we need to pass it in the form of lines in an internal table (see example below).

Limitation: While using CL_BCS class to send email, we can set maximum 50 character subject only.
* Message body and subject
DATA: LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS VALUE IS INITIAL. "document object
DATA : I_TEXT TYPE BCSY_TEXT. "Table for body
DATA : W_TEXT LIKE LINE OF I_TEXT. "work area for message body
*Set body
W_TEXT-LINE = 'This is the first tutorial of sending email using SAP ABAP programming by SAPNuts.com'.
APPEND W_TEXT TO I_TEXT.
CLEAR W_TEXT.
W_TEXT-LINE = 'SAPNuts.com is the best SAP ABAP learning portal'.
APPEND W_TEXT TO I_TEXT.
CLEAR W_TEXT.
*Create Email document
LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT( "create document
I_TYPE = 'TXT' "Type of document HTM, TXT etc
I_TEXT =  I_TEXT "email body internal table
I_SUBJECT = P_SUB ). "email subject here p_sub input parameter
Pass the email document with subject, body and type to send request.
* Pass the document to send request
  LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).

Step4:Convert Data into binary and create ZIP attachment Important Top^

Declare internal table and work area for MARA, add logic to get data from MARA.

DATA : IT_MARA TYPE TABLE OF MARA, "internal table for MARA
       WA_MARA TYPE MARA. "work area for MARA
**Get data from MARA
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS
    WHERE MTART = P_MTART.

Declare strings, convert internal table data into string with horizantal tab (tab) and new line.

DATA : LV_STRING TYPE STRING, "declare string
       LV_DATA_STRING TYPE STRING, "declare string
LOOP AT IT_MARA INTO WA_MARA.
    CONCATENATE WA_MARA-MATNR WA_MARA-MTART WA_MARA-MEINS WA_MARA-MBRSH WA_MARA-MATKL INTO LV_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
    CONCATENATE LV_DATA_STRING LV_STRING INTO LV_DATA_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>NEWLINE.
    CLEAR: WA_MARA, LV_STRING.
  ENDLOOP.

Convert string data into xstring using function module HR_KR_STRING_TO_XSTRING.

DATA LV_XSTRING TYPE XSTRING .
**Convert string to xstring
  CALL FUNCTION 'HR_KR_STRING_TO_XSTRING'
    EXPORTING
*     codepage_to      = '8300'
      UNICODE_STRING   = LV_DATA_STRING
*     OUT_LEN          =
    IMPORTING
      XSTRING_STREAM   = LV_XSTRING
    EXCEPTIONS
      INVALID_CODEPAGE = 1
      INVALID_STRING   = 2
      OTHERS           = 3.
  IF SY-SUBRC <> 0.
    IF SY-SUBRC = 1 .

    ELSEIF SY-SUBRC = 2 .
      WRITE:/ 'invalid string ' .
    ENDIF.
  ENDIF.
Convert data into ZIP xstring using CL_ABAP_ZIP class by passing the xstring.
DATA: L_ZIPPER TYPE REF TO CL_ABAP_ZIP. " Zip class declerration
DATA : L_DATA TYPE STRING.
***Xstring to binary
  CREATE OBJECT L_ZIPPER.
  "add file to zip
  CALL METHOD L_ZIPPER->ADD
    EXPORTING
      NAME    = 'Material_master_report.xls'"filename
      CONTENT = LV_XSTRING.
  "save zip
  CALL METHOD L_ZIPPER->SAVE
    RECEIVING
      ZIP = LV_XSTRING.
Convert xstring to binary format using function module SCMS_XSTRING_TO_BINARY and ZIP attachment to send request.
* Convert Xstring into Binary
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      BUFFER     = LV_XSTRING
    TABLES
      BINARY_TAB = LIT_BINARY_CONTENT.
*        EXCEPTIONS
*          program_error                     = 1
*          OTHERS                            = 2.

  CLEAR: L_DATA.
  CONCATENATE 'Material_master_' SY-DATUM INTO L_DATA.
  L_ATTSUBJECT = L_DATA.
  CLEAR L_DATA.
* Create Attachment
  TRY.
    LO_DOCUMENT->ADD_ATTACHMENT( EXPORTING
                                    I_ATTACHMENT_TYPE = 'ZIP'
                                    I_ATTACHMENT_SUBJECT = L_ATTSUBJECT
                                    I_ATT_CONTENT_HEX = LIT_BINARY_CONTENT  ).
  ENDTRY.

Step5:Set Sender and Recipient to send request Important Top^

For every email, there is a sender and reciever (recipient), set sender and recipient for send request.

TRY...ENDTRY is used for exception handleing in Object Oriented programming.
*Set Sender
DATA: LO_SENDER TYPE REF TO IF_SENDER_BCS VALUE IS INITIAL.
TRY.
  LO_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ). "sender is the logged in user
* Set sender to send request
  LO_SEND_REQUEST->SET_SENDER(
  EXPORTING
  I_SENDER = LO_SENDER ).
*    CATCH CX_ADDRESS_BCS.
****Catch exception here
ENDTRY.
Set recipient for the send request
**Set recipient
DATA: LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS VALUE IS INITIAL.
LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( P_EMAIL ). "Here Recipient is email input p_email
TRY.
  LO_SEND_REQUEST->ADD_RECIPIENT(
      EXPORTING
      I_RECIPIENT = LO_RECIPIENT
      I_EXPRESS = 'X' ).
*  CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
ENDTRY.

Step6:Set send email for send request Important Top^

In some servers network management team (BASIS) set mail sending frequency to reduce server load in SMTP(Simple Mail Transfer Protocol) configuration, in such cases the mails will be sent based on server load (may be some time later depends on server load)...In such cases we can set send immediately for the send request.

*Set immediate sending
TRY.
  CALL METHOD LO_SEND_REQUEST->SET_SEND_IMMEDIATELY
    EXPORTING
      I_SEND_IMMEDIATELY = 'X'.
*    CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
ENDTRY.
Finally send email using send request.
TRY.
** Send email
  LO_SEND_REQUEST->SEND(
  EXPORTING
  I_WITH_ERROR_SCREEN = 'X' ).
  COMMIT WORK.
  IF SY-SUBRC = 0.
    WRITE :/ 'Mail sent successfully'.
  ENDIF.
*    CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
*catch exception here
ENDTRY.

Step7:Example program for sending email with attachment in SAP ABAP Normal Top^

Example program for sending email with attachment in SAP ABAP programming.

Send email with attachment

send email with attachment

REPORT ZSAPN_SEND_EMAIL.
*Prepare Mail Object
DATA:  LO_SEND_REQUEST TYPE REF TO CL_BCS VALUE IS INITIAL.
CLASS CL_BCS DEFINITION LOAD.
DATA: LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS VALUE IS INITIAL. "document object
DATA : I_TEXT TYPE BCSY_TEXT. "Table for body
DATA : W_TEXT LIKE LINE OF I_TEXT. "work area for message body
DATA: LO_SENDER TYPE REF TO IF_SENDER_BCS VALUE IS INITIAL. "sender
DATA: LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS VALUE IS INITIAL. "recipient
**Selection screen
PARAMETERS P_MTART TYPE MARA-MTART. "material type input
PARAMETERS : P_EMAIL TYPE ADR6-SMTP_ADDR. "Emai input
PARAMETERS: P_SUB TYPE CHAR50. "email subject
PARAMETERS : P_SEND AS CHECKBOX. "send immediatly flag
***Attachment data
DATA : IT_MARA TYPE TABLE OF MARA,
  WA_MARA TYPE MARA.
DATA : LV_STRING TYPE STRING,
  LV_DATA_STRING TYPE STRING,
  LV_XSTRING TYPE XSTRING.
DATA: LIT_BINARY_CONTENT TYPE SOLIX_TAB,
  L_ATTSUBJECT  TYPE SOOD-OBJDES.

START-OF-SELECTION.
  LO_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).
* Message body and subject
*Set body
  W_TEXT-LINE = 'This is the first tutorial of sending email using SAP ABAP programming by SAPNuts.com'.
  APPEND W_TEXT TO I_TEXT.
  CLEAR W_TEXT.
  W_TEXT-LINE = 'SAPNuts.com is the best SAP ABAP learning portal'.
  APPEND W_TEXT TO I_TEXT.
  CLEAR W_TEXT.
  LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT( "create document
  I_TYPE = 'TXT' "Type of document HTM, TXT etc
  I_TEXT =  I_TEXT "email body internal table
  I_SUBJECT = P_SUB ). "email subject here p_sub input parameter
* Pass the document to send request
  LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).
***Set attachment
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS
  WHERE MTART = P_MTART.

  LOOP AT IT_MARA INTO WA_MARA.
  CONCATENATE WA_MARA-MATNR WA_MARA-MTART WA_MARA-MEINS WA_MARA-MBRSH WA_MARA-MATKL INTO LV_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
  CONCATENATE LV_DATA_STRING LV_STRING INTO LV_DATA_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>NEWLINE.
  ENDLOOP.
**Convert string to xstring
  CALL FUNCTION 'HR_KR_STRING_TO_XSTRING'
  EXPORTING
*  codepage_to  = '8300'
  UNICODE_STRING  = LV_DATA_STRING
*  OUT_LEN  =
  IMPORTING
  XSTRING_STREAM  = LV_XSTRING
  EXCEPTIONS
  INVALID_CODEPAGE = 1
  INVALID_STRING  = 2
  OTHERS  = 3.
  IF SY-SUBRC <> 0.
  IF SY-SUBRC = 1 .

  ELSEIF SY-SUBRC = 2 .
  WRITE:/ 'invalid string ' .
  ENDIF.
  ENDIF.
  DATA: L_ZIPPER TYPE REF TO CL_ABAP_ZIP. " Zip class declerration
  DATA : L_DATA TYPE STRING.
***Xstring to binary
  CREATE OBJECT L_ZIPPER.
  "add file to zip
  CALL METHOD L_ZIPPER->ADD
  EXPORTING
  NAME  = 'Material_master_report.xls'"filename
  CONTENT = LV_XSTRING.
  "save zip
  CALL METHOD L_ZIPPER->SAVE
  RECEIVING
  ZIP = LV_XSTRING.
* Convert Xstring into Binary
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
  BUFFER  = LV_XSTRING
  TABLES
  BINARY_TAB = LIT_BINARY_CONTENT.
*  EXCEPTIONS
*  program_error  = 1
*  OTHERS  = 2.
*  IF sy-subrc <> 0.
*  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
*  ENDIF.
  CLEAR: L_DATA.
  CONCATENATE 'Material_master_' SY-DATUM INTO L_DATA.
  L_ATTSUBJECT = L_DATA.
  CLEAR L_DATA.
* Create Attachment
  TRY.
  LO_DOCUMENT->ADD_ATTACHMENT( EXPORTING
  I_ATTACHMENT_TYPE = 'ZIP'
  I_ATTACHMENT_SUBJECT = L_ATTSUBJECT
  I_ATT_CONTENT_HEX = LIT_BINARY_CONTENT  ).
  ENDTRY.
  TRY.
  LO_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ). "sender is the logged in user
* Set sender to send request
  LO_SEND_REQUEST->SET_SENDER(
  EXPORTING
  I_SENDER = LO_SENDER ).
*  CATCH CX_ADDRESS_BCS.
****Catch exception here
  ENDTRY.
**Set recipient
  LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( P_EMAIL ). "Here Recipient is email input p_email
  TRY.
  LO_SEND_REQUEST->ADD_RECIPIENT(
  EXPORTING
  I_RECIPIENT = LO_RECIPIENT
  I_EXPRESS = 'X' ).
*  CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
  ENDTRY.
  TRY.
  CALL METHOD LO_SEND_REQUEST->SET_SEND_IMMEDIATELY
  EXPORTING
  I_SEND_IMMEDIATELY = P_SEND. "here selection screen input p_send
*  CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
  ENDTRY.
  TRY.
** Send email
  LO_SEND_REQUEST->SEND(
  EXPORTING
  I_WITH_ERROR_SCREEN = 'X' ).
  COMMIT WORK.
  IF SY-SUBRC = 0. "mail sent successfully
  WRITE :/ 'Mail sent successfully'.
  ENDIF.
*  CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
*catch exception here
  ENDTRY.

Author | Last Updated| | 2 Comments | F Share

Tutorial Comments

Total Comments: Add your Comment
07 Jul 2014

That tutorial was very helpful. Thank you.

21 Jul 2014

hello, i've tried to implement this solution, but not sendig to an email, just saving with gui_download, but the txt file doesnt separate the lines.. every data is in the same line. best regards

Leave a Comment

Enter Comment Text