Smartform Email

Send Smartform as an email directly during the call to smartform’s function module.

During the call to smartform’s function module, you can send the output of smartform as an email to the recipient. The recipient will get the smartform output as an attachment in the email.

For the purpose of this blog I am using this simple smartform with an image and text.

sap smartform sap logo

Below is source code of class with method EMAIL_SMARTFORM which contains code to call smartform function module in a way that it sends forms output as email.

CLASS zcl_form_email DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    CLASS-METHODS email_smartform .
  PROTECTED SECTION.
  PRIVATE SECTION.

    CLASS-METHODS get_app_rec
      IMPORTING
        !iv_receiver      TYPE so_recname
      RETURNING
        VALUE(rs_rec_obj) TYPE swotobjid .
    CLASS-METHODS get_sen_obj
      RETURNING
        VALUE(rs_sen_obj) TYPE swotobjid .
    CLASS-METHODS get_smartform_name
      IMPORTING
        !iv_form_name     TYPE tdsfname
      RETURNING
        VALUE(rv_fm_name) TYPE rs38l_fnam .
ENDCLASS.



CLASS ZCL_FORM_EMAIL IMPLEMENTATION.


* <SIGNATURE>-----------------------------------------------------------------------------------+
* | Static Public Method ZCL_FORM_EMAIL=>EMAIL_SMARTFORM
* +---------------------------------------------------------------------------------------------+
* +----------------------------------------------------------------------------------</SIGNATURE>
  METHOD email_smartform.

    DATA : ls_mail_app_obj       TYPE  swotobjid,
           ls_mail_rec_obj       TYPE  swotobjid,
           ls_mail_sen_obj       TYPE  swotobjid,
           ls_output_options     TYPE ssfcompop,
           ls_control_parameters TYPE  ssfctrlop,
           lv_fm_name            TYPE rs38l_fnam.

    ls_mail_rec_obj = get_app_rec( iv_receiver = 'abc@xyz.com' ) .
    ls_mail_sen_obj = get_sen_obj( ) .

    CONCATENATE 'Form Email Subject' sy-datum INTO ls_output_options-tdtitle
    SEPARATED BY space.

    ls_control_parameters-device = 'MAIL'.

    lv_fm_name = zcl_form_email=>get_smartform_name( iv_form_name = 'ZPWSMARTFORMS' ).

    CALL FUNCTION lv_fm_name
      EXPORTING
        control_parameters = ls_control_parameters
        mail_appl_obj      = ls_mail_app_obj
        mail_recipient     = ls_mail_rec_obj
        mail_sender        = ls_mail_sen_obj
        output_options     = ls_output_options
        user_settings      = space
      EXCEPTIONS
        formatting_error   = 1
        internal_error     = 2
        send_error         = 3
        user_canceled      = 4.

    IF sy-subrc <> 0.
      RETURN .
    ENDIF.

    COMMIT WORK .

  ENDMETHOD.


* <SIGNATURE>-----------------------------------------------------------------------------------+
* | Static Private Method ZCL_FORM_EMAIL=>GET_APP_REC
* +---------------------------------------------------------------------------------------------+
* | [--->] IV_RECEIVER                    TYPE        SO_RECNAME
* | [<-()] RS_REC_OBJ                     TYPE        SWOTOBJID
* +----------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_app_rec.

    DATA lv_mailaddr TYPE so_name .

    lv_mailaddr = iv_receiver .
    CALL FUNCTION 'CREATE_RECIPIENT_OBJ_PPF'
      EXPORTING
        ip_mailaddr       = lv_mailaddr
        ip_type_id        = 'U'
      IMPORTING
        ep_recipient_id   = rs_rec_obj
      EXCEPTIONS
        invalid_recipient = 1
        OTHERS            = 2.

  ENDMETHOD.


* <SIGNATURE>-----------------------------------------------------------------------------------+
* | Static Private Method ZCL_FORM_EMAIL=>GET_SEN_OBJ
* +---------------------------------------------------------------------------------------------+
* | [<-()] RS_SEN_OBJ                     TYPE        SWOTOBJID
* +----------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_sen_obj.

    CALL FUNCTION 'CREATE_SENDER_OBJECT_PPF'
      EXPORTING
        ip_sender      = sy-uname
      IMPORTING
        ep_sender_id   = rs_sen_obj
      EXCEPTIONS
        invalid_sender = 1
        OTHERS         = 2.

  ENDMETHOD.


* <SIGNATURE>-----------------------------------------------------------------------------------+
* | Static Private Method ZCL_FORM_EMAIL=>GET_SMARTFORM_NAME
* +---------------------------------------------------------------------------------------------+
* | [--->] IV_FORM_NAME                   TYPE        TDSFNAME
* | [<-()] RV_FM_NAME                     TYPE        RS38L_FNAM
* +----------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_smartform_name.

    CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
      EXPORTING
        formname           = iv_form_name
      IMPORTING
        fm_name            = rv_fm_name
      EXCEPTIONS
        no_form            = 1
        no_function_module = 2
        OTHERS             = 3.

    IF sy-subrc <> 0.
    ENDIF.

  ENDMETHOD.
ENDCLASS.

Testing class in SE24 by calling method EMAIL_SMARTFOM.

I can see the email in SOST.

And received in outlook

One Reply to “Smartform Email

Leave a Reply