Skip to content
Skip to content
Menu
SAP ABAP DWIMAN
  • About
SAP ABAP DWIMAN

SAP ABAP – Send Email with Smartforms Attachment

By juananda.satria on November 23, 2023November 23, 2023

Hello Friends, today I will share again my codes to you about “How to send email with smartforms attachment as PDF”

First You Need to call your smartforms and convert it to OTF

*&---------------------------------------------------------------------*
*&      Form  F_DISPLAY_DATA
*&---------------------------------------------------------------------*
*       Display/Print Smartforms layout
*----------------------------------------------------------------------*
FORM f_display_data USING uw_header TYPE gty_header
                          ut_item    TYPE gtt_item
                          uv_screen  TYPE char1.

  DATA: lv_form            TYPE tdsfname,
        lv_intern_formname TYPE rs38l_fnam,
        ls_output_options  TYPE ssfcompop,
        ls_control         TYPE ssfctrlop,
        ls_job_output      TYPE ssfcrescl.

  CONSTANTS:
    lc_formname TYPE tdsfname VALUE 'ZYOURSMARTFORMS'.

  lv_form = tnapr-sform.
  IF lv_form IS INITIAL.
    lv_form = lc_formname.   
  ENDIF.

* Obtain Smartforms Function Module Name
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = lv_form
    IMPORTING
      fm_name            = lv_intern_formname
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.
  IF sy-subrc NE 0.
    MESSAGE ID sy-msgid
      TYPE sy-msgty
      NUMBER sy-msgno
      WITH  sy-msgv1
            sy-msgv2
            sy-msgv3
            sy-msgv4.
  ENDIF.

  IF lv_intern_formname IS NOT INITIAL.

      DATA: lv_devtype TYPE rspoptype.
      CALL FUNCTION 'SSF_GET_DEVICE_TYPE'
        EXPORTING
          i_language             = sy-langu
        IMPORTING
          e_devtype              = lv_devtype
        EXCEPTIONS
          no_language            = 1
          language_not_installed = 2
          no_devtype_found       = 3
          system_error           = 4
          OTHERS                 = 5.

      ls_output_options-tdprinter = lv_devtype.
      ls_output_options-tddest = 'LOCL'.
      ls_output_options-tdimmed = 'X'.
      ls_control-no_dialog = 'X'.
      ls_control-getotf = 'X'.

