PDF Attached To An Email
PDF Attached To An Email
Introduction
In this blog post, you will learn how to attach a PDF document to your email. The PDF can be
generated from a smartform as an example.
Prerequisite
1. Some knowledge of using class CL_BCS for email sending.
2. A Smartform – It can be a basic smart form with a short text, we just need it for display
purpose.
3. A trigger program.
Steps
Create a Smartform with any Z/Y name.
I created with the name “ZTEST_SMARTFORM”. It actually just have a short text as below:
CONSTANTS:
lc_sfname TYPE tdsfname VALUE 'ZTEST_SMARTFORM'. "Name of Smartform
"Object References
DATA: lo_bcs TYPE REF TO cl_bcs,
lo_doc_bcs TYPE REF TO cl_document_bcs,
lo_recep TYPE REF TO if_recipient_bcs,
lo_sapuser_bcs TYPE REF TO cl_sapuser_bcs,
lo_cx_bcx TYPE REF TO cx_bcs.
"Internal Tables.
DATA: lt_otfdata TYPE ssfcrescl,
lt_binary_content TYPE solix_tab,
lt_text TYPE bcsy_text,
lt_pdf_tab TYPE STANDARD TABLE OF tline,
lt_otf TYPE STANDARD TABLE OF itcoo.
"Work Areas
DATA: ls_ctrlop TYPE ssfctrlop,
ls_outopt TYPE ssfcompop.
"Variables
DATA: lv_bin_filesize TYPE so_obj_len,
lv_sent_to_all TYPE os_boolean,
lv_bin_xstr TYPE xstring,
lv_fname TYPE rs38l_fnam,
lv_string_text TYPE string.
"Control Parameters
ls_ctrlop-getotf = 'X'.
ls_ctrlop-no_dialog = 'X'.
ls_ctrlop-preview = space.
"Output Options
ls_outopt-tdnoprev = 'X'.
ls_outopt-tddest = 'LOCL'.
ls_outopt-tdnoprint = 'X'.
lt_otf[] = lt_otfdata-otfdata[].
*---------------------------------------------------------------------
*-----------------& Add attachment to document *----------------
*---------------------------------------------------------------------
* BCS expects document content here e.g. from document upload
* binary_content = ...
CALL METHOD lo_doc_bcs->add_attachment
EXPORTING
i_attachment_type = 'PDF'
i_attachment_size = lv_bin_filesize
i_attachment_subject = 'Test Email'
i_att_content_hex = lt_binary_content.
Set Sender
Note: this is necessary only if you want to set the sender different from actual user (SY-
UNAME). Otherwise sender is set automatically with actual user.
FYI. I have commented the code in my program.
*---------------------------------------------------------------------
*------------------------& Set Sender *-------------------------
*---------------------------------------------------------------------
* lo_sapuser_bcs = cl_sapuser_bcs=>create( sy-uname ).
* CALL METHOD lo_bcs->set_sender
* EXPORTING
* i_sender = lo_sapuser_bcs.
lo_recep = cl_cam_address_bcs=>create_internet_address(
'[email protected]' ).
*---------------------------------------------------------------------
*-----------------& Send the email *-----------------------------
*---------------------------------------------------------------------
CALL METHOD lo_bcs->send(
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = lv_sent_to_all ).
*---------------------------------------------------------------------
*-----------------& Exception Handling *------------------------
*---------------------------------------------------------------------
CATCH cx_bcs INTO lo_cx_bcx.
"Appropriate Exception Handling
WRITE: 'Exception:', lo_cx_bcx->error_type.
ENDTRY.
Results
Once you will execute the above program and see the result in transaction SOST, the email
will look like below:
You can see the email body as below and you can find the attached document in the
Attachments tab
The attached document will look like below: