An Object-Oriented Approach to Emailing a PDF Attachment

You will also discover how to send an object-oriented email with a PDF attachment in this blog post. This time, a SMARTFORM will create the PDF.

Required

A Smartform: All that is needed for display is a simple smartform with a brief text field.

basic familiarity with sending emails using class CL_BCS.

an application that triggers.

Steps

Any Z/Y name can be used to create a Smartform.

I made something called “ZTEST_SMARTFORM_ATTACHMENT.” Actually, it only has the little text seen below:

In SE38, create an executable program.

It was made by me and was named “ZTEST_EMAIL_WITH_ATTACHMENT.”

INFORMATION DECLARATIONS

(We apologize for the naming convention that was employed.)

CONSTANTS:
lc_sfname TYPE tdsfname VALUE 'ZTEST_SMARTFORM_ATTACHMENT'. "Name of Smartform

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

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

"Local Structures
DATA: ls_ctrlop TYPE ssfctrlop,
      ls_outopt TYPE ssfcompop.

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

Get the name of the function module of the SMARTFORM .

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    formname           = lc_sfname
  IMPORTING
    fm_name            = lv_fname
  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.

Fill the Control Parameters, output options and 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'.    "No printing from print preview

CALL FUNCTION lv_fname
  EXPORTING
    control_parameters = ls_ctrlop
    output_options     = ls_outopt
  IMPORTING
    job_output_info    = lt_otfdata
  EXCEPTIONS
    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.
ENDIF.


lt_otf[] = lt_otfdata-otfdata[].

Convert OTF Data to XSTRING “lv_bin_xstr”.

CALL FUNCTION 'CONVERT_OTF'
  EXPORTING
    format                = 'PDF'   "PDF to get pdf output
  IMPORTING
    bin_filesize          = lv_bin_filesize
    bin_file              = lv_bin_xstr
  TABLES
    otf                   = lt_otf[]
    lines                 = lt_pdf_tab[]
  EXCEPTIONS
    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.
ENDIF.

Convert the XSTRING to Binary table “lt_binary_content”.

***Xstring to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer     = lv_bin_xstr
  TABLES
    binary_tab = lt_binary_content.

Prepare to Send Email

 Create persistent send request

TRY.
*     -------- create persistent send request ------------------------
    lo_bcs = cl_bcs=>create_persistent( ).

Create Email Body

    "Line-1
    CONCATENATE 'Dear Colleague' cl_abap_char_utilities=>newline INTO lv_string_text.
    APPEND lv_string_text TO lt_text.
    CLEAR lv_string_text.
    "Line-2
    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.
    "Line-3
    APPEND 'Best Regards,' TO lt_text.
    "Line-4
    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
      EXPORTING
        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

Keep in mind that you only need to do this if you wish to set the sender to be different from the real user (SY-UNAME). If not, the sender is automatically configured using the real user.
For the record. In my software, I have commented out the code.

*---------------------------------------------------------------------
*------------------------&   Set Sender     *-------------------------
*---------------------------------------------------------------------
*    lo_sapuser_bcs = cl_sapuser_bcs=>create( sy-uname ).
*    CALL METHOD lo_bcs->set_sender
*      EXPORTING
*        i_sender = lo_sapuser_bcs.

Add recipient (e–mail address)

You can use multiple recipients as well.

    lo_recep = cl_cam_address_bcs=>create_internet_address(
                                                'test@test123.com' ).

"Add recipient with its respective attributes to send request
    CALL METHOD lo_bcs->add_recipient
      EXPORTING
        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
      EXPORTING
        i_send_immediately = 'X'.

Send the Email

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

    IF lv_sent_to_all IS NOT INITIAL.
      COMMIT WORK.
    ENDIF.


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

Conclusion

One method for sending an email with a PDF attachment is as follows.

Please get in touch with us if you have any questions or concerns about any problems, updates, additions, or other matters.

I eagerly await your opinions and recommendations.

Please share your thoughts.


you may be interested in this blog here:-

Just a key and two clicks for ALV consistency check

Making Your First Salesforce Appexchange: A Complete Strategy

  • Related Posts

    Important Tables with SAP FI

    Financial Accounting Table Name           Description                           Important Fields Financial Accounting FBAS             Financial Accounting “Basis”BKPF             Accounting Document Header              BUKRS / BELNR / GJAHRBSEG             Accounting Document Segment             BUKRS / BELNR / GJAHR /…

    Table of Great Significance in SAP AA (Asset Accounting)

    Introduction Maintaining accurate financial records and guaranteeing operational efficiency depend heavily on accounting entries for inventory. Businesses can maximize inventory management and boost revenue by comprehending important ideas, adhering to…

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You Missed

    Important Tables with SAP FI

    • By Varad
    • February 22, 2025
    • 9 views
    Important Tables with SAP FI

    Table of Great Significance in SAP AA (Asset Accounting)

    • By Varad
    • February 21, 2025
    • 17 views
    Table of Great Significance in SAP AA (Asset Accounting)

    How Can UI5 Apps Be Deployed Without LPD_CUST?

    • By Varad
    • February 20, 2025
    • 36 views
    How Can UI5 Apps Be Deployed Without LPD_CUST?

    SAPUI5: Fundamental Factory Functions for Freshmen

    • By Varad
    • February 19, 2025
    • 46 views
    SAPUI5: Fundamental Factory Functions for Freshmen

    Utilizing the Formatter Function in SAPUI5

    • By Varad
    • February 18, 2025
    • 40 views
    Utilizing the Formatter Function in SAPUI5

    SAPUI5, the newly introduced Resource Model

    • By Varad
    • February 17, 2025
    • 66 views
    SAPUI5, the newly introduced Resource Model