*   Print Smartforms
    CALL FUNCTION lv_intern_formname
      EXPORTING
        control_parameters = ls_control
        user_settings      = ''
        output_options     = ls_output_options
        iw_header          = uw_header
      IMPORTING
        job_output_info    = ls_job_output
      TABLES
        tt_item            = ut_item
      EXCEPTIONS
        formatting_error   = 1
        internal_error     = 2
        send_error         = 3
        user_canceled      = 4
        OTHERS             = 5.
    IF sy-subrc NE 0.
      MESSAGE ID sy-msgid
        TYPE sy-msgty
        NUMBER sy-msgno
        WITH  sy-msgv1
              sy-msgv2
              sy-msgv3
              sy-msgv4.
    ENDIF.
  ENDIF.


  PERFORM f_send_email USING uw_header ls_job_output-otfdata.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form f_send_email
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> LS_JOB_OUTPUT
*&---------------------------------------------------------------------*
FORM f_send_email USING uw_header TYPE gty_header
                          ut_otfdata TYPE ssfcrescl-otfdata.

  DATA: lo_send_request   TYPE REF TO cl_bcs,
        lt_lines          TYPE tline_tab,
        lv_size           TYPE i,
        lv_message        TYPE string,
        lv_bin            TYPE xstring,
        lo_document       TYPE REF TO cl_document_bcs,
        lv_filename       TYPE sood-objdes,
        lt_binary_content TYPE solix_tab,
        lv_bin_content    LIKE LINE OF lt_binary_content,
        lt_text           TYPE bcsy_text,
        ls_text           LIKE LINE OF lt_text,
        lv_subject        TYPE so_obj_des,
        lv_dear           TYPE char60,
        lv_email          TYPE adr6-smtp_addr.

  DATA: ls_err     TYPE REF TO cx_send_req_bcs,
        ls_err_adr TYPE REF TO cx_address_bcs,
        ls_err_doc TYPE REF TO cx_document_bcs.

  DEFINE _add_body.
    ls_text-line = &1.
    APPEND ls_text TO lt_text.
  END-OF-DEFINITION.

  " convert smartforms pdf to binary content
  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
    IMPORTING
      bin_filesize          = lv_size
      bin_file              = lv_bin
    TABLES
      otf                   = ut_otfdata
      lines                 = lt_lines
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      err_bad_otf           = 4
      OTHERS                = 5.
  lt_binary_content = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_bin ).

  " initial
  TRY.
      lo_send_request = cl_bcs=>create_persistent( ).
    CATCH cx_send_req_bcs INTO ls_err.
      lv_message = ls_err->get_longtext( ).
  ENDTRY.

  " set body
  _add_body '<HTML><BODY>'.
  lv_dear = `Dear Recipient,`.
  _add_body lv_dear.
  _add_body '<BR><BR>'.
  _add_body 'Please find the Smartforms in the attachment'.
  _add_body '</BODY></HTML>'.

  " set subject
  lv_subject = `YOUR SUBJECT`.

  " create & set document
  TRY.
      lo_document = cl_document_bcs=>create_document( EXPORTING i_type = 'HTM' i_text = lt_text i_subject = lv_subject ).
    CATCH cx_document_bcs INTO ls_err_doc.
      lv_message = ls_err_doc->get_longtext( ).
  ENDTRY.

  TRY.
      lo_send_request->set_document( lo_document ).
    CATCH cx_send_req_bcs INTO ls_err.
      lv_message = ls_err->get_longtext( ).
  ENDTRY.

  " set attachment
  lv_filename = 'filename'.
  TRY.
      lo_document->add_attachment( EXPORTING
      i_attachment_type = 'PDF'
      i_attachment_subject = lv_filename
      i_att_content_hex = lt_binary_content ).
    CATCH cx_document_bcs INTO DATA(lx_document_bcs).
  ENDTRY.

  " set sender
  TRY.
      lv_email = 'emailsender@xxx.com'.
      DATA(lo_sender) = cl_cam_address_bcs=>create_internet_address( i_address_string = lv_email
                                                                     i_address_name = lv_email ).
    CATCH cx_address_bcs INTO ls_err_adr.
      lv_message = ls_err_adr->get_longtext( ).
  ENDTRY.
  TRY.
      lo_send_request->set_sender( EXPORTING i_sender = lo_sender ).
    CATCH cx_send_req_bcs INTO ls_err.
      lv_message = ls_err->get_longtext( ).
  ENDTRY.

  " set recipient email
  PERFORM f_add_recipient USING lo_send_request 'recipient@xxx.com' '' ''. " to
  PERFORM f_add_recipient USING lo_send_request 'cc@xxx.com' '' 'X'. " CC
  PERFORM f_add_recipient USING lo_send_request '' 'user_sap' 'X'. " CC using SAP user

  " set immediately
  TRY.
      CALL METHOD lo_send_request->set_send_immediately EXPORTING i_send_immediately = 'X'.
    CATCH cx_send_req_bcs INTO ls_err.
      lv_message = ls_err->get_longtext( ).
  ENDTRY.

  " send email
  TRY.
      lo_send_request->send( EXPORTING i_with_error_screen = 'X' ).
      COMMIT WORK.
    CATCH cx_send_req_bcs INTO ls_err.
      lv_message = ls_err->get_longtext( ).
  ENDTRY.

ENDFORM.


*&---------------------------------------------------------------------*
*& Form f_add_recipient
*&---------------------------------------------------------------------*
*&      --> LO_SEND_REQUEST
*&      --> UW_HEADER_EMAIL
*&      --> UW_HEADER_PO_CREATOR
*&      --> P_
*&---------------------------------------------------------------------*
FORM f_add_recipient USING po_bcs TYPE REF TO cl_bcs
                               p_email
                               p_user
                               p_cc TYPE os_boolean.

  DATA:
    lv_email   TYPE adr6-smtp_addr,
    lv_uname   TYPE uname,
    lv_message TYPE string.

  TRY.
      lv_email = p_email.
      lv_uname = p_user.
      DATA(lo_recipient) = SWITCH #( p_user WHEN '' THEN cl_cam_address_bcs=>create_internet_address( lv_email )
                                                    ELSE cl_cam_address_bcs=>create_user_home_address( lv_uname ) ).
    CATCH cx_address_bcs INTO DATA(ls_err_adr).
      lv_message = ls_err_adr->get_longtext( ).
  ENDTRY.

  TRY.
      po_bcs->add_recipient( EXPORTING i_recipient = lo_recipient i_express = 'X' i_copy = p_cc ).
    CATCH cx_send_req_bcs INTO DATA(ls_err).
      lv_message = ls_err->get_longtext( ).
  ENDTRY.

ENDFORM.

Post navigation

SAP ABAP – Macro #1
SAP ABAP – Get Working Schedule

Leave a Reply Cancel reply

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

Recent Posts

  • SAP ABAP – Generate Fiori URL
  • SAP ABAP – Workflow Agents CDS
  • SAP ABAP – Workflow Level with Table Function
  • SAP ABAP – Download ALV to Excel with Total and Subtotal
  • SAP ABAP – BDC Template

Recent Comments

  1. SAP ABAP – Simple Interface FTP Inbound (SAP Consume File From FTP) – SAP ABAP DWIMAN on SAP ABAP – String Encode & Decode BASE64
  2. Upload file – SAP ABAP DWIMAN on F4 Search Help File

Archives

  • May 2025
  • August 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • May 2023
  • April 2023

Categories

  • Uncategorized
©2026 SAP ABAP DWIMAN | WordPress Theme by SuperbThemes.com