Send Email with attachment of any format
By Amit Bisht, Infosys
Target Readers: SAP ABAP developers with basic knowledge of Report Programming, Classes and Methods.
Keywords: Email 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 Format.
1. 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).
2. SAP has provided a variety of features for sending an Email to an external ID which are dependent (limitation) on the file formats of the attachments.
3. 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.
4. 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.
Steps involved Emailing the Attachment:
Uploading the document (to be E-mailed) to SAP.
Framing the body of the Email.
Creating the Attachment.
Sending the Email using Classes and its Methods.
1. Uploading 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.
2. Framing 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.
1. In the below read Statement we are reading the entire text pool into the Internal table TL_TEXTPOOL of type TEXTPOOL.
2. 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:
1. i_type: Document Class, here we are using HTML format for creating the mail body (i.e. “HTM”).
2. i_text: Data to appear in the body of the mail, (T_CONTENTS data).
3. i_length: Length of the document (Mail Body).
4. i_subject: Subject of the Email.
3. Creating 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.
1. i_attachment_type: Extension of the file.
2. i_attachment_subject: Subject of the Attachment.
3. 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).
4. Sending 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.
Some useful Tips about HTML:
1. ‘ FONT face=Arial size=2 ‘. is used to set the Font and Size of the text in the Email.
2. ‘ br ‘. is used to insert a new line.
3. ‘ A href= www.irctc.co.in ‘. is used to start a Hyperlink in the Email body.
4. ‘ /A ‘. is used to end a hyperlink.
5. ‘ nbsp;’. is used to give a space in between two texts in the mail body.
6. For more information on HTML please visit the link http://w ww.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 via Email.
Click here for the report Program for attaching and sending files (any format) via Email.
Text Symbols used in the above Example:
001 Please enter at-least one file name
002 Error in reading file for upload
003 Approval Mail Sent Successfully
004 Approval Mail Not Successful
T01 Attachment Detail
T02 Reciever’s EmailID
T04 You can change/add the Text here by changing/adding the text elements and the code.
T05 For giving spaces between texts, you can use HTML Keyword: nbsp .
T06 For Ex; Employee Numbers:
T07 For more information on HTML, Please visit:
T08 Email with Attached document
Selection Texts used:
S_MAILID Email ID
I have only attached the Screen Shots of the Test Emails (so that the document size won’t increase)
1. 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.
· Explain briefly about the various HTML key words which can be used to Format the Email body.
© 2006-2007 SAPTechnical.COM. All rights reserved.
All product names are trademarks of their respective companies. SAPTechnical.COM is in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver, and and any other SAP trademarks are registered trademarks of SAP AG in Germany and in several other countries.
Every effort is made to ensure content integrity. Use information on this site at your own risk.