signed

QiShunwang

“诚信为本、客户至上”

ABAP 发送内部邮件给用户【函数】

2021/1/28 12:20:59   来源:

导语:最近码农小夏和我说,想在一些自动的作业里加发送邮件功能,就是自动作业报错的时候,给用户发送系统邮件,避免报错石沉大海没人关注,这里就涉及到用代码给用户发邮件而非工作台里面发送,我翻了一些资料,借鉴了项目中老顾问的一些代码,封装了一个函数。

字段描述
函数名称ZYH_SEND_EMAIL邮件发送
函数入参L_TITLE邮件的主题
ADD_TITLE附件名称
IV_REC_TYPE收件人类型的说明
表结构IT_CONTXT文本内容
IT_SMTP_ADDR邮件提醒功能-电子邮件
IT_ADD附件内表

函数代码

FUNCTION ZYH_SEND_EMAIL .
*"--------------------------------------------------------------------
*"*"局部接口:
*"  IMPORTING
*"     REFERENCE(L_TITLE) TYPE  SODOCCHGI1-OBJ_DESCR
*"     REFERENCE(ADD_TITLE) TYPE  SOLISTI1-LINE OPTIONAL
*"     REFERENCE(IV_REC_TYPE) TYPE  SO_ESCAPE DEFAULT 'U'
*"  EXPORTING
*"     REFERENCE(ES_MSG) TYPE  ZSBC_MSG
*"  TABLES
*"      IT_CONTXT STRUCTURE  SOLISTI1
*"      IT_SMTP_ADDR STRUCTURE  ZSMSG_C1
*"      IT_ADD OPTIONAL
*"--------------------------------------------------------------------

  DATA: ls_docdata   TYPE sodocchgi1, "邮件抬头信息
        lt_packlist  TYPE TABLE OF sopcklsti1 WITH HEADER LINE, "传输内容格式
*      it_contxt    TYPE TABLE OF solisti1   WITH HEADER LINE, "邮件正文
        lt_conhex    TYPE TABLE OF solix      WITH HEADER LINE, "二进制内容
        lt_objbin    TYPE TABLE OF solisti1   WITH HEADER LINE, "附件内容
        lt_objheader TYPE TABLE OF solisti1   WITH HEADER LINE, "附件名称
        lt_receivers TYPE TABLE OF somlreci1  WITH HEADER LINE. "收件人信息

  DATA: lv_tablines TYPE i,
        lv_string   TYPE string,
        lv_mimetype TYPE char64 VALUE 'APPLICATION/MSEXCEL;charset=utf-16le',
        lv_xattach  TYPE xstring.

*  DATA: IT_ADD TYPE TABLE OF spfli WITH HEADER LINE.

  CONSTANTS:
    lc_tab  TYPE c VALUE cl_bcs_convert=>gc_tab,  "CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
    lc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf. "CL_ABAP_CHAR_UTILITIES=>CR_LF



"邮件主题
  CLEAR ls_docdata.
  ls_docdata-obj_name  = l_title.        "文档名称 名称随便取
  ls_docdata-obj_descr = l_title. "邮件的主题

*添加邮件正文内容格式
  CLEAR lv_tablines.
  DESCRIBE TABLE it_contxt LINES lv_tablines.
  READ     TABLE it_contxt INDEX lv_tablines.
  ls_docdata-doc_size = ( lv_tablines - 1 ) * 255 + strlen( it_contxt ). "文档的大小

  CLEAR:lt_packlist,lt_packlist[].
  lt_packlist-head_start = 1.
  lt_packlist-head_num   = 0.
  lt_packlist-body_start = 1.
  lt_packlist-body_num   = lv_tablines.
  lt_packlist-doc_type   = 'RAW'.
  APPEND lt_packlist.

"收件人
  CLEAR:lt_receivers,lt_receivers[].
  LOOP AT it_smtp_addr INTO DATA(is_smtp_addr).
    lt_receivers-receiver = is_smtp_addr-smtp_addr.  "接收人邮箱
*    lt_receivers-rec_type = 'U'.               "收件人类型,具体可看字段的域值
    lt_receivers-rec_type = iv_rec_type.
    lt_receivers-express  = 'X'.              "快件
    APPEND lt_receivers.
  ENDLOOP.

