Skip to Content
Technical Articles
Author's profile photo Mohit Sharma

PDF attached to an email – But with object oriented way


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.


  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.



Create a Smartform with any Z/Y name.

I created with the name “ZTEST_SMARTFORM”. It actually just have a short text as below:


Create an executable program in SE38.

I created it with the name “ZTEST_EMAIL_ATTACHMENT”.



You can create the data objects as per your naming conventions.

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.

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.


Import the function module name of the SMARTFORM .

    formname           = lc_sfname
    fm_name            = lv_fname
    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.


Populate control parameters, output options and then call the FM of your SMARTFORM.

We will get the OTF data generated by the smartform in internal table “lt_otf”.

"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'.   

    control_parameters = ls_ctrlop
    output_options     = ls_outopt
    job_output_info    = lt_otfdata
    formatting_error   = 1
    internal_error     = 2
    send_error         = 3
    user_canceled      = 4
    OTHERS             = 5.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

lt_otf[] = lt_otfdata-otfdata[].


Convert the OTF data to XSTRING “lv_bin_xstr”.

    format                = 'PDF'  
    bin_filesize          = lv_bin_filesize
    bin_file              = lv_bin_xstr
    otf                   = lt_otf[]
    lines                 = lt_pdf_tab[]
    err_max_linewidth     = 1
    err_format            = 2
    err_conv_not_possible = 3
    OTHERS                = 4.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.


Convert the XSTRING to a binary table “lt_binary_content”.

***Xstring to binary
    buffer     = lv_bin_xstr
    binary_tab = lt_binary_content.


Prepare to Send Email

  •  Create persistent send request

*     -------- create persistent send request ------------------------
    lo_bcs = cl_bcs=>create_persistent( ).
  • Create Email Body

    "First line
    CONCATENATE 'Dear Colleague' cl_abap_char_utilities=>newline INTO lv_string_text.
    APPEND lv_string_text TO lt_text.
    CLEAR lv_string_text.
    "Second line
    CONCATENATE 'Please find attached a test smartform.'
     cl_abap_char_utilities=>newline INTO lv_string_text.
    APPEND lv_string_text TO lt_text.
    CLEAR lv_string_text.
    "Third line
    APPEND 'Best Regards,' TO lt_text.
    "Fourth line
    APPEND 'Systems Administrator.' TO lt_text.


  • Create Email

*-----------------&      Create Document     *------------------------
    lo_doc_bcs = cl_document_bcs=>create_document(
                    i_type    = 'RAW'
                    i_text    = lt_text[]
                    i_length  = '12'
                    i_subject = 'Test Email' ).   "Subject of the Email


  • Add attachment to document and Add document to send request

The internal table “lt_binary_content” contains the content of our attachment.

*-----------------&   Add attachment to document     *----------------
*     BCS expects document content here e.g. from document upload
*     binary_content = ...
    CALL METHOD lo_doc_bcs->add_attachment
        i_attachment_type    = 'PDF'
        i_attachment_size    = lv_bin_filesize
        i_attachment_subject = 'Test Email'
        i_att_content_hex    = lt_binary_content.

*     add document to send request
    CALL METHOD lo_bcs->set_document( lo_doc_bcs ).


  • 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
*        i_sender = lo_sapuser_bcs.


  • Add recipient (email address)

You can use more than one recipient as well.

    lo_recep = cl_cam_address_bcs=>create_internet_address(
                                                '' ).

"Add recipient with its respective attributes to send request
    CALL METHOD lo_bcs->add_recipient
        i_recipient = lo_recep
        i_express   = 'X'.


  • Set Send Immediately

You can set this to send  your email immediately.

CALL METHOD lo_bcs->set_send_immediately
        i_send_immediately = 'X'.


  • Send the Email

*-----------------&   Send the email    *-----------------------------
    CALL METHOD lo_bcs->send(
        i_with_error_screen = 'X'
        result              = lv_sent_to_all ).

    IF lv_sent_to_all IS NOT INITIAL.

*-----------------&   Exception Handling     *------------------------
  CATCH cx_bcs INTO lo_cx_bcx.
    "Appropriate Exception Handling
    WRITE: 'Exception:', lo_cx_bcx->error_type.



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:



In this way you can attach a PDF document with your email using object oriented way.


For any issues, improvements, additions or any other concerns, please feel free to contact me.

I look forward for your feedback and suggestions.

Keep learning!! Keep improving!!

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Wang Tao
      Wang Tao

      Nice Article , Great.

      Author's profile photo Mohit Sharma
      Mohit Sharma
      Blog Post Author

      Thanks Wang Tao, I am glad you liked it..

      Author's profile photo Puneeth Sangepu
      Puneeth Sangepu

      Nice article. I have to send excel as attachment. I am able to send it to excel but when I opened the excel I am getting an warning. Please help me in avoiding that warning.

      Author's profile photo Shamsher Ali Mohammed
      Shamsher Ali Mohammed

      Well explained 👏

      Author's profile photo Melih Eren
      Melih Eren

      Suppose i am sending an email directly from ADobeForm how i can figure out

       i_attachment_size    = lv_bin_filesize


      Or do i need it ? Thank you for article it helps a lot to me ^^

      Author's profile photo Ashish Kaple
      Ashish Kaple

      Thanks a lot Mohit. This immensely helped me.

      Author's profile photo Thol KHOM
      Thol KHOM

      Thanks a lot, Mohit. I have an Excel form that I need to attach to the email. This Excel is not generated by SAP. How can I do this?