邮件发送smartform(转成PDF)实例

介绍一个项目中经常会遇到的场景:将smartform转化成PDF,然后再把PDF作为邮件的附件发送出去。

1, 创建smartform

Tcode:smartforms创建一个简单的smartform

2017-02-23_10-48-08

添加一个文本节点,

2017-02-23_10-50-10

2, 创建程序

创建一个abap程序,简单逻辑如下:

1,生成smartform保存到spool中,注意控制参数(control_parameters)和输出选项(output_options )的设定

2,调用RSPO_GET_ATTRIBUTES_SPOOLJOB ()取得spool的属性

3,将spool中smartform转换成PDF

4,通过类cl_bcs将PDF作为附件发送邮件

 

代码如下:其中有很多错误处理没有写,大家自己完善吧。。。

REPORT ztest_smartform_mail.

DATA: g_fm_name      TYPE rs38l_fnam.
DATA: git_spoolids TYPE tsfspoolid,
      gwa_spool    TYPE rspoid.
DATA: gwa_result     TYPE ssfcrescl,
      gwa_output     TYPE ssfcompop,
      g_filename     TYPE string,
      g_bin_filesize TYPE i,
      git_lines      TYPE STANDARD TABLE OF tline,
      gwa_control    TYPE ssfctrlop.

* for mail
DATA send_request  TYPE REF TO cl_bcs.
DATA document      TYPE REF TO cl_document_bcs.
DATA recipient     TYPE REF TO if_recipient_bcs.
DATA bcs_exception TYPE REF TO cx_bcs.
DATA sent_to_all   TYPE os_boolean.
DATA pdf_size      TYPE so_obj_len.
DATA pdf_content   TYPE solix_tab.
DATA pdf_xstring   TYPE xstring.
DATA:g_mailto      TYPE ad_smtpadr.

DATA rq       TYPE tsp01.
DATA bin_size TYPE i.
DATA dummy    TYPE TABLE OF rspoattr.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    formname           = 'ZTEST_PDF'
*   VARIANT            = ' '
*   DIRECT_CALL        = ' '
  IMPORTING
    fm_name            = g_fm_name
  EXCEPTIONS
    no_form            = 1
    no_function_module = 2
    OTHERS             = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
gwa_control-no_dialog = 'X'.
gwa_control-langu     = sy-langu.
gwa_output-tdnewid   = 'X'.
gwa_output-tdimmed   = ' '.
gwa_output-tddelete  = ' '.

CALL FUNCTION g_fm_name
  EXPORTING
    control_parameters = gwa_control
    output_options     = gwa_output
  IMPORTING
    job_output_info    = gwa_result
  EXCEPTIONS
    formatting_error   = 1
    internal_error     = 2
    send_error         = 3
    user_canceled      = 4
    OTHERS             = 5.

MOVE gwa_result-spoolids TO git_spoolids.

* get mail address
CALL FUNCTION 'EFG_GEN_GET_USER_EMAIL'
  EXPORTING
    i_uname           = sy-uname
  IMPORTING
    e_email_address   = g_mailto
  EXCEPTIONS
    not_qualified     = 1
    user_not_found    = 2
    address_not_found = 3
    OTHERS            = 4.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

LOOP AT git_spoolids INTO gwa_spool.
*&---------------------------------------------------------------------*
*& Create PDF
*&---------------------------------------------------------------------*
* Create PDF Content
* 1) get attributes of spool request
* 2) convert spool request to PDF dependent on document type
*----------------------------------------------------------------------*
*   ------------ get attributes of spool request ---------------------
  CALL FUNCTION 'RSPO_GET_ATTRIBUTES_SPOOLJOB'
    EXPORTING
      rqident     = gwa_spool
    IMPORTING
      rq          = rq
    TABLES
      attributes  = dummy
    EXCEPTIONS
      no_such_job = 1
      OTHERS      = 2.
  IF sy-subrc <> 0.
*   error handling
    CONTINUE.
  ENDIF.

*   --- convert spool request into PDF, dependent on document type ---
  IF rq-rqdoctype = 'OTF' OR rq-rqdoctype = 'SMART'.
    CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
      EXPORTING
        src_spoolid              = gwa_spool
        no_dialog                = 'X'
        pdf_destination          = 'X'
        no_background            = 'X'
      IMPORTING
        pdf_bytecount            = bin_size
        bin_file                 = pdf_xstring
      EXCEPTIONS
        err_no_otf_spooljob      = 1
        err_no_spooljob          = 2
        err_no_permission        = 3
        err_conv_not_possible    = 4
        err_bad_dstdevice        = 5
        user_cancelled           = 6
        err_spoolerror           = 7
        err_temseerror           = 8
        err_btcjob_open_failed   = 9
        err_btcjob_submit_failed = 10
        err_btcjob_close_failed  = 11
        OTHERS                   = 12.
    IF sy-subrc <> 0.
*   error handling
      CONTINUE.
    ENDIF.
  ELSEIF rq-rqdoctype = 'LIST'.
    CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
      EXPORTING
        src_spoolid              = gwa_spool
        no_dialog                = 'X'
        pdf_destination          = 'X'
        no_background            = 'X'
      IMPORTING
        pdf_bytecount            = bin_size
        bin_file                 = pdf_xstring
      EXCEPTIONS
        err_no_abap_spooljob     = 1
        err_no_spooljob          = 2
        err_no_permission        = 3
        err_conv_not_possible    = 4
        err_bad_destdevice       = 5
        user_cancelled           = 6
        err_spoolerror           = 7
        err_temseerror           = 8
        err_btcjob_open_failed   = 9
        err_btcjob_submit_failed = 10
        err_btcjob_close_failed  = 11
        OTHERS                   = 12.
    IF sy-subrc <> 0.
*   error handling
      CONTINUE.
    ENDIF.
  ELSE.
*   error handling
    CONTINUE.
  ENDIF.
  pdf_size = bin_size.

*&---------------------------------------------------------------------*
*& Send mail
*&---------------------------------------------------------------------*

  TRY.

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

*     -------- create and set document -------------------------------
      pdf_content = cl_document_bcs=>xstring_to_solix( pdf_xstring ).

      document = cl_document_bcs=>create_document(
            i_type    = 'PDF'
            i_hex     = pdf_content
            i_length  = pdf_size
            i_subject = '邮件题目' ).                           "#EC NOTEXT

*     add document object to send request
      send_request->set_document( document ).

*     --------- add recipient (e-mail address) -----------------------

*     create recipient object
      recipient = cl_cam_address_bcs=>create_internet_address( g_mailto ).

*     add recipient object to send request
      send_request->add_recipient( recipient ).

*     ---------- send document ---------------------------------------
      sent_to_all = send_request->send( i_with_error_screen = 'X' ).

      COMMIT WORK.

      IF sent_to_all IS INITIAL.
*       error handling
        CONTINUE.
      ELSE.
        MESSAGE 'send mail successfully' TYPE 'S'.
      ENDIF.

*   ------------ exception handling ----------------------------------
*   replace this rudimentary exception handling with your own one !!!
    CATCH cx_bcs INTO bcs_exception.

  ENDTRY.

ENDLOOP.

运行后,邮件会暂时保存在SOST中,

2017-02-23_14-38-16

手动触发后会发送到邮箱里,也可以等待自动触发,具体等待多久要看SOST中的配置。

收到的邮件就是下面这个样子。。

2017-02-23_14-41-22

打开PDF

2017-02-23_14-42-42

以上。

posted @ 2019-08-30 16:40  真的是很难  阅读(642)  评论(0编辑  收藏  举报