***附件内容(内表内容做附件)
  FIELD-SYMBOLS : <is_add>,<fs_l>.
  IF it_add[] IS NOT INITIAL AND add_title IS NOT INITIAL.
    LOOP AT it_add ASSIGNING <is_add>.
      DO.
        ASSIGN COMPONENT sy-index OF STRUCTURE <is_add> TO <fs_l>.
        IF sy-subrc = 0.
          lv_string = lv_string && <fs_l> && lc_tab.
        ELSE.
          lv_string = lv_string && lc_crlf.
          EXIT.
        ENDIF.
      ENDDO.
    ENDLOOP.

* Convert string to xstring type
* 'APPLICATION/MSEXCEL;charset=utf-16le'
    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text     = lv_string
        mimetype = lv_mimetype
      IMPORTING
        buffer   = lv_xattach
      EXCEPTIONS
        failed   = 1
        OTHERS   = 2.

* Add the file header for utf-16le. .
    IF sy-subrc = 0.
      CONCATENATE cl_abap_char_utilities=>byte_order_mark_little
      lv_xattach INTO lv_xattach IN BYTE MODE.
    ENDIF.

    CLEAR:lt_conhex,lt_conhex[].
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer     = lv_xattach
      TABLES
        binary_tab = lt_conhex.

"设置附件名称
    lt_objheader-line = add_title && 'XLSX'.
    APPEND lt_objheader.

"计算附件大小分配空间
    CLEAR lv_tablines.
    DESCRIBE TABLE it_add LINES lv_tablines.
    CLEAR lt_packlist.
    lt_packlist-transf_bin = 'X'.
    lt_packlist-head_start = 1.
    lt_packlist-head_num   = 0.
    lt_packlist-body_start = 1.
    lt_packlist-body_num   = lv_tablines.
    lt_packlist-doc_type   = 'XLSX'.
    lt_packlist-obj_name   = 'ATTACHMENT'.
    lt_packlist-obj_descr  = add_title.
    lt_packlist-doc_size   =  lv_tablines * 255.
    APPEND lt_packlist.

  ENDIF.

"发送邮件
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = ls_docdata "邮件标题
      put_in_outbox              = 'X'
      commit_work                = 'X'
* IMPORTING
*     SENT_TO_ALL                =
*     NEW_OBJECT_ID              =
    TABLES
      packing_list               = lt_packlist "传输内容格式
      object_header              = lt_objheader "附件名称
*     CONTENTS_BIN               =
      contents_txt               = it_contxt "邮件文字内容
      contents_hex               = lt_conhex "二进制内容
*     OBJECT_PARA                =
*     OBJECT_PARB                =
      receivers                  = lt_receivers "接收人列表
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.

  IF sy-subrc NE 0.
    es_msg-type = 'E'.
    es_msg-message = '邮件发送失败'.
  ELSE.
" 如果不自动送,加上执行邮件立即发送程序代码
    SUBMIT rsconn01 WITH mode = 'INT'
                    WITH output = '' "显示发送结果  可以使用空
                    AND RETURN.

    es_msg-type = 'S'.
    es_msg-message = '邮件发送成功'.
  ENDIF.

ENDFUNCTION.

测试

*&---------------------------------------------------------------------*
*& Report ZYH_EMAIL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zyh_email.


DATA lt_contxt LIKE TABLE OF solisti1 WITH HEADER LINE.
DATA lt_smtp_addr LIKE TABLE OF zsmsg_c1 WITH HEADER LINE.
DATA lv_title TYPE sodocchgi1-obj_descr.


lv_title = '测试'.
lt_contxt-line = '邮件测试'.
APPEND lt_contxt.
lt_smtp_addr-smtp_addr = 'CABAP03'.
APPEND lt_smtp_addr.

CALL FUNCTION 'ZYH_SEND_EMAIL'
  EXPORTING
    l_title      = lv_title     "标题
*   ADD_TITLE    =
    iv_rec_type  = 'B'          "类型
*       IMPORTING
*   ES_MSG       =
  TABLES
    it_contxt    = lt_contxt     "内容
    it_smtp_addr = lt_smtp_addr  "收件人
*   IT_ADD       =               "附件
  .

在这里插入图片描述
在这里插入图片描述
作者:小飞猪猪猪猪猪猪猪–CSDN
注意:转发请注明作者