DATA:
gc_subject TYPE so_obj_des.
gv_sent_to_all TYPE os_boolean,
gt_receiver TYPE TABLE OF adr6-smtp_addr,
gv_subject TYPE so_obj_des,
gr_send_request TYPE REF TO cl_bcs,
gr_bcs_exception TYPE REF TO cx_bcs,
gr_recipient TYPE REF TO if_recipient_bcs,
gr_document TYPE REF TO cl_document_bcs.
DATA : it_contents TYPE STANDARD TABLE OF solisti1,
wa_contents TYPE solisti1.
DATA binary_content TYPE solix_tab.
DATA size TYPE so_obj_len.
DATA sent_to_all TYPE os_boolean.
CONSTANTS:
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.
START-OF-SELECTION.
PERFORM create_content.
PERFORM send.
FORM create_content .
"Set Subject
gc_subject = 'Send Email test'.
"Set receiver
gt_receiver = VALUE #( ( 'lee@xx.com' ) ).
PERFORM frm_build_content.
DATA lv_string TYPE string.
DATA ls_t100 TYPE t100.
CONCATENATE 'This Is Just Example Text!' "#EC NOTEXT
gc_crlf gc_crlf
INTO lv_string.
* header line
CONCATENATE lv_string
'MSGID' gc_tab
'MSGNO' gc_tab
'Language' gc_tab "#EC NOTEXT
'Text' gc_crlf "#EC NOTEXT
INTO lv_string.
* data lines
SELECT * FROM t100 INTO ls_t100
WHERE arbgb = 'SO' AND msgnr = '182'.
CONCATENATE lv_string
ls_t100-arbgb gc_tab
ls_t100-msgnr gc_tab
ls_t100-sprsl gc_tab
ls_t100-text gc_crlf
INTO lv_string.
ENDSELECT.
TRY.
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = lv_string
iv_codepage = '4103' "suitable for MS Excel, leave empty
iv_add_bom = 'X' "for other doc types
IMPORTING
et_solix = binary_content
ev_size = size ).
CATCH cx_bcs.
MESSAGE e445(so).
ENDTRY.
ENDFORM.
FORM frm_build_content .
" html
CLEAR : it_contents[].
"email body
wa_contents-line = '<HTML> <BODY>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
CONCATENATE '<p style="font-family:Calibri;font-size:15;">' 'Dear All,' INTO wa_contents-line.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
PERFORM line_break.
PERFORM line_break.
MOVE 'This is a eamil for test.' TO wa_contents-line .
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
" Set table style and detail
wa_contents-line = '<table style="font-family:calibri;font-size:15;MARGIN:10px;"'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
wa_contents-line = 'cellspacing="0" cellpadding="1" width="75%" '.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
wa_contents-line = 'border="1"><tbody><tr>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
"header
wa_contents-line = '<th bgcolor="#C0C0C0">Table Name</th>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
wa_contents-line = '<th bgcolor="#C0C0C0">Count(Yesterday)</th>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
wa_contents-line = '<th bgcolor="#C0C0C0">Count(Today)</th>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
wa_contents-line = '<th bgcolor="#C0C0C0">User</th>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
wa_contents-line = '<th bgcolor="#C0C0C0">Backup Date</th>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
wa_contents-line = '<th bgcolor="#C0C0C0">Backup Timestemp</th>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
" body
wa_contents-line = '<tr align = "center">'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
CONCATENATE '<td>' 'table name' '</td>' INTO wa_contents-line SEPARATED BY space.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
CONCATENATE '<td>' 'table count 1' '</td>' INTO wa_contents-line SEPARATED BY space.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
CONCATENATE '<td>' 'table count 2' '</td>' INTO wa_contents-line SEPARATED BY space.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
CONCATENATE '<td>' sy-uname '</td>' INTO wa_contents-line SEPARATED BY space.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
CONCATENATE '<td>' sy-datum '</td>' INTO wa_contents-line SEPARATED BY space.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
DATA(lv_backup_timestemp) = CONV string( sy-datum ).
CONCATENATE '<td>' lv_backup_timestemp '</td>' INTO wa_contents-line SEPARATED BY space.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
wa_contents-line = '</tbody> </table>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
PERFORM line_break.
PERFORM line_break.
**end
wa_contents-line = 'Thanks & Regards'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
PERFORM line_break.
ENDFORM.
FORM line_break.
wa_contents-line = '<br>'.
APPEND wa_contents TO it_contents.
CLEAR : wa_contents.
ENDFORM.
FORM send .
TRY.
"create send request
TRY.
gr_send_request = cl_bcs=>create_persistent( ).
CATCH cx_send_req_bcs..
ENDTRY.
"email sender
TRY.
CALL METHOD gr_send_request->set_sender
EXPORTING
i_sender = cl_cam_address_bcs=>create_internet_address( 'xxx@xx.com' ).
CATCH cx_send_req_bcs.
CATCH cx_address_bcs.
ENDTRY.
" email to
LOOP AT gt_receiver INTO DATA(ls_receiver).
TRY.
gr_recipient = cl_cam_address_bcs=>create_internet_address( ls_receiver ).
CATCH cx_address_bcs.
ENDTRY.
"Add recipient to send request
TRY.
CALL METHOD gr_send_request->add_recipient
EXPORTING
i_recipient = gr_recipient
i_express = 'X'.
CATCH cx_send_req_bcs.
ENDTRY.
ENDLOOP.
"email body create document
TRY.
gr_document = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = it_contents
i_importance = '1'
* i_length = '12'
i_subject = gc_subject ).
CATCH cx_document_bcs INTO gr_bcs_exception.
ENDTRY.
" add the spread sheet as attachment to document object
gr_document->add_attachment(
i_attachment_type = 'xls'
i_attachment_subject = 'ExampleSpreadSheet'
i_attachment_size = size
i_att_content_hex = binary_content ).
"Add document to send request
TRY.
CALL METHOD gr_send_request->set_document( gr_document ).
CATCH cx_send_req_bcs INTO gr_bcs_exception.
ENDTRY.
"Send email
TRY.
CALL METHOD gr_send_request->send(
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = gv_sent_to_all ).
CATCH cx_send_req_bcs.
ENDTRY.
IF gv_sent_to_all = 'X'.
WRITE 'Email sent!'.
ENDIF.
"Commit to send email
COMMIT WORK.
CATCH cx_bcs INTO gr_bcs_exception.
WRITE:
'Error!',
'Error type:',
gr_bcs_exception->error_type..
ENDTRY.
ENDFORM.