sending dynamic emails with attachments to external

SENDING DYNAMIC EMAILS WITH
ATTACHMENTS TO EXTERNAL MAIL ID
FROM SAP SYSTEM 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
A. BUILDING A DYNAMIC EMAIL BODY
We can have scenarios like the following email, where customer and the year can be dynamic
and can be replaced in runtime.
Dear customer,
Please find attached the amounts to be invoiced related to SAP Cloud
Subscription Services for year (net values), based upon the
contracts signed by both parties.
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,
CALL FUNCTION 'READ_TEXT'
EXPORTING
*
CLIENT
= SY-MANDT
id
= 'ST'
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
OBJECT
= 4
= 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
LOOP AT lt_lines INTO wa_lines.
REPLACE '&gt_cust_name&' WITH gt_cust_name INTO wa_lines-tdline.
REPLACE '&gt_year&' WITH sy-datum+0(4) INTO wa_lines-tdline."current year
MODIFY lt_lines FROM wa_lines.
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
B. SENDING EMAIL TO EXTERNAL IDs FROM SAP SYSTEM
Here the function module 'SO_NEW_DOCUMENT_SEND_API1' is used for the demonstration of
sending the email, Provide the recipient details into table it_recievers.
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 = '[email protected]'.
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.
populating the message pack details as follows,
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.
APPEND LINES OF LT_LINES TO IT_MESSAGE.
**where LT_LINES is the result of FM
READ_TEXT
After populating the email content (IT_MESSAGE), the FM for sending emails can be called,
CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
document_data
= gd_doc_data
put_in_outbox
= 'X'
commit_work
= 'X'
TABLES
packing_list
= it_packing_list
contents_txt
= it_message
receivers
= it_receivers
EXCEPTIONS
too_many_receivers
= 1
document_not_sent
= 2
document_type_not_exist
= 3
operation_no_authorization = 4
parameter_error
= 5
x_error
= 6
enqueue_error
= 7
OTHERS
= 8.
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).
The detailed description will be as follows,
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,
Once this is done, the email will be sent successfully.
8
Dynamic emails with PDF attachments to external mail IDs - a step by step guide
C. CONVERTING INVOICE / SMARTFORM AS PDF ATTACHMENT IN THE EMAIL
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,
* finding the function module of smartform
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname
= 'ZZ_PO_CLOUD_COLL'
IMPORTING
fm_name
= fm_name
EXCEPTIONS
no_form
= 1
no_function_module = 2
OTHERS
= 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
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,
ls_output_options-tdnoprint
ls_output_options-tddest
= 'X'.
= 'LOC2'.
"bypassing the print preview window
"setting LOC2 as default printer
ls_output_options-tdsenddate
= sy-datum.
ls_output_options-tdsendtime
= sy-uzeit.
ls_output_options-tdnoprint
= abap_true.
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'.
*calling the smartform function module
CALL FUNCTION fm_name
EXPORTING
control_parameters = ls_control_parameters
output_options
= ls_output_options
user_settings
= ' '
is_header
= ls_header
is_settings
= ls_settings
is_created_by
= ls_created_by
IMPORTING
job_output_info
= ls_job_info
TABLES
it_summary
= lt_summary[]
EXCEPTIONS
formatting_error
= 1
internal_error
= 2
send_error
= 3
user_canceled
= 4
OTHERS
= 5.
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
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format
= 'PDF'
IMPORTING
bin_file
= lv_otf
bin_filesize
= lv_filesize
TABLES
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