ABAP:发送邮件函数
发送邮件:SO_NEW_DOCUMENT_ATT_SEND_API1
这个函数指定当前人为发送人,有时候 我们需要指定发件人所以可以Copy出来自己的函数
ZSO_NEW_DOCUMENT_ATT_SEND_API1:
FUNCTION ZSO_NEW_DOCUMENT_ATT_SEND_API1. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(DOCUMENT_DATA) LIKE SODOCCHGI1 STRUCTURE SODOCCHGI1 *" VALUE(PUT_IN_OUTBOX) LIKE SONV-FLAG DEFAULT SPACE *" VALUE(COMMIT_WORK) LIKE SONV-FLAG DEFAULT SPACE *" EXPORTING *" VALUE(SENT_TO_ALL) LIKE SONV-FLAG *" VALUE(NEW_OBJECT_ID) LIKE SOFOLENTI1-OBJECT_ID *" TABLES *" PACKING_LIST STRUCTURE SOPCKLSTI1 *" OBJECT_HEADER STRUCTURE SOLISTI1 OPTIONAL *" CONTENTS_BIN STRUCTURE SOLISTI1 OPTIONAL *" CONTENTS_TXT STRUCTURE SOLISTI1 OPTIONAL *" CONTENTS_HEX STRUCTURE SOLIX OPTIONAL *" OBJECT_PARA STRUCTURE SOPARAI1 OPTIONAL *" OBJECT_PARB STRUCTURE SOPARBI1 OPTIONAL *" RECEIVERS STRUCTURE SOMLRECI1 *" EXCEPTIONS *" TOO_MANY_RECEIVERS *" DOCUMENT_NOT_SENT *" DOCUMENT_TYPE_NOT_EXIST *" OPERATION_NO_AUTHORIZATION *" PARAMETER_ERROR *" X_ERROR *" ENQUEUE_ERROR *"---------------------------------------------------------------------- DATA:LV_TYPE TYPE SOEXTRECI1-ADR_TYP VALUE 'INT', LV_ADDRESS TYPE SOEXTRECI1-RECEIVER VALUE 'erp@polygroup.com'. CALL FUNCTION 'SO_DOCUMENT_SEND_API1' EXPORTING DOCUMENT_DATA = DOCUMENT_DATA PUT_IN_OUTBOX = PUT_IN_OUTBOX SENDER_ADDRESS = LV_ADDRESS SENDER_ADDRESS_TYPE = LV_TYPE COMMIT_WORK = COMMIT_WORK IMPORTING SENT_TO_ALL = SENT_TO_ALL NEW_OBJECT_ID = NEW_OBJECT_ID TABLES PACKING_LIST = PACKING_LIST OBJECT_HEADER = OBJECT_HEADER CONTENTS_BIN = CONTENTS_BIN CONTENTS_TXT = CONTENTS_TXT CONTENTS_HEX = CONTENTS_HEX OBJECT_PARA = OBJECT_PARA OBJECT_PARB = OBJECT_PARB RECEIVERS = 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. CASE SY-SUBRC. WHEN 0. WHEN 1. RAISE TOO_MANY_RECEIVERS. WHEN 2. RAISE DOCUMENT_NOT_SENT . WHEN 3. RAISE DOCUMENT_TYPE_NOT_EXIST. WHEN 4. RAISE OPERATION_NO_AUTHORIZATION. WHEN 5. RAISE PARAMETER_ERROR. WHEN 7. RAISE ENQUEUE_ERROR . WHEN OTHERS. RAISE X_ERROR. ENDCASE. ENDFUNCTION.
封装了一个函数用来发送邮件及附件,使用起来会方便很多
FUNCTION zsdfu024. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(IV_TITLE) TYPE SODOCCHGI1-OBJ_DESCR OPTIONAL *" VALUE(IV_ADD_TITLE) TYPE SOLISTI1-LINE OPTIONAL *" VALUE(IV_REC_TYPE) TYPE SO_ESCAPE DEFAULT 'U' *" VALUE(IV_HTM) TYPE FLAG OPTIONAL *" EXPORTING *" VALUE(EV_CODE) TYPE CHAR1 *" VALUE(EV_MESSAGE) TYPE CHAR255 *" TABLES *" IT_CONTXT STRUCTURE SOLISTI1 OPTIONAL *" IT_SMTP_ADDR STRUCTURE SOMLRECI1 OPTIONAL *" 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 = iv_title. "文档名称 名称随便取 ls_docdata-obj_descr = iv_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'. IF iv_htm EQ 'X'. lt_packlist-doc_type = 'HTM'. ENDIF. APPEND lt_packlist. "收件人 CLEAR:lt_receivers,lt_receivers[]. LOOP AT it_smtp_addr INTO DATA(is_smtp_addr). lt_receivers-receiver = is_smtp_addr-receiver. "接收人邮箱 * lt_receivers-rec_type = 'U'. "收件人类型,具体可看字段的域值 lt_receivers-rec_type = iv_rec_type. lt_receivers-express = 'X'. "快件 APPEND lt_receivers. ENDLOOP. ***附件内容(内表内容做附件)"标题行 CONCATENATE '项目' '物料代码' '物料描述' '单位' '数量' '委托方(购买方)' '受托方(销售方)' '采购类型' 'PR1(采购申请)' 'PO1(采购订单)' 'SO1(贸易销售单)' 'PR2(采购申请)' 'PO2(采购订单)' 'SO2委托方销售单' '申请人' '接收人' INTO lv_string SEPARATED BY cl_abap_char_utilities=>horizontal_tab. CONCATENATE lv_string cl_abap_char_utilities=>cr_lf INTO lv_string. FIELD-SYMBOLS : <is_add>,<fs_l>. IF it_add[] IS NOT INITIAL AND iv_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 = iv_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 * 5. lt_packlist-doc_type = 'xls'. lt_packlist-obj_name = 'ATTACHMENT'. lt_packlist-obj_descr = iv_add_title. * lt_packlist-doc_size = ( lv_tablines + 1 ) * 10000. APPEND lt_packlist. ENDIF. "发送邮件 CALL FUNCTION 'ZSO_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. ev_code = 'E'. ev_message = '邮件发送失败'. ELSE. " 如果不自动送,加上执行邮件立即发送程序代码 SUBMIT rsconn01 WITH mode = 'INT' WITH output = '' "显示发送结果 可以使用空 AND RETURN. ev_code = 'S'. ev_message = '邮件发送成功'. ENDIF. ENDFUNCTION.
使用案例:
FORM frm_send_mail USING pv_id pv_text pv_title pv_zrfcid. DATA:BEGIN OF ls_data, ebelp TYPE ekpo-ebelp, "项目 matnr TYPE ekpo-matnr, "物料编码 maktx TYPE makt-maktx, "物料描述 meins TYPE ekpo-meins, "单位 menge TYPE ekpo-menge, "数量 lifn1 TYPE ekpa-lifn2, "委托方 lifn2 TYPE ekpa-lifn2, "受托方 bsart TYPE ekko-bsart, "采购类型 banfn1 TYPE ekpo-banfn, "采购申请1 ebeln1 TYPE ekpo-ebeln, "采购订单1 vbeln1 TYPE vbak-vbeln, "销售订单1 banfn2 TYPE ekpo-banfn, "采购申请2 ebeln2 TYPE ekpo-ebeln, "采购订单2 vbeln2 TYPE vbak-vbeln, "SO2 ernam TYPE ekko-ernam, "申请人 zbname TYPE zsdt042-zbname, "接受人 flag TYPE char10,"标记 zrfcid TYPE zsdt008-zrfcid, END OF ls_data, lt_data LIKE TABLE OF ls_data, BEGIN OF ls_bname, bname TYPE zsdt042-bname, END OF ls_bname, lt_bname LIKE TABLE OF ls_bname. DATA:lv_ebeln1 TYPE ebeln, lv_ebeln2 TYPE ebeln, lv_vbeln1 TYPE vbeln, lv_vbeln2 TYPE vbeln. "将业务流程ID数据取出 SELECT * FROM zsdt008 INTO TABLE @DATA(lt_zsdt008) WHERE zrfc_logid EQ @pv_id. LOOP AT lt_zsdt008 INTO DATA(ls_zsdt008). CASE ls_zsdt008-canum. WHEN '0010'. "查询PO1数据 SELECT * INTO TABLE @DATA(lt_ekpo1) FROM ekpo WHERE ebeln EQ @ls_zsdt008-objkey. lv_ebeln1 = ls_zsdt008-objkey. WHEN '0030'. "查询PO2数据 SELECT ebeln,ebelp,banfn INTO TABLE @DATA(lt_ekpo2) FROM ekpo WHERE ebeln EQ @ls_zsdt008-objkey. lv_ebeln2 = ls_zsdt008-objkey. WHEN '0020'. "SO1 lv_vbeln1 = ls_zsdt008-objkey. WHEN '0040'. "SO2 lv_vbeln2 = ls_zsdt008-objkey. WHEN OTHERS. ENDCASE. CLEAR:ls_zsdt008. ENDLOOP. LOOP AT lt_ekpo1 INTO DATA(ls_ekpo1). "项目 ls_data-ebelp = ls_ekpo1-ebelp. "物料编码 ls_data-matnr = ls_ekpo1-matnr. "物料描述 SELECT SINGLE maktx INTO ls_data-maktx FROM makt WHERE matnr EQ ls_data-matnr. "单位 ls_data-meins = ls_ekpo1-meins. "数量 ls_data-menge = ls_ekpo1-menge. "委托方(购买方) SELECT SINGLE lifn2 INTO ls_data-lifn1 FROM ekpa WHERE parvw EQ 'LW' AND ebeln EQ ls_ekpo1-ebeln. "受托方(销售方) SELECT SINGLE lifn2 INTO ls_data-lifn2 FROM ekpa WHERE parvw EQ 'HS' AND ebeln EQ ls_ekpo1-ebeln. "采购类型 SELECT SINGLE bsart INTO ls_data-bsart FROM ekko WHERE ebeln = ls_ekpo1-ebeln. "采购申请 ls_data-banfn1 = ls_ekpo1-banfn. "采购订单1 ls_data-ebeln1 = ls_ekpo1-ebeln. "贸易销售单 READ TABLE lt_zsdt008 INTO ls_zsdt008 WITH KEY canum = '0020'. IF sy-subrc EQ 0. ls_data-vbeln1 = ls_zsdt008-objkey. ENDIF. "采购申请2 READ TABLE lt_ekpo2 INTO DATA(ls_ekpo2) WITH KEY ebelp = ls_ekpo1-ebelp. IF sy-subrc EQ 0. ls_data-banfn2 = ls_ekpo2-banfn. "采购订单2 ls_data-ebeln2 = ls_ekpo2-ebeln. ENDIF. "委托方销售单 READ TABLE lt_zsdt008 INTO ls_zsdt008 WITH KEY canum = '0040'. IF sy-subrc EQ 0. ls_data-vbeln2 = ls_zsdt008-objkey. ENDIF. "申请人 SELECT SINGLE ernam INTO ls_data-ernam FROM ekko WHERE ebeln = ls_ekpo1-ebeln. "接收人 SELECT SINGLE zbname INTO ls_data-zbname FROM zsdt042 WHERE bname EQ sy-uname. "标记 READ TABLE gt_flag INTO gs_flag WITH KEY item = ls_data-ebelp. IF sy-subrc EQ 0. IF gs_flag-flag EQ 'I'. ls_data-flag = '新增'. ELSEIF gs_flag-flag EQ 'U'. ls_data-flag = '修改'. ELSEIF gs_flag-flag EQ 'D'. ls_data-flag = '删除'. ENDIF. ENDIF. IF ls_ekpo1-loekz NE ''. ls_data-flag = '删除'. ENDIF. "业务流程ID ls_data-zrfcid = pv_zrfcid. APPEND ls_data TO lt_data. CLEAR:ls_ekpo1,ls_data. ENDLOOP. "查询当前操作人对应的接受人 SELECT SINGLE * FROM zsdt042 INTO @DATA(ls_zsdt042) WHERE bname EQ @sy-uname AND zrfcid EQ @pv_zrfcid AND zactive EQ 'X'. SPLIT ls_zsdt042-zbname AT ';' INTO TABLE lt_bname. "查询USR21数据 IF lt_bname IS NOT INITIAL. SELECT smtp_addr FROM usr21 AS a INNER JOIN adr6 AS b ON a~persnumber EQ b~persnumber INTO TABLE @DATA(lt_smtp_addr_2) FOR ALL ENTRIES IN @lt_bname WHERE a~bname EQ @lt_bname-bname. ENDIF. DATA lt_contxt LIKE TABLE OF solisti1 WITH HEADER LINE. DATA lt_smtp_addr LIKE TABLE OF somlreci1 WITH HEADER LINE. DATA lv_title TYPE sodocchgi1-obj_descr. DATA lv_str TYPE string. "设置标题 lv_title = sy-datum+4(4) && pv_text && '-' && lv_ebeln1 && '/' && lv_ebeln2 && '/' && lv_vbeln1 && '/' && lv_vbeln2 . "设置收件人邮箱 LOOP AT lt_smtp_addr_2 INTO DATA(ls_smtp_addr). lt_smtp_addr-receiver = ls_smtp_addr-smtp_addr. APPEND lt_smtp_addr. CLEAR:lt_smtp_addr,ls_smtp_addr. ENDLOOP. "设置邮件正文 lt_contxt-line = '<table width="75%" border="1" cellspaceing="0"><tr>'. APPEND lt_contxt.CLEAR:lt_contxt. "表头 lv_str = '<td>项目</td><td>物料代码</td><td>物料描述</td><td>单位</td>' && '<td>数量</td><td>委托方(购买方)</td><td>受托方(销售方)</td>' && '<td>采购类型</td><td>PR1(采购申请)</td><td>PO1(采购订单)</td>'. lt_contxt-line = lv_str. APPEND lt_contxt.CLEAR:lt_contxt,lv_str. lv_str = '<td>SO1贸易销售单</td><td>PR2(采购申请)</td><td>PO2(采购订单)</td>' && '<td>SO2(委托方销售订单)</td><td>申请人</td><td>接受人</td><td>业务流程ID</td><td>操作标识</td></tr>'. lt_contxt-line = lv_str. APPEND lt_contxt.CLEAR:lt_contxt,lv_str. SORT lt_data BY ebelp. LOOP AT lt_data INTO ls_data. CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = ls_data-matnr IMPORTING output = ls_data-matnr. lv_str = '<tr><td>' && ls_data-ebelp && '</td>' && '<td>' && ls_data-matnr && '</td>' && '<td>' && ls_data-maktx && '</td>' && '<td>' && ls_data-meins && '</td>' && '<td>' && ls_data-menge && '</td>' && '<td>' && ls_data-lifn1 && '</td>' && '<td>' && ls_data-lifn2 && '</td>' && '<td>' && ls_data-bsart && '</td>'. lt_contxt-line = lv_str. APPEND lt_contxt.CLEAR:lt_contxt,lv_str. lv_str = '<td>' && ls_data-banfn1 && '</td>' && '<td>' && ls_data-ebeln1 && '</td>' && '<td>' && ls_data-vbeln1 && '</td>' && '<td>' && ls_data-banfn2 && '</td>' && '<td>' && ls_data-ebeln2 && '</td>' && '<td>' && ls_data-vbeln2 && '</td>' && '<td>' && ls_data-ernam && '</td>' && '<td>' && ls_data-zbname && '</td>'. lt_contxt-line = lv_str. APPEND lt_contxt.CLEAR:lt_contxt,lv_str. lv_str = '<td>' && ls_data-zrfcid && '</td>'. lt_contxt-line = lv_str. APPEND lt_contxt.CLEAR:lt_contxt,lv_str. IF pv_text EQ '修改'. lv_str = '<td>' && ls_data-flag && '</td></tr>'. lt_contxt-line = lv_str. APPEND lt_contxt.CLEAR:lt_contxt,lv_str. ENDIF. CLEAR:ls_data. ENDLOOP. lt_contxt-line = '</table>'. APPEND lt_contxt.CLEAR:lt_contxt. IF lt_ekpo1 IS INITIAL. CLEAR:lt_contxt[]. lt_contxt-line = '订单已删除,请知悉'. APPEND lt_contxt.CLEAR:lt_contxt. ENDIF. IF lt_smtp_addr[] IS NOT INITIAL AND lt_contxt[] IS NOT INITIAL. CALL FUNCTION 'ZSDFU024' EXPORTING iv_title = lv_title "标题 iv_rec_type = 'U' "类型 iv_htm = 'X' "是否HTML iv_add_title = pv_title TABLES it_contxt = lt_contxt "内容 it_smtp_addr = lt_smtp_addr "收件人 it_add = lt_data. "附件 ENDIF. ENDFORM.