Dynamic Email and Attachment Using Reports
Dynamic Email and Attachment Using Reports
JISHA VIJAYAN
SAP ABAP DEVELOPER
TATA CONSULTANCY SERVICES
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
Sending emails from SAP system to external email IDs can be achieved by various techniques, we
can use FMs like SO_NEW_DOCUMENT_SEND_API1. In object oriented way, we can use classes
like CL_BCS etc.
But sometimes we will need a dynamic email body, sometimes with attachments and this
attachments can be a pdf of an invoice available in SAP system etc. So taking all this scenarios
into consideration this document explains the following practical scenarios step by step.
A. How to have a dynamic email body using standard texts - creation of such standard text
and replacing the dynamic variables as per our requirements
B. Sending Emails to external mail IDs - how to enable the external mail ID in SAP system
for sending emails
C. How to convert an Invoice to PDF format and add as an email attachment
2
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
We can have scenarios like the following email, where customer and the year can be dynamic
and can be replaced in runtime.
Dear customer,
Inorder to achieve this, Go to transaction SO10 which is for the creation of standard texts, and
create a standard text as follows,
Add the content of the standard text as per your email text,
3
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
Now it is time to replace the dynamic variables customer and year. Go to Edit -> command ->
insert command and add your dynamic variable which's beginning and end marked by '&'
Now your standard text will look like something like this,
The variables gt_cust_name and gt_year can be replaced in the report after calling the standard
text using the READ_TEXT function module as follows,
4
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
language = 'EN'
name = 'ZDYN_EMAIL_BODY'
object = 'TEXT'
* ARCHIVE_HANDLE = 0
* LOCAL_CAT = ' '
* IMPORTING
* HEADER =
* OLD_LINE_COUNTER =
TABLES
lines = lt_lines
EXCEPTIONS
ID = 1
LANGUAGE = 2
NAME = 3
NOT_FOUND = 4
OBJECT = 5
REFERENCE_CHECK = 6
WRONG_ACCESS_TO_ARCHIVE = 7
OTHERS = 8
.
Now the table lt_lines holds the standard text content, the dynamic texts can be replaced as
shown below,
SELECT SINGLE name1 INTO gt_cust_name FROM kna1 WHERE kunnr EQ pcust_no."cust
omer name
ENDLOOP.
The table lt_lines can be converted to the desired format (based upon the FM or class you are
using) and used as your dynamic email body.
5
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
FORM populate_receivers.
**Populating Mail Recepients
**If there are more than one mail recepient then loop and append them to it_r
eceivers
it_receivers-receiver = 'jisha.vijayan.e@sap.com'.
it_receivers-rec_type = 'U'.
it_receivers-com_type = 'INT'.
it_receivers-notif_del = 'X'.
it_receivers-notif_ndel = 'X'.
it_receivers-express = 'X'.
APPEND it_receivers.
ENDFORM.
FORM populate_pack .
* Populate the subject/generic message attributes
gd_doc_data-obj_langu = sy-langu.
gd_doc_data-obj_name = 'SAPRPT'.
gd_doc_data-obj_descr = psubject .
gd_doc_data-sensitivty = 'F'.
* Describe the body of the message
CLEAR it_packing_list.
REFRESH it_packing_list.
it_packing_list-transf_bin = space.
it_packing_list-head_start = 1.
it_packing_list-head_num = 0.
it_packing_list-body_start = 1.
DESCRIBE TABLE it_message LINES it_packing_list-body_num.
it_packing_list-doc_type = 'RAW'.
APPEND it_packing_list.
ENDFORM.
6
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
The mail content is stored in internal table IT_MESSAGE which can be a simple text, standard text
etc. If it is a standard text accessed using function module READ_TEXT as previous example, we
can use the following statement for storing the same in IT_MESSAGE.
After populating the email content (IT_MESSAGE), the FM for sending emails can be called,
Once you execute the following code, in 99 percentage of the cases you will get an error saying
'Mail cannot be send'. The same error can be observed in detail SAP business workplace inbox
(transaction SO01).
7
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
The roadblock is the highlighted issue in the image, we need to determine a node for the email
ID of the intended recipient. Since this is an email related issue, we should define this email
address under SMTP node in the transaction SCOT (Business communication services -
Administration ) as follows,
8
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
The below scenario the class CL_BCS is used for sending email, CL_DOCUMENT_BCS is used for
attachment and email body generation. The same can be achieved using function module
'SO_NEW_DOCUMENT_ATT_SEND_API1' when it comes to attachments, instead of
'SO_NEW_DOCUMENT_SEND_API1'.
In order to find the function module of the smart form the following function is used,
Before calling the function module name obtained from the above code, the following
parameters are set,
ls_control_parameters-device = 'PRINTER'.
ls_control_parameters-no_dialog = abap_true.
ls_control_parameters-preview = abap_true.
ls_control_parameters-replangu1 = ls_control_parameters-langu.
ls_control_parameters-getotf = 'X'. "to get the otf format,
9
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
ls_output_options-tdnoarch = abap_true.
ls_output_options-tdlifetime = 0.
ls_output_options-tdreceiver = sy-uname.
ls_output_options-tdcopies = '001'.
The local structure field LS_JOB_INFO-OTFDATA received from the above function will contain
the open type format of the smart form/Invoice which is converted into PDF format by the below
code,
*convert the smartform OTF file to PDF
10
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
otf = ls_job_info-otfdata
lines = lt_lines
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
err_bad_otf = 4
OTHERS = 5.
The output LV_OTF can now be attached in any email, but depending upon the FM or class we
are using, it should be converted to the right format. In our case since CL_DOCUMENT_BCS
class is used, LV_OTF should be converted to SOLIX_TAB since the attachment file type is
SOLIX_TAB in the ADD_ATTACHMENT method of the class.
CALL METHOD cl_document_bcs=>xstring_to_solix
EXPORTING
ip_xstring = lv_otf
RECEIVING
rt_solix = lt_pdf_data.
After the conversion, the PDF is attached to the object LR_DOC_BCS using the below method.
lr_doc_bcs->add_attachment(
EXPORTING
i_attachment_type = 'BIN'
i_attachment_subject = 'PO_LETTER.PDF'
i_att_content_hex = lt_pdf_data
).
Now when the mail is sent using the same object, it will contain the smart form / invoice pdf
attachment.
11