PERFORM send_report_via_email.
*&---------------------------------------------------------------------*
*& Form send_report_via_email
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM send_report_via_email.
PERFORM build_column_header.
IF p_emsubj IS INITIAL.
p_emsubj = gcf_subjtxt.
ENDIF.
PERFORM build_subject_attach_name
USING
p_emsubj
gcf_emlfile_desc
CHANGING
gds_email_subject
gds_attach_descr.
PERFORM send_email TABLES
p_addres
gdt_email_body
gdt_rept
USING
gds_email_subject
gds_attach_descr
gds_column_headers.
ENDFORM. " send_report_via_email
*&---------------------------------------------------------------------*
*& Form build_subject_attach_name
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_SUBJECT_TEXT text
* -->P_ATTACH_TEXT text
* <--P_EMAIL_SUBJECT text
* <--P_ATTACH_FILENAME text
*----------------------------------------------------------------------*
FORM build_subject_attach_name USING prf_subject_text
prf_attach_text
CHANGING prf_email_subject
prf_attach_descr.
DATA: ldf_date(8) TYPE c,
ldf_time(8) TYPE c.
CLEAR: prf_email_subject,
prf_attach_descr.
WRITE sy-datum TO ldf_date DD/MM/YY.
GET TIME.
WRITE sy-uzeit TO ldf_time USING EDIT MASK '__:__:__'.
* build email subject
CONCATENATE prf_subject_text ldf_date ldf_time
INTO prf_email_subject SEPARATED BY space.
* build attachment file name
CONCATENATE prf_attach_text gcf_undscore sy-datum sy-uzeit
INTO prf_attach_descr.
ENDFORM. " build_subject_attach_name
*&---------------------------------------------------------------------*
*& Form send_email
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->FT_EMAIL_ADDRESS text
* -->FT_EMAIL_BODY text
* -->FT_REPT_DATA text
* -->FP_EMAIL_SUBJECT text
* -->FP_ATTACH_DESCR text
* -->FP_COLUMN_HEADERS text
*----------------------------------------------------------------------*
FORM send_email TABLES prt_email_address STRUCTURE p_addres
prt_email_body STRUCTURE soli
prt_rept_data
USING prt_email_subject LIKE sodocchgi1-obj_descr
prt_attach_descr LIKE sopcklsti1-obj_descr
prt_column_headers LIKE solisti1.
DATA: ldf_count TYPE i,
ldf_attach_cnt TYPE i,
ldf_body_cnt TYPE i,
ldf_data TYPE gts_rept,
ldf_tabix TYPE sytabix,
ldf_chardata TYPE char200,
ldf_mailsubject TYPE sodocchgi1.
DATA: ldt_packing_list TYPE STANDARD TABLE OF sopcklsti1,
lds_packing_list TYPE sopcklsti1.
DATA: ldt_recipients TYPE STANDARD TABLE OF somlreci1,
lts_recipients TYPE somlreci1.
DATA: ldt_rept_data TYPE TABLE OF gts_rept,
lds_rept_data TYPE gts_rept.
DATA: ldt_objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE.
* populate email addresses itab
REFRESH ldt_recipients.
LOOP AT prt_email_address.
CLEAR lts_recipients.
lts_recipients-receiver = prt_email_address-low.
APPEND lts_recipients TO ldt_recipients.
CLEAR prt_email_address.
ENDLOOP.
* populate email attachment itab with tab delimited data
REFRESH gdt_attachment.
* prepare attachment
ldt_rept_data[] = prt_rept_data[].
LOOP AT ldt_rept_data INTO lds_rept_data.
CONCATENATE lds_rept_data-vkorg
lds_rept_data-ktokd
lds_rept_data-kunag
lds_rept_data-namag
lds_rept_data-kunwe
lds_rept_data-namwe
lds_rept_data-name2
lds_rept_data-city
lds_rept_data-state
lds_rept_data-pstcd
lds_rept_data-stadd
lds_rept_data-telno
lds_rept_data-faxno
INTO ldt_objbin
SEPARATED BY gcf_tab.
CONCATENATE ldt_objbin gcf_newline INTO ldt_objbin.
APPEND ldt_objbin.
ENDLOOP.
gdt_attachment[] = ldt_objbin[].
* insert column headers
INSERT prt_column_headers INTO gdt_attachment INDEX 1.
DELETE ldt_recipients WHERE receiver = space.
* check that the recipients table is not empty
IF ldt_recipients[] IS INITIAL.
RAISE no_address_given.
ENDIF.
DESCRIBE TABLE gdt_attachment LINES ldf_attach_cnt.
* populate email recipients table
LOOP AT ldt_recipients INTO lts_recipients
WHERE NOT receiver IS INITIAL.
ldf_tabix = sy-tabix.
lts_recipients-rec_type = gcf_u.
lts_recipients-com_type = gcf_int.
MODIFY ldt_recipients FROM lts_recipients INDEX ldf_tabix.
ENDLOOP.
* populate email subject (document_data) structure
ldf_mailsubject-obj_name = gcf_mailsubject.
ldf_mailsubject-obj_langu = sy-langu.
ldf_mailsubject-obj_descr = prt_email_subject.
ldf_mailsubject-sensitivty = gcf_f.
ldf_mailsubject-doc_size = ( ldf_attach_cnt - 1 ) * 255 +
STRLEN( gdt_attachment ).
* email body
lds_packing_list-transf_bin = space.
lds_packing_list-head_start = 1.
lds_packing_list-head_num = 0.
lds_packing_list-body_start = 1.
lds_packing_list-body_num = ldf_body_cnt.
lds_packing_list-doc_type = gcf_raw.
APPEND lds_packing_list TO ldt_packing_list.
CLEAR lds_packing_list.
* email attachment
lds_packing_list-transf_bin = gcf_x.
lds_packing_list-head_start = 1.
lds_packing_list-head_num = 1.
lds_packing_list-body_start = 1.
lds_packing_list-body_num = ldf_attach_cnt.
lds_packing_list-doc_type = gcf_xls. "text file
lds_packing_list-obj_name = gcf_mailattach.
lds_packing_list-obj_descr = prt_attach_descr.
lds_packing_list-doc_size = lds_packing_list-body_num * 255.
APPEND lds_packing_list TO ldt_packing_list.
CLEAR lds_packing_list.
* Call SAP Office function to send the email out
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = ldf_mailsubject
put_in_outbox = ' '
TABLES
packing_list = ldt_packing_list
contents_bin = gdt_attachment
receivers = ldt_recipients
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.
COMMIT WORK.
* Push mail out from SAP outbox
SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
MESSAGE s000(zev0301) WITH text-m03.
WHEN 1.
MESSAGE s000(zev0301) WITH text-m04.
WHEN 2.
MESSAGE s000(zev0301) WITH text-m05.
WHEN 3.
MESSAGE s000(zev0301) WITH text-m06.
WHEN 4.
MESSAGE s000(zev0301) WITH text-m07.
WHEN OTHERS.
MESSAGE s000(zev0301) WITH text-m08.
ENDCASE.
ENDFORM. " send_email
*&---------------------------------------------------------------------*
*& Form send_report_via_email
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM send_report_via_email.
PERFORM build_column_header.
IF p_emsubj IS INITIAL.
p_emsubj = gcf_subjtxt.
ENDIF.
PERFORM build_subject_attach_name
USING
p_emsubj
gcf_emlfile_desc
CHANGING
gds_email_subject
gds_attach_descr.
PERFORM send_email TABLES
p_addres
gdt_email_body
gdt_rept
USING
gds_email_subject
gds_attach_descr
gds_column_headers.
ENDFORM. " send_report_via_email
*&---------------------------------------------------------------------*
*& Form build_subject_attach_name
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_SUBJECT_TEXT text
* -->P_ATTACH_TEXT text
* <--P_EMAIL_SUBJECT text
* <--P_ATTACH_FILENAME text
*----------------------------------------------------------------------*
FORM build_subject_attach_name USING prf_subject_text
prf_attach_text
CHANGING prf_email_subject
prf_attach_descr.
DATA: ldf_date(8) TYPE c,
ldf_time(8) TYPE c.
CLEAR: prf_email_subject,
prf_attach_descr.
WRITE sy-datum TO ldf_date DD/MM/YY.
GET TIME.
WRITE sy-uzeit TO ldf_time USING EDIT MASK '__:__:__'.
* build email subject
CONCATENATE prf_subject_text ldf_date ldf_time
INTO prf_email_subject SEPARATED BY space.
* build attachment file name
CONCATENATE prf_attach_text gcf_undscore sy-datum sy-uzeit
INTO prf_attach_descr.
ENDFORM. " build_subject_attach_name
*&---------------------------------------------------------------------*
*& Form send_email
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->FT_EMAIL_ADDRESS text
* -->FT_EMAIL_BODY text
* -->FT_REPT_DATA text
* -->FP_EMAIL_SUBJECT text
* -->FP_ATTACH_DESCR text
* -->FP_COLUMN_HEADERS text
*----------------------------------------------------------------------*
FORM send_email TABLES prt_email_address STRUCTURE p_addres
prt_email_body STRUCTURE soli
prt_rept_data
USING prt_email_subject LIKE sodocchgi1-obj_descr
prt_attach_descr LIKE sopcklsti1-obj_descr
prt_column_headers LIKE solisti1.
DATA: ldf_count TYPE i,
ldf_attach_cnt TYPE i,
ldf_body_cnt TYPE i,
ldf_data TYPE gts_rept,
ldf_tabix TYPE sytabix,
ldf_chardata TYPE char200,
ldf_mailsubject TYPE sodocchgi1.
DATA: ldt_packing_list TYPE STANDARD TABLE OF sopcklsti1,
lds_packing_list TYPE sopcklsti1.
DATA: ldt_recipients TYPE STANDARD TABLE OF somlreci1,
lts_recipients TYPE somlreci1.
DATA: ldt_rept_data TYPE TABLE OF gts_rept,
lds_rept_data TYPE gts_rept.
DATA: ldt_objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE.
* populate email addresses itab
REFRESH ldt_recipients.
LOOP AT prt_email_address.
CLEAR lts_recipients.
lts_recipients-receiver = prt_email_address-low.
APPEND lts_recipients TO ldt_recipients.
CLEAR prt_email_address.
ENDLOOP.
* populate email attachment itab with tab delimited data
REFRESH gdt_attachment.
* prepare attachment
ldt_rept_data[] = prt_rept_data[].
LOOP AT ldt_rept_data INTO lds_rept_data.
CONCATENATE lds_rept_data-vkorg
lds_rept_data-ktokd
lds_rept_data-kunag
lds_rept_data-namag
lds_rept_data-kunwe
lds_rept_data-namwe
lds_rept_data-name2
lds_rept_data-city
lds_rept_data-state
lds_rept_data-pstcd
lds_rept_data-stadd
lds_rept_data-telno
lds_rept_data-faxno
INTO ldt_objbin
SEPARATED BY gcf_tab.
CONCATENATE ldt_objbin gcf_newline INTO ldt_objbin.
APPEND ldt_objbin.
ENDLOOP.
gdt_attachment[] = ldt_objbin[].
* insert column headers
INSERT prt_column_headers INTO gdt_attachment INDEX 1.
DELETE ldt_recipients WHERE receiver = space.
* check that the recipients table is not empty
IF ldt_recipients[] IS INITIAL.
RAISE no_address_given.
ENDIF.
DESCRIBE TABLE gdt_attachment LINES ldf_attach_cnt.
* populate email recipients table
LOOP AT ldt_recipients INTO lts_recipients
WHERE NOT receiver IS INITIAL.
ldf_tabix = sy-tabix.
lts_recipients-rec_type = gcf_u.
lts_recipients-com_type = gcf_int.
MODIFY ldt_recipients FROM lts_recipients INDEX ldf_tabix.
ENDLOOP.
* populate email subject (document_data) structure
ldf_mailsubject-obj_name = gcf_mailsubject.
ldf_mailsubject-obj_langu = sy-langu.
ldf_mailsubject-obj_descr = prt_email_subject.
ldf_mailsubject-sensitivty = gcf_f.
ldf_mailsubject-doc_size = ( ldf_attach_cnt - 1 ) * 255 +
STRLEN( gdt_attachment ).
* email body
lds_packing_list-transf_bin = space.
lds_packing_list-head_start = 1.
lds_packing_list-head_num = 0.
lds_packing_list-body_start = 1.
lds_packing_list-body_num = ldf_body_cnt.
lds_packing_list-doc_type = gcf_raw.
APPEND lds_packing_list TO ldt_packing_list.
CLEAR lds_packing_list.
* email attachment
lds_packing_list-transf_bin = gcf_x.
lds_packing_list-head_start = 1.
lds_packing_list-head_num = 1.
lds_packing_list-body_start = 1.
lds_packing_list-body_num = ldf_attach_cnt.
lds_packing_list-doc_type = gcf_xls. "text file
lds_packing_list-obj_name = gcf_mailattach.
lds_packing_list-obj_descr = prt_attach_descr.
lds_packing_list-doc_size = lds_packing_list-body_num * 255.
APPEND lds_packing_list TO ldt_packing_list.
CLEAR lds_packing_list.
* Call SAP Office function to send the email out
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = ldf_mailsubject
put_in_outbox = ' '
TABLES
packing_list = ldt_packing_list
contents_bin = gdt_attachment
receivers = ldt_recipients
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.
COMMIT WORK.
* Push mail out from SAP outbox
SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
MESSAGE s000(zev0301) WITH text-m03.
WHEN 1.
MESSAGE s000(zev0301) WITH text-m04.
WHEN 2.
MESSAGE s000(zev0301) WITH text-m05.
WHEN 3.
MESSAGE s000(zev0301) WITH text-m06.
WHEN 4.
MESSAGE s000(zev0301) WITH text-m07.
WHEN OTHERS.
MESSAGE s000(zev0301) WITH text-m08.
ENDCASE.
ENDFORM. " send_email
No comments:
Post a Comment