FUNCTION ZEMAIL_TRIGGER.
*”———————————————————————-
*”*”Local Interface:
*” IMPORTING
*” REFERENCE(CONTENT_SUBJECT) TYPE SO_OBJ_DES
*” REFERENCE(ATTACHMENT_SUBJECT) TYPE SO_OBJ_DES
*” REFERENCE(FILE_TYPE) TYPE SOODK-OBJTP OPTIONAL
*” REFERENCE(NO_HEADER) OPTIONAL
*” TABLES
*” RECEPIENTS TYPE BCSY_SMTPA
*” ITAB
*” HEADER OPTIONAL
*” CON_HEAD OPTIONAL
*”———————————————————————-
type-pools: truxs.
data: l_text type char1024.
data: begin of it_text occurs 0,
text type char1024,
end of it_text.
data: l_lines type i,
l_size type sood-objlen. ” Size of Attachment
* Mail related
data: i_content type soli_tab, ” Mail content
i_attach type soli_tab. ” Attachment
data: l_send_request type ref to cl_bcs, ” E-Mail Send Request
l_document type ref to cl_document_bcs, ” E-Mail Attachment
l_recipient type ref to if_recipient_bcs, ” Distribution List
l_sender type ref to if_sender_bcs, ” Address of Sender
l_uname type salrtdrcpt, ” Sender Name(SY-UNAME)
l_bcs_exception type ref to cx_document_bcs, ” BCS Exception
l_addr_exception type ref to cx_address_bcs, ” Address Exception
l_send_exception type ref to cx_send_req_bcs. ” E-Mail sending Exception
*Constants————————————————————*
constants: c_cr(1) type c value cl_abap_char_utilities=>cr_lf. ” Line Feed for End-Of_line
data: c_tab(1). ” Tab Character
c_tab = cl_abap_char_utilities=>horizontal_tab.
* if file_type = ‘CSV’.
* c_tab = ‘,’.
* endif.
data: c_ext type soodk-objtp value ‘XLS’. ” XLS Extension
if file_type is not initial.
c_ext = file_type.
endif.
* Preparing body of the Mail
move ‘Dear All:’ to l_text.
append l_text to i_content.
move ‘ This Email is generated automatically by SAP. Please do not reply to this email. Kindly contact relevant IT staff.’ to l_text.
append l_text to i_content.
* Preparing contents of attachment with Change Log
field-symbols: <lfs_table>, ” Internal table structure
<lfs_con>. ” Field Content
field-symbols: <alv_table>, ” Internal table structure
<alv_con>. ” Field Content
data: l_con(200) type c. ” Field Content in character format
*** for CSV file
data: csvtab type truxs_t_text_data,
wa_csvtab(4096),
csvtab_new type truxs_t_text_data.
data: begin of tmp occurs 0,
txt(4096),
end of tmp.
data: seperator type c value ‘,’.
data : lv_xstring type xstring.
data : c_index like sy-tabix.
data : temp_binary_attach type solix_tab.
data : temp_text like it_text occurs 0 with header line.
data : it_binary_attach type solix_tab.
if file_type = ‘CSV’.
call function ‘ZFTP_COMBINE_ITAB’
exporting
no_header = no_header
file_type = ‘CSV’
seperator = seperator
* XMLDATA =
tables
it_fieldcat = header[]
itab = itab[]
it_title = con_head[]
binary_tab = it_binary_attach[]
* CHANGING
* CONVERT_TAB =
exceptions
conv_error = 1
others = 2
.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
else.
*&Header text
clear l_text.
refresh it_text.
if con_head[] is not initial.
clear l_text.
loop at con_head assigning <lfs_table>.
loop at header assigning <alv_table>.
assign component ‘FIELDNAME’ of structure <alv_table>
to <alv_con>.
assign component <alv_con> of structure <lfs_table>
to <lfs_con>.
if sy-subrc ne 0.
else.
clear: l_con.
move <lfs_con> to l_con.
condense l_con.
if sy-tabix = 1.
clear: l_text.
move l_con to l_text.
else.
concatenate l_text l_con into l_text
separated by c_tab.
endif.
endif.
at last.
clear it_text-text.
it_text-text = l_text.
append it_text.
clear it_text.
endat.
endloop.
endloop.
endif.
if no_header = ‘ ‘.
loop at header assigning <lfs_table>.
assign component ‘SELTEXT_L’ of structure <lfs_table>
to <lfs_con>.
clear: l_con.
move <lfs_con> to l_con.
condense l_con.
if sy-tabix = 1.
clear: l_text.
move l_con to l_text.
else.
concatenate l_text l_con into l_text
separated by c_tab.
endif.
at last.
clear it_text-text.
it_text-text = l_text.
append it_text.
clear it_text.
endat.
endloop.
endif.
* Columns to be tab delimeted
clear l_text.
loop at itab assigning <lfs_table>.
loop at header assigning <alv_table>.
assign component ‘FIELDNAME’ of structure <alv_table>
to <alv_con>.
assign component <alv_con> of structure <lfs_table>
to <lfs_con>.
if sy-subrc ne 0.
else.
clear: l_con.
move <lfs_con> to l_con.
condense l_con.
if sy-tabix = 1.
clear: l_text.
move l_con to l_text.
else.
concatenate l_text l_con into l_text
separated by c_tab.
endif.
endif.
at last.
clear it_text-text.
it_text-text = l_text.
append it_text.
clear it_text.
endat.
endloop.
endloop.
* Convert text to xstring.
loop at it_text.
c_index = c_index + 1.
append it_text to temp_text.
clear lv_xstring.
call function ‘SCMS_TEXT_TO_XSTRING’
exporting
mimetype = ‘APPLICATION/MSEXCEL;charset=utf-16le’
importing
buffer = lv_xstring
tables
text_tab = temp_text
exceptions
failed = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
* Convert xstring to binary.
clear temp_binary_attach.
refresh temp_binary_attach.
call function ‘SCMS_XSTRING_TO_BINARY’
exporting
buffer = lv_xstring
tables
binary_tab = temp_binary_attach.
append lines of temp_binary_attach to it_binary_attach.
clear temp_text.
refresh temp_text.
clear c_index.
endloop.
endif.
* Creates persistent send request
try.
l_send_request = cl_bcs=>create_persistent( ).
* Creating Document
l_document = cl_document_bcs=>create_document(
i_type = ‘RAW’
i_text = i_content[]
i_subject = content_subject ).
describe table itab lines l_lines.
* Size to multiplied by 2 for UNICODE enabled systems
* Adding Attachment
call method l_document->add_attachment
exporting
i_attachment_type = c_ext
i_attachment_subject = attachment_subject
i_att_content_hex = it_binary_attach[].
* Add document to send request
call method l_send_request->set_document( l_document ).
* Get Sender Object
l_uname = sy-uname.
l_sender = cl_sapuser_bcs=>create( l_uname ).
call method l_send_request->set_sender
exporting
i_sender = l_sender.
* E-Mail
loop at recepients.
translate recepients to lower case.
l_recipient = cl_cam_address_bcs=>create_internet_address( recepients ).
call method l_send_request->add_recipient
exporting
i_recipient = l_recipient
i_express = ‘U’
i_copy = ‘ ‘
i_blind_copy = ‘ ‘
i_no_forward = ‘X’. “是否要回执
endloop.
*Trigger E-Mail immediately
l_send_request->set_send_immediately( ‘X’ ).
call method l_send_request->send( ).
commit work.
catch cx_document_bcs into l_bcs_exception.
catch cx_send_req_bcs into l_send_exception.
catch cx_address_bcs into l_addr_exception.
endtry.
ENDFUNCTION.
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/19570.html