Code:
______________________________________________________________________________
**** Global Data declarations ****
* Internal tables and workarea
DATA:
i_final TYPE STANDARD TABLE OF <Enter the final table structure name>,
wa_final TYPE <Enter the structure name>,
i_list_content TYPE solix_tab,
i_recievers TYPE STANDARD TABLE OF somlreci1,
i_contents_txt TYPE STANDARD TABLE OF solisti1,
wa_contents_txt TYPE solisti1,
i_packinglist TYPE STANDARD TABLE OF sopcklsti1,
wa_packinglist TYPE sopcklsti1,
i_contents_bin TYPE STANDARD TABLE OF solisti1,
wa_contents_bin TYPE solisti1,
i_objectheader TYPE STANDARD TABLE OF solisti1,
wa_objectheader TYPE solisti1,
wa_documentdata TYPE sodocchgi1.
* For HTML
DATA:
i_header TYPE STANDARD TABLE OF w3head WITH HEADER LINE, "Header
i_fields TYPE STANDARD TABLE OF w3fields WITH HEADER LINE, "Fields
i_html TYPE STANDARD TABLE OF w3html, "Html
wa_header TYPE w3head,
wa_head TYPE w3head.
* Variables
DATA:
v_count TYPE i,
v_mainlines TYPE i,
v_attachlines TYPE i.
**** Send Mail ****
* Note:
* Populate I_FINAL with details that will be output in attachment.
* Pass atleast one parameter among Distribution List and Email ID.
PERFORM send_mail USING: <Enter Distribution List>
<Enter Email ID>
'<Enter Mail Subject text>'.
*&---------------------------------------------------------------------*
*& Form SEND_MAIL
*&---------------------------------------------------------------------*
* Send mail with attachment
*----------------------------------------------------------------------*
* -->I_FINAL Final Table containing mail content
* -->P_DLIST Distribution List
* -->P_EMAIL Email ID
* -->P_SUBJECT Mail Subject
*----------------------------------------------------------------------*
FORM send_mail USING p_dlist TYPE soid-objnam
p_email TYPE somlreci1-receiver
p_subject TYPE char100.
**** Local Data Declaration ****
DATA:
v_string TYPE string,
v_size TYPE so_obj_len.
**** Initialize variables and internal tables ****
CLEAR : v_string, v_size.
REFRESH: i_recievers, i_list_content, i_objectheader,
i_contents_txt,i_packinglist, i_contents_bin.
**** Build the recievers table ****
PERFORM set_mail_id USING: p_dlist
p_email.
* Exit if no recievers
CHECK i_recievers[] IS NOT INITIAL.
**** Set document header ****
CLEAR wa_documentdata.
wa_documentdata-obj_descr = p_subject.
**** Set Object Header ****
CLEAR wa_objectheader.
wa_objectheader = p_subject.
APPEND wa_objectheader TO i_objectheader.
**** Build mail contents table ****
PERFORM build_mail_content USING:
'Please find the attached document.'.
* Get the number of lines
DESCRIBE TABLE i_contents_txt LINES v_mainlines.
**** Set packing list for body of email ****
CLEAR wa_packinglist.
wa_packinglist-transf_bin = space.
wa_packinglist-head_start = 1.
wa_packinglist-head_num = 0.
wa_packinglist-body_start = 1.
wa_packinglist-body_num = v_mainlines..
wa_packinglist-doc_type = 'RAW'.
CLEAR wa_contents_txt.
READ TABLE i_contents_txt INTO wa_contents_txt
INDEX v_mainlines.
wa_documentdata-doc_size =
( ( v_mainlines - 1 ) * 255 + STRLEN( wa_contents_txt ) ).
APPEND wa_packinglist TO i_packinglist.
**** Populate attachment content ****
PERFORM fill_attachment_table.
* Get the number of lines
DESCRIBE TABLE i_contents_bin LINES v_attachlines.
**** Set packing list for attachment ****
CLEAR wa_packinglist.
wa_packinglist-transf_bin = 'X'.
wa_packinglist-head_start = 1.
wa_packinglist-head_num = 0.
wa_packinglist-body_start = 1.
wa_packinglist-body_num = v_attachlines.
wa_packinglist-obj_descr = wa_documentdata-obj_descr.
wa_packinglist-doc_type = 'HTM'.
**** Convert data into binary ****
TRY.
* take attachment data into string
CLEAR wa_contents_bin.
LOOP AT i_contents_bin INTO wa_contents_bin.
CONCATENATE v_string wa_contents_bin INTO v_string.
ENDLOOP.
* Convert into binary
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = v_string
iv_add_bom = 'X'
IMPORTING
et_solix = i_list_content
ev_size = v_size ).
CATCH cx_bcs.
MESSAGE s001(00) WITH 'Error when transfering document contents - Email'.
ENDTRY.
wa_packinglist-doc_size = v_size.
APPEND wa_packinglist TO i_packinglist.
*Note: For adding more attachments. Append data into i_list_content
* & uncomment the following code
* v_attachlines2 = v_size - v_attachlines.
* v_start2 = v_attachlines + 1.
* CLEAR wa_packinglist.
* wa_packinglist-transf_bin = 'X'.
* wa_packinglist-head_start = 1.
* wa_packinglist-head_num = 0.
* wa_packinglist-body_start = v_start2.
* wa_packinglist-body_num = v_attachlines2.
* wa_packinglist-obj_descr = '<Enter the file name>'.
* wa_packinglist-doc_type = '<Enter the file type>'.
* wa_packinglist-doc_size = <Enter the size>.
* APPEND wa_packinglist TO i_packinglist.
**** Send emails ****
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = wa_documentdata
* PUT_IN_OUTBOX = ' '
commit_work = 'X'
* IMPORTING
* SENT_TO_ALL =
* NEW_OBJECT_ID =
TABLES
packing_list = i_packinglist
object_header = i_objectheader
* CONTENTS_BIN =
contents_txt = i_contents_txt
contents_hex = i_list_content
* OBJECT_PARA =
* OBJECT_PARB =
receivers = i_recievers
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 <> 0.
MESSAGE e001(00) WITH 'Distribution of the report failed'.
ELSE.
MESSAGE s001(00) WITH 'Email sent successfully'.
ENDIF.
ENDFORM. " SEND_MAIL
*&---------------------------------------------------------------------*
*& Form SET_MAIL_ID
*&---------------------------------------------------------------------*
* Build the recievers table which will contain the mail IDs
*----------------------------------------------------------------------*
* -->P_P_DLIST Distribution list
* -->P_P_EMAIL Email ID
*----------------------------------------------------------------------*
FORM set_mail_id USING p_p_dlist TYPE soid-objnam
p_p_email TYPE somlreci1-receiver.
**** Local Data Declaration ****
DATA: wa_recievers TYPE somlreci1.
* Populate Distribution List
IF NOT p_p_dlist IS INITIAL.
CLEAR: wa_recievers.
wa_recievers-receiver = p_p_dlist.
wa_recievers-rec_type = 'C'.
APPEND wa_recievers TO i_recievers.
ENDIF.
* Populate Email ID
IF NOT p_p_email IS INITIAL.
CLEAR: wa_recievers.
wa_recievers-receiver = p_p_email.
wa_recievers-rec_type = 'U'.
wa_recievers-com_type = 'INT'.
APPEND wa_recievers TO i_recievers.
ENDIF.
ENDFORM. " SET_MAIL_ID
*&---------------------------------------------------------------------*
*& Form BUILD_MAIL_CONTENT
*&---------------------------------------------------------------------*
* Populate mail content
*----------------------------------------------------------------------*
* -->P_TEXT Mail content
*----------------------------------------------------------------------*
FORM build_mail_content USING p_text TYPE text72.
CLEAR: wa_contents_txt.
wa_contents_txt-line = p_text.
APPEND wa_contents_txt TO i_contents_txt.
ENDFORM. " BUILD_MAIL_CONTENT
*&---------------------------------------------------------------------*
*& Form FILL_ATTACHMENT_TABLE
*&---------------------------------------------------------------------*
* Populate attachment content
*----------------------------------------------------------------------*
* -->I_FINAL Table containing mail content
*----------------------------------------------------------------------*
FORM fill_attachment_table.
**** Initialize table & workarea ****
REFRESH: i_header, i_fields, i_html.
CLEAR : wa_header, wa_head.
**** Populate column headings ****
PERFORM build_column USING:
'Transaction',
'Program',
'Screen'.
**** Set title ****
wa_header-text = '<Enter the title>'.
wa_header-font = 'Arial'.
wa_header-size = '2'.
**** Convert the Intenal Table details into HTML format ****
REFRESH i_html.
CALL FUNCTION 'WWW_ITAB_TO_HTML'
EXPORTING
table_header = wa_header
TABLES
html = i_html
fields = i_fields
row_header = i_header
itable = i_final.
**** Move data into I_CONTENTS_BIN table ****
APPEND LINES OF i_html TO i_contents_bin.
ENDFORM. " FILL_ATTACHMENT_TABLE
*&---------------------------------------------------------------------*
*& Form BUILD_COLUMN
*&---------------------------------------------------------------------*
* Populate the columns
*----------------------------------------------------------------------*
* -->P_field field name
*----------------------------------------------------------------------*
FORM build_column USING p_field TYPE char20.
**** Local Data Declaration ****
STATICS: v_count TYPE i.
v_count = v_count + 1. "increment the line count
wa_head-text = p_field.
* Populate the Column Headings
CALL FUNCTION 'WWW_ITAB_TO_HTML_HEADERS'
EXPORTING
field_nr = v_count
text = wa_head-text
fgcolor = 'black'
bgcolor = ' '
TABLES
header = i_header.
* Populate Column Properties
CALL FUNCTION 'WWW_ITAB_TO_HTML_LAYOUT'
EXPORTING
field_nr = v_count
fgcolor = 'black'
size = '3'
TABLES
fields = i_fields.
ENDFORM. " BUILD_COLUMN
* Internal tables and workarea
DATA:
i_final TYPE STANDARD TABLE OF <Enter the final table structure name>,
wa_final TYPE <Enter the structure name>,
i_list_content TYPE solix_tab,
i_recievers TYPE STANDARD TABLE OF somlreci1,
i_contents_txt TYPE STANDARD TABLE OF solisti1,
wa_contents_txt TYPE solisti1,
i_packinglist TYPE STANDARD TABLE OF sopcklsti1,
wa_packinglist TYPE sopcklsti1,
i_contents_bin TYPE STANDARD TABLE OF solisti1,
wa_contents_bin TYPE solisti1,
i_objectheader TYPE STANDARD TABLE OF solisti1,
wa_objectheader TYPE solisti1,
wa_documentdata TYPE sodocchgi1.
* For HTML
DATA:
i_header TYPE STANDARD TABLE OF w3head WITH HEADER LINE, "Header
i_fields TYPE STANDARD TABLE OF w3fields WITH HEADER LINE, "Fields
i_html TYPE STANDARD TABLE OF w3html, "Html
wa_header TYPE w3head,
wa_head TYPE w3head.
* Variables
DATA:
v_count TYPE i,
v_mainlines TYPE i,
v_attachlines TYPE i.
**** Send Mail ****
* Note:
* Populate I_FINAL with details that will be output in attachment.
* Pass atleast one parameter among Distribution List and Email ID.
PERFORM send_mail USING: <Enter Distribution List>
<Enter Email ID>
'<Enter Mail Subject text>'.
*&---------------------------------------------------------------------*
*& Form SEND_MAIL
*&---------------------------------------------------------------------*
* Send mail with attachment
*----------------------------------------------------------------------*
* -->I_FINAL Final Table containing mail content
* -->P_DLIST Distribution List
* -->P_EMAIL Email ID
* -->P_SUBJECT Mail Subject
*----------------------------------------------------------------------*
FORM send_mail USING p_dlist TYPE soid-objnam
p_email TYPE somlreci1-receiver
p_subject TYPE char100.
**** Local Data Declaration ****
DATA:
v_string TYPE string,
v_size TYPE so_obj_len.
**** Initialize variables and internal tables ****
CLEAR : v_string, v_size.
REFRESH: i_recievers, i_list_content, i_objectheader,
i_contents_txt,i_packinglist, i_contents_bin.
**** Build the recievers table ****
PERFORM set_mail_id USING: p_dlist
p_email.
* Exit if no recievers
CHECK i_recievers[] IS NOT INITIAL.
**** Set document header ****
CLEAR wa_documentdata.
wa_documentdata-obj_descr = p_subject.
**** Set Object Header ****
CLEAR wa_objectheader.
wa_objectheader = p_subject.
APPEND wa_objectheader TO i_objectheader.
**** Build mail contents table ****
PERFORM build_mail_content USING:
'Please find the attached document.'.
* Get the number of lines
DESCRIBE TABLE i_contents_txt LINES v_mainlines.
**** Set packing list for body of email ****
CLEAR wa_packinglist.
wa_packinglist-transf_bin = space.
wa_packinglist-head_start = 1.
wa_packinglist-head_num = 0.
wa_packinglist-body_start = 1.
wa_packinglist-body_num = v_mainlines..
wa_packinglist-doc_type = 'RAW'.
CLEAR wa_contents_txt.
READ TABLE i_contents_txt INTO wa_contents_txt
INDEX v_mainlines.
wa_documentdata-doc_size =
( ( v_mainlines - 1 ) * 255 + STRLEN( wa_contents_txt ) ).
APPEND wa_packinglist TO i_packinglist.
**** Populate attachment content ****
PERFORM fill_attachment_table.
* Get the number of lines
DESCRIBE TABLE i_contents_bin LINES v_attachlines.
**** Set packing list for attachment ****
CLEAR wa_packinglist.
wa_packinglist-transf_bin = 'X'.
wa_packinglist-head_start = 1.
wa_packinglist-head_num = 0.
wa_packinglist-body_start = 1.
wa_packinglist-body_num = v_attachlines.
wa_packinglist-obj_descr = wa_documentdata-obj_descr.
wa_packinglist-doc_type = 'HTM'.
**** Convert data into binary ****
TRY.
* take attachment data into string
CLEAR wa_contents_bin.
LOOP AT i_contents_bin INTO wa_contents_bin.
CONCATENATE v_string wa_contents_bin INTO v_string.
ENDLOOP.
* Convert into binary
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = v_string
iv_add_bom = 'X'
IMPORTING
et_solix = i_list_content
ev_size = v_size ).
CATCH cx_bcs.
MESSAGE s001(00) WITH 'Error when transfering document contents - Email'.
ENDTRY.
wa_packinglist-doc_size = v_size.
APPEND wa_packinglist TO i_packinglist.
*Note: For adding more attachments. Append data into i_list_content
* & uncomment the following code
* v_attachlines2 = v_size - v_attachlines.
* v_start2 = v_attachlines + 1.
* CLEAR wa_packinglist.
* wa_packinglist-transf_bin = 'X'.
* wa_packinglist-head_start = 1.
* wa_packinglist-head_num = 0.
* wa_packinglist-body_start = v_start2.
* wa_packinglist-body_num = v_attachlines2.
* wa_packinglist-obj_descr = '<Enter the file name>'.
* wa_packinglist-doc_type = '<Enter the file type>'.
* wa_packinglist-doc_size = <Enter the size>.
* APPEND wa_packinglist TO i_packinglist.
**** Send emails ****
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = wa_documentdata
* PUT_IN_OUTBOX = ' '
commit_work = 'X'
* IMPORTING
* SENT_TO_ALL =
* NEW_OBJECT_ID =
TABLES
packing_list = i_packinglist
object_header = i_objectheader
* CONTENTS_BIN =
contents_txt = i_contents_txt
contents_hex = i_list_content
* OBJECT_PARA =
* OBJECT_PARB =
receivers = i_recievers
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 <> 0.
MESSAGE e001(00) WITH 'Distribution of the report failed'.
ELSE.
MESSAGE s001(00) WITH 'Email sent successfully'.
ENDIF.
ENDFORM. " SEND_MAIL
*&---------------------------------------------------------------------*
*& Form SET_MAIL_ID
*&---------------------------------------------------------------------*
* Build the recievers table which will contain the mail IDs
*----------------------------------------------------------------------*
* -->P_P_DLIST Distribution list
* -->P_P_EMAIL Email ID
*----------------------------------------------------------------------*
FORM set_mail_id USING p_p_dlist TYPE soid-objnam
p_p_email TYPE somlreci1-receiver.
**** Local Data Declaration ****
DATA: wa_recievers TYPE somlreci1.
* Populate Distribution List
IF NOT p_p_dlist IS INITIAL.
CLEAR: wa_recievers.
wa_recievers-receiver = p_p_dlist.
wa_recievers-rec_type = 'C'.
APPEND wa_recievers TO i_recievers.
ENDIF.
* Populate Email ID
IF NOT p_p_email IS INITIAL.
CLEAR: wa_recievers.
wa_recievers-receiver = p_p_email.
wa_recievers-rec_type = 'U'.
wa_recievers-com_type = 'INT'.
APPEND wa_recievers TO i_recievers.
ENDIF.
ENDFORM. " SET_MAIL_ID
*&---------------------------------------------------------------------*
*& Form BUILD_MAIL_CONTENT
*&---------------------------------------------------------------------*
* Populate mail content
*----------------------------------------------------------------------*
* -->P_TEXT Mail content
*----------------------------------------------------------------------*
FORM build_mail_content USING p_text TYPE text72.
CLEAR: wa_contents_txt.
wa_contents_txt-line = p_text.
APPEND wa_contents_txt TO i_contents_txt.
ENDFORM. " BUILD_MAIL_CONTENT
*&---------------------------------------------------------------------*
*& Form FILL_ATTACHMENT_TABLE
*&---------------------------------------------------------------------*
* Populate attachment content
*----------------------------------------------------------------------*
* -->I_FINAL Table containing mail content
*----------------------------------------------------------------------*
FORM fill_attachment_table.
**** Initialize table & workarea ****
REFRESH: i_header, i_fields, i_html.
CLEAR : wa_header, wa_head.
**** Populate column headings ****
PERFORM build_column USING:
'Transaction',
'Program',
'Screen'.
**** Set title ****
wa_header-text = '<Enter the title>'.
wa_header-font = 'Arial'.
wa_header-size = '2'.
**** Convert the Intenal Table details into HTML format ****
REFRESH i_html.
CALL FUNCTION 'WWW_ITAB_TO_HTML'
EXPORTING
table_header = wa_header
TABLES
html = i_html
fields = i_fields
row_header = i_header
itable = i_final.
**** Move data into I_CONTENTS_BIN table ****
APPEND LINES OF i_html TO i_contents_bin.
ENDFORM. " FILL_ATTACHMENT_TABLE
*&---------------------------------------------------------------------*
*& Form BUILD_COLUMN
*&---------------------------------------------------------------------*
* Populate the columns
*----------------------------------------------------------------------*
* -->P_field field name
*----------------------------------------------------------------------*
FORM build_column USING p_field TYPE char20.
**** Local Data Declaration ****
STATICS: v_count TYPE i.
v_count = v_count + 1. "increment the line count
wa_head-text = p_field.
* Populate the Column Headings
CALL FUNCTION 'WWW_ITAB_TO_HTML_HEADERS'
EXPORTING
field_nr = v_count
text = wa_head-text
fgcolor = 'black'
bgcolor = ' '
TABLES
header = i_header.
* Populate Column Properties
CALL FUNCTION 'WWW_ITAB_TO_HTML_LAYOUT'
EXPORTING
field_nr = v_count
fgcolor = 'black'
size = '3'
TABLES
fields = i_fields.
ENDFORM. " BUILD_COLUMN