|Home • Trainings • Quiz • Tips • Tutorials • Functional • Cert Q's • Interview Q's • Jobs • Testimonials • Advertise • Contact Us|
Send Email with attachment of any format
By Amit Bisht, Infosys
SAP ABAP developers with basic knowledge of Report Programming, Classes and
Body Design, Creating Attachment and Classes (CL_DOCUMENT_BCS).
Purpose of the document:
Step by step procedure to learn how to send an Email with attachment of any
The method used
here for creating and Emailing an attachment is very lucid i.e. the user does
not have to bother about the format of
the file, which he needs to send as an attachment, as the below method
requires only the file-path and the receiver’s Email ID(s).
Many a times
there is a requirement where in we need to send the data from a SAP to external
Email IDs. If we are sure about the Attachment format, we can easily figure out
the best suited Function Module (SAP has provided) and its parameters required
to achieve the functionality, for the attachment to be Emailed.
This method of
sending Email accepts any format of the Attachment i.e. it can be directly used
to attach and send any kind of an Attachment via Email.
involved Emailing the Attachment:
the Document to SAP:
For uploading the document to SAP, function Module ‘GUI_UPLOAD’ is
used with the below mentioned input parameters i.e. filename, file-type.
The document data should be uploaded in Binary Format; hence an
internal table (T_MAILHEX in this case) of SOLIX type (Binary data,
Length 255 characters) is used to hold the data uploaded into SAP. Once the
internal table is populated with the document data it can be used to create the
attachment and send it via Email.
the body of the Email:
In order to Frame the body of the Email we can create Text Symbols with
the texts to be used in the body of the Email.
Once Text Symbols have been created we can populate them into an Internal
Table of type SOLISTI1 (Single List with Column Length 255) in the
sequence, in which they should appear in the body of the Email.
In the below read Statement we are reading the entire text pool into the
Internal table TL_TEXTPOOL of type TEXTPOOL.
Internal table T_CONTENTS can now be populated with the Text symbols as
per the sequence of the Texts (or sentences) required in the mail body.
“Key” above refers to the Text Symbol.
Once the body of the Email is ready (Table T_CONTENTS is populated) we
proceed further towards creating the Attachment using the data in Internal table
T_MAILHEX (populated in the step-1 above).
For creating the Email we need to use the Method Create_Document of
Class CL_DOCUMENT_BCS which requires parameters as follows:
i_type: Document Class, here we are using
HTML format for creating the mail body (i.e. “HTM”).
i_text: Data to appear in the body of the
mail, (T_CONTENTS data).
i_length: Length of the document (Mail
i_subject: Subject of the Email.
the Attachment using Classes:
Now to create the attachment we use the data uploaded
to SAP in Point number 1 (uploading data to SAP) and method ADD_ATTACHMENT
of the class CL_DOCUMENT_BCS as below.
i_attachment_type: Extension of the file.
i_attachment_subject: Subject of the
i_att_content_hex: Data (of the Document
to be attached) uploaded to internal table T_MAILHEX in point number 1
(uploading data to SAP).
the Email using Classes:
Finally, after creating the mail body and
the attachment we can send the Email using SEND method of Class CL_BCS.
Where in, the result of the method (whether the mail
is sent successfully or not) is captured in the parameter result.
useful Tips about HTML:
'<FONT face=Arial size=2>': is used to set the Font and Size of the text in the
'<br>': is used to insert a new line.
'<A href="www.irctc.co.in">': is used to start a Hyperlink in the
'</A>': is used to end a hyperlink.
' ': is used to give a space in between two texts in the mail body.
For more information on HTML please visit
the link http://www.w3schools.com/
You can also attach Audio and Video files using the above method
but there are some size constraints (i.e. if the data is huge, by the time it is
converted into binary format, the session might terminate).
Below, I have attached a demo Program for sending attachment of any format
here for the report Program for attaching and sending files (any format) via Email.
Symbols used in the above Example:
I have only
attached the Screen Shots of the Test Emails (so that the document size won’t
A PDF Attachment with Embedded images
2. MP3 Attachment
After reading this tutorial you
should be able to:
Use classes to
attach and send Email.
Attach a file
(of any format) and send it via Email.
about the various HTML key words which can be used to Format the Email body.
Please send us your feedback/suggestions at webmaster@SAPTechnical.COM
©2006-2007 SAPTechnical.COM. All rights reserved.
product names are trademarks of their respective companies. SAPTechnical.COM
is in no way affiliated with SAP AG.
Graphic Design by Round the Bend Wizards