标签:lv return type 发送 lr mail TYPE 邮件
FUNCTION zfmxx_send_email.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(I_SENDER) TYPE ADR6-SMTP_ADDR
*" REFERENCE(I_TITLE) TYPE SO_OBJ_DES
*" REFERENCE(I_HTML) TYPE OS_BOOLEAN DEFAULT ABAP_TRUE
*" REFERENCE(I_SEND_IMMEDIATELY) TYPE OS_BOOLEAN DEFAULT ABAP_TRUE
*" EXPORTING
*" REFERENCE(E_RETURN) TYPE ZSXX003
*" TABLES
*" T_MAIL_TEXT TYPE BCSY_TEXT
*" T_RECIVER TYPE BCSY_SMTPA
*" T_CARBONCOPY TYPE BCSY_SMTPA OPTIONAL
*" T_BCARBONCOPY TYPE BCSY_SMTPA OPTIONAL
*" T_ATTACHMENTS TYPE ZTTXX006 OPTIONAL
*"----------------------------------------------------------------------
*Global data declarations
DATA:lr_send_request TYPE REF TO cl_bcs,
lr_document TYPE REF TO cl_document_bcs,
lr_fail TYPE REF TO cx_bcs,
lr_mail_recipient TYPE REF TO if_recipient_bcs,
lr_mail_sender TYPE REF TO if_sender_bcs.
DATA:lv_mail_addr TYPE adr6-smtp_addr.
DATA:lv_type TYPE so_obj_tp,
lv_result TYPE os_boolean.
DATA:lt_attach_solix TYPE solix_tab,
lv_attach_size TYPE so_obj_len.
DATA:lv_email_text TYPE string.
* 校验部分
IF i_sender IS INITIAL
OR i_title IS INITIAL
OR t_mail_text[] IS INITIAL
OR t_reciver[] IS INITIAL.
e_return-type = 'E'.
e_return-message = TEXT-006. " 必填项为空
RETURN.
ENDIF.
* 处理
IF i_html = abap_true.
lv_type = 'HTM'.
ELSE.
lv_type = 'RAW'.
ENDIF.
* 创建请求
lr_send_request = cl_bcs=>create_persistent( ).
* 创建邮件内容
lr_document = cl_document_bcs=>create_document(
i_type = lv_type
i_text = t_mail_text[]
i_subject = i_title
).
* 添加发件人
lr_mail_sender = cl_cam_address_bcs=>create_internet_address( i_sender ).
lr_send_request->set_sender( i_sender = lr_mail_sender ).
* 添加收件人
LOOP AT t_reciver INTO lv_mail_addr.
IF lv_mail_addr IS INITIAL.
e_return-type = 'E'.
e_return-message = TEXT-007. " 收件人为空(或含有空行)
RETURN.
ENDIF.
"校验邮箱格式是否正确
CLEAR lv_email_text.
lv_email_text = lv_mail_addr.
FIND REGEX '^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$' IN lv_email_text.
IF sy-subrc <> 0.
e_return-type = 'E'.
e_return-message = TEXT-011.
RETURN.
ENDIF.
lr_mail_recipient = cl_cam_address_bcs=>create_internet_address( lv_mail_addr ).
lr_send_request->add_recipient( lr_mail_recipient ).
CLEAR: lv_mail_addr.
ENDLOOP.
* 添加抄送人
LOOP AT t_carboncopy INTO lv_mail_addr.
IF lv_mail_addr IS INITIAL.
e_return-type = 'E'.
e_return-message = TEXT-008. " 抄送人为空(或含有空行)
RETURN.
ENDIF.
"校验邮箱格式是否正确
CLEAR lv_email_text.
lv_email_text = lv_mail_addr.
FIND REGEX '^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$' IN lv_email_text.
IF sy-subrc <> 0.
e_return-type = 'E'.
e_return-message = TEXT-011.
RETURN.
ENDIF.
lr_mail_recipient = cl_cam_address_bcs=>create_internet_address( lv_mail_addr ).
CALL METHOD lr_send_request->add_recipient
EXPORTING
i_recipient = lr_mail_recipient
i_express = 'X'
i_copy = 'X'
i_blind_copy = ' '
i_no_forward = ' '.
CLEAR: lv_mail_addr.
ENDLOOP.
* 添加密送人
LOOP AT t_bcarboncopy INTO lv_mail_addr.
IF lv_mail_addr IS INITIAL.
e_return-type = 'E'.
e_return-message = TEXT-009. " 密送人为空(或含有空行)
RETURN.
ENDIF.
"校验邮箱格式是否正确
CLEAR lv_email_text.
lv_email_text = lv_mail_addr.
FIND REGEX '^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$' IN lv_email_text.
IF sy-subrc <> 0.
e_return-type = 'E'.
e_return-message = TEXT-011.
RETURN.
ENDIF.
lr_mail_recipient = cl_cam_address_bcs=>create_internet_address( lv_mail_addr ).
CALL METHOD lr_send_request->add_recipient
EXPORTING
i_recipient = lr_mail_recipient
"i_express = 'X'
"i_copy = 'X'
i_blind_copy = 'X'
i_no_forward = ' '.
ENDLOOP.
"* 添加附件
LOOP AT t_attachments INTO DATA(ls_attachment).
IF ls_attachment IS INITIAL.
e_return-type = 'E'.
e_return-message = TEXT-010. " 附件为空(或含有空行)
RETURN.
ENDIF.
" 二进制 转换为 solix类型内表
lt_attach_solix = cl_document_bcs=>xstring_to_solix( ls_attachment-file_binary ).
" 数据长度
lv_attach_size = xstrlen( ls_attachment-file_binary ).
" 如果没有指定文件语言则为中文
IF ls_attachment-file_lagu IS INITIAL.
ls_attachment-file_lagu = 1.
ENDIF.
lr_document->add_attachment(
i_attachment_type = ls_attachment-file_type
i_attachment_subject = ls_attachment-file_name
i_attachment_size = lv_attach_size
i_attachment_language = ls_attachment-file_lagu
i_att_content_hex = lt_attach_solix
).
ENDLOOP.
IF e_return-type = 'E'.
RETURN.
ENDIF.
* 添加邮件内容
lr_send_request->set_document( lr_document ).
* 立即发送
IF i_send_immediately = abap_true.
lr_send_request->set_send_immediately( 'X' ).
ENDIF.
* 发送邮件
lv_result = lr_send_request->send( ).
IF lv_result = 'X'.
e_return-type = 'S'.
e_return-message = 'SUCCESS'.
COMMIT WORK.
ELSE.
e_return-type = 'E'.
e_return-message = 'ERROR'.
ROLLBACK WORK.
ENDIF.
ENDFUNCTION.
*Text elements
*----------------------------------------------------------
* 001 删除成功!
* 002 删除失败!
* 004 上载成功!
* 005 上载失败!
* 006 必填项为空
* 007 收件人为空(或含有空行)
* 008 抄送人为空(或含有空行)
* 009 密送人为空(或含有空行)
* 010 附件为空(或含有空行)
* 011 收件人邮箱格式不正确,请检查!
*Messages
*----------------------------------------------------------
*
* Message class: ALSMEX
*037 剪切板内容导入时错误
*
* Message class: Hard coded
* 下载成功!
*
* Message class: ZXX01
*004 用户取消操作!
*005 模板 & 不存在!
*006 下载成功!
*007 下载失败!
描述: 接口返回消息结构 ,表: ZSXX003
行号 | 字段名 | 位置 | 键 | 数据元素 | 域 | 数据类型 | 长度 | 小写 | 域文本 |
1 |
TYPE |
1 |
|
BAPI_MTYPE |
SYCHAR01 |
CHAR |
1 |
|
消息类型: S 成功,E 错误,W 警告,I 信息,A 中断 |
2 |
ID |
2 |
|
SYMSGID |
ARBGB |
CHAR |
20 |
|
消息类 |
3 |
NUMBER |
3 |
|
SYMSGNO |
SYMSGNO |
NUMC |
3 |
|
消息编号 |
4 |
MESSAGE |
4 |
|
BAPI_MSG |
TEXT220 |
CHAR |
220 |
|
消息文本 |
5 |
MESSAGE_V1 |
5 |
|
SYMSGV |
SYCHAR50 |
CHAR |
50 |
|
消息变量 |
6 |
MESSAGE_V2 |
6 |
|
SYMSGV |
SYCHAR50 |
CHAR |
50 |
|
消息变量 |
7 |
MESSAGE_V3 |
7 |
|
SYMSGV |
SYCHAR50 |
CHAR |
50 |
|
消息变量 |
8 |
MESSAGE_V4 |
8 |
|
SYMSGV |
SYCHAR50 |
CHAR |
50 |
|
消息变量 |
描述:附件内容表类型,表:ZTTXX006
行号 | 表类型名 | 行项目结构 | 表类型的类别 | Ranges类型LOW/HIGH组件数据元素类型 | 引用的对象类型 | 表类型的初始行号 | 描述 |
1 |
ZTTXX006 |
ZSXX005 |
|
|
|
00000 |
附件内容表类型 |
表: ZSXX005
描述: 邮件附件信息
行号 | 字段名 | 位置 | 键 | 数据元素 | 域 | 数据类型 | 长度 | 小写 | 域文本 |
1 |
FILE_TYPE |
1 |
|
SO_OBJ_TP |
SO_OBJ_TP |
CHAR |
3 |
|
文档类别代码 |
2 |
FILE_NAME |
2 |
|
SO_OBJ_DES |
SO_OBJ_DES |
CHAR |
50 |
X |
内容的简短描述 |
3 |
FILE_LAGU |
3 |
|
SO_OBJ_LA |
SPRAS |
LANG |
1 |
X |
创建文档使用的语言 |
4 |
FILE_BINARY |
4 |
|
|
|
RSTR |
0 |
|
附件的二进制数据 |
标签:lv,
return,
type,
发送,
lr,
mail,
TYPE,
邮件
From: https://www.cnblogs.com/yy-abaper/p/16914918.html