2023 Jan 16 9:33 AM
FORM send_errorlog_email.
TYPES: BEGIN OF ty_dl,
address TYPE rvari_val_255,
END OF ty_dl.
"Constants
CONSTANTS: c_codepage TYPE abap_encod VALUE '4103',
c_raw TYPE so_obj_tp VALUE 'RAW',
c_dl TYPE tvarvc-name VALUE 'ZEMAIL',
c_mailfrom TYPE adr6-smtp_addr VALUE 'Batch_Basis@xyz.com',
c_type type soodk-objtp value 'XLS',
c_tab type c value cl_bcs_convert=>gc_tab,
c_crlf type c value cl_bcs_convert=>gc_crlf.
"Data Declaration
DATA : v_email TYPE adr6-smtp_addr.
DATA : v_string TYPE string,
v_date TYPE char10.
DATA: v_size TYPE so_obj_len,
v_sent_to_all TYPE os_boolean,
v_mtitle TYPE sodocchgi1-obj_descr,
v_attach_name TYPE sood-objdes,
v_time TYPE char8,
"Internal Tables
t_dl TYPE STANDARD TABLE OF ty_dl,
t_objtxt TYPE srm_t_solisti1,
t_binary_content TYPE solix_tab,
"work areas
wa_dl TYPE ty_dl,
"Objects
o_sender TYPE REF TO cl_cam_address_bcs,
o_send_request TYPE REF TO cl_bcs,
o_document TYPE REF TO cl_document_bcs,
o_recipient TYPE REF TO if_recipient_bcs,
o_bcs_exception TYPE REF TO cx_bcs.
" Add heading to Excel file
CLEAR:v_string.
CONCATENATE text-009 "ID'(009)
text-010 "'Date'(010)
text-011 "'Time'(011)
text-012 "'Physician Name'(012)
text-013 "'Physician Email ID'(013)
text-014 "'Error Message'(014)
INTO v_string SEPARATED BY c_tab.
CONCATENATE v_string c_crlf INTO v_string.
DATA(v_temptime) = wa_log-ztime.
* convert date in YYYY-MM-DD
CLEAR:v_date, v_time.
CONCATENATE wa_zassure_log-zdate+0(4) '-'
wa_zassure_log-zdate+4(2) '-'
wa_zassure_log-zdate+6(2) INTO v_date.
* convert time in HH:MM:SS format
CONCATENATE v_temptime+0(2)
v_temptime+2(2)
v_temptime+4(2)
INTO v_time
SEPARATED BY ':'.
* Pass error log data to excel file
CONCATENATE v_string
wa_log-assure_id c_tab
v_date c_tab
v_time c_tab
wa_log-physician_name c_tab
wa_og-physician_email c_tab
wa_log-message c_crlf
INTO v_string.
* Fetch email addressed/DL from TVARVC table
SELECT low
INTO TABLE t_dl
FROM tvarvc
WHERE name = c_dl.
IF sy-subrc EQ 0.
"Convert v_string into Binary format
TRY.
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = v_string
iv_codepage = c_codepage "'4103'suitable for MS Excel
iv_add_bom = abap_true "for other doc types
IMPORTING
et_solix = t_binary_content
ev_size = v_size ).
CATCH cx_bcs.
MESSAGE e445(so). "Error when transfering document contents
ENDTRY.
* Get email Body details
PERFORM get_body CHANGING t_objtxt.
* Process email Body details
LOOP AT t_objtxt INTO DATA(wa_objtxt) WHERE line CA '<ID>'.
REPLACE 'ID>' IN wa_objtxt-line WITH wa-id.
MODIFY t_objtxt FROM wa_objtxt INDEX sy-tabix.
CLEAR : wa_objtxt.
ENDLOOP.
* Add Email subject line
CLEAR v_mtitle.
CONCATENATE text-022 "' ID'(022)
text-023 "'has failed in SAP'(023)
INTO v_mtitle SEPARATED BY space.
* Add attachment name
CLEAR v_attach_name.
CONCATENATE text-008 "'Error Log '(008).
wa_log-id INTO v_attach_name SEPARATED BY space.
TRY.
* -------- create persistent send request ------------------------
o_send_request = cl_bcs=>create_persistent( ).
* -------- create and set document with attachment ---------------
* create document object from internal table with text
o_document = cl_document_bcs=>create_document(
i_type = c_raw
i_text = t_objtxt[]
i_subject = v_mtitle ).
* add the spread sheet as attachment to document object
o_document->add_attachment(
i_attachment_type = c_type
i_attachment_subject = v_attach_name
i_attachment_size = v_size
i_att_content_hex = t_binary_content ).
* add document object to send request
o_send_request->set_document( o_document ).
o_sender = cl_cam_address_bcs=>create_internet_address( c_mailfrom ).
CALL METHOD o_send_request->set_sender
EXPORTING
i_sender = o_sender.
* add recipient (e-mail address)
LOOP AT t_dl INTO wa_dl.
* create recipient object
v_email = wa_dl-address .
o_recipient = cl_cam_address_bcs=>create_internet_address( v_email ).
CALL METHOD o_send_request->add_recipient
EXPORTING
i_recipient = o_recipient
i_express = abap_true.
CLEAR: wa_dl, v_email.
ENDLOOP.
* ---------- send document ---------------------------------------
v_sent_to_all = o_send_request->send( i_with_error_screen = abap_true ).
COMMIT WORK.
IF v_sent_to_all IS INITIAL.
MESSAGE s719(sawe_bo_sa). "E-mail sent successfully
ENDIF.
* ------------ exception handling ----------------------------------
CATCH cx_bcs INTO o_bcs_exception.
"Error occurred during transmission - return code: <&>
MESSAGE i865(so) WITH o_bcs_exception->error_type.
ENDTRY.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_BODY
*&---------------------------------------------------------------------*
* Get error log email body details
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_body CHANGING t_objtxt TYPE srm_t_solisti1.
DATA: wa_objtxt TYPE solisti1.
DATA: t_lines TYPE STANDARD TABLE OF tline,
wa_lines TYPE tline.
CONSTANTS: c_st TYPE tdid VALUE 'ST',
c_en TYPE spras VALUE 'E',
c_txt TYPE tdobject VALUE 'TEXT',
c_body TYPE thead-tdname VALUE 'ZSD_TEXT'.
** Get maintained standard text to use as email body
REFRESH t_lines.
CALL FUNCTION 'READ_TEXT'
EXPORTING
client = sy-mandt
id = c_st
language = c_en
name = c_body
object = c_txt
TABLES
lines = t_lines
EXCEPTIONS
id = 1
language = 2
name = 3
not_found = 4
object = 5
reference_check = 6
wrong_access_to_archive = 7
OTHERS = 8.
IF sy-subrc EQ 0.
CLEAR: wa_lines, wa_objtxt.
LOOP AT t_lines INTO wa_lines.
wa_objtxt = wa_lines-tdline.
APPEND wa_objtxt TO t_objtxt. "return email body
CLEAR : wa_objtxt, wa_lines.
ENDLOOP.
ENDIF.
ENDFORM.
2023 Jan 16 9:53 AM
2023 Jan 16 10:34 AM
Please edit your question, select your code and press the button [CODE], which makes the code appear colored/indented, it will be easier for people to look at it. Thank you!
2023 Jan 16 11:53 AM
So you created a txt attachment and send it as an xls, so what's the question?
(also there are already many threads/questions on attaching xls[x] data to a mail, perform some search)