Note: In many places I haven't removed the hard coding. Please remove it while coding. In some places I have used ABAP 7.4 syntax just to write less codes (highlighted by red).
*&---------------------------------------------------------------------* *& Report ZEMAIL_ATTACHMENT *&---------------------------------------------------------------------* *& Email Attachment - Excel and PDF *&---------------------------------------------------------------------* REPORT zemail_attachment. DATA: gv_attachment_string TYPE string. START-OF-SELECTION. PERFORM create_merge_text. PERFORM send_email. FORM create_merge_text. DATA: lv_string_tmp TYPE string. lv_string_tmp = |Hello| & |{ cl_abap_char_utilities=>horizontal_tab }| & |World| & |{ cl_abap_char_utilities=>horizontal_tab }| . gv_attachment_string = |{ lv_string_tmp }| & |{ cl_abap_char_utilities=>cr_lf }|. ************************* FOR PDF ATTACHMENT *************************** CALL FUNCTION 'SET_PRINT_PARAMETERS' EXPORTING destination = 'LOCL' " Printer layout = 'X_65_512/2' "Format line_count = '65' "Line Count line_size = '1024'. "Line Size IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. NEW-PAGE PRINT ON NO DIALOG. WRITE: / |HELLO WORLD FOR PDF|. NEW-PAGE PRINT OFF. ENDFORM. FORM send_email. TYPES: ty_t_pdf TYPE STANDARD TABLE OF tline. DATA: lv_mlrec TYPE so_obj_nam, lv_sent_to_all TYPE os_boolean, lv_email TYPE adr6-smtp_addr, lv_subject TYPE so_obj_des, lt_text TYPE bcsy_text, lo_send_request TYPE REF TO cl_bcs, lo_bcs_exception TYPE REF TO cx_bcs, lo_recipient TYPE REF TO if_recipient_bcs, lo_sender TYPE REF TO cl_sapuser_bcs, lo_document TYPE REF TO cl_document_bcs, lt_binary_content TYPE solix_tab, lv_size TYPE so_obj_len, lt_dl_entry TYPE TABLE OF sodlienti1, lt_pdf_output TYPE ty_t_pdf, ls_pdf_output TYPE tline, lv_buffer TYPE string, lt_mess_att TYPE TABLE OF solisti1, ls_mess_att TYPE solisti1, lv_spool TYPE rspoid, ls_print TYPE slis_print_alv, ls_print_ctrl TYPE alv_s_pctl. CONSTANTS: lc_subject TYPE so_obj_des VALUE 'Test Mail with Excel & PDF Attachment', lc_raw TYPE char03 VALUE 'RAW', lc_container TYPE c LENGTH 30 VALUE 'CC', lc_col1 TYPE c LENGTH 2 VALUE ' ~', lc_col2 TYPE c LENGTH 2 VALUE '~ ', lc_255 TYPE i VALUE 255. TRY. "Create send request lo_send_request = cl_bcs=>create_persistent( ). "Email FROM... lo_sender = cl_sapuser_bcs=>create( sy-uname ). "Add sender to send request CALL METHOD lo_send_request->set_sender EXPORTING i_sender = lo_sender. "Email TO... lv_email = |TEST| & |@TEST.COM|. lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_email ). "Add recipient to send request CALL METHOD lo_send_request->add_recipient EXPORTING i_recipient = lo_recipient i_express = 'X'. "Email BODY APPEND INITIAL LINE TO lt_text ASSIGNING FIELD-SYMBOL(<fs_text>). <fs_text>-line = |Hi|. lo_document = cl_document_bcs=>create_document( i_type = lc_raw i_text = lt_text i_length = '12' i_subject = lc_subject ). "Add document to send request CALL METHOD lo_send_request->set_document( lo_document ). "Excel Attachment IF gv_attachment_string IS NOT INITIAL. TRY. cl_bcs_convert=>string_to_solix( EXPORTING iv_string = gv_attachment_string iv_codepage = '4103' "suitable for MS Excel, leave empty iv_add_bom = 'X' "for other doc types IMPORTING et_solix = lt_binary_content ev_size = lv_size ). CATCH cx_bcs. * message E445(SO). EXIT. ENDTRY. lo_document->add_attachment( i_attachment_type = 'CSV' i_attachment_subject = 'Excel Attachment' i_attachment_size = lv_size i_att_content_hex = lt_binary_content ). ************************* PDF ATTACHMENT ******************************* ls_print-print = abap_true. lv_spool = sy-spono. CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF' EXPORTING src_spoolid = lv_spool no_dialog = space dst_device = 'LOCL' get_size_from_format = abap_true TABLES pdf = lt_pdf_output EXCEPTIONS err_no_abap_spooljob = 1 err_no_spooljob = 2 err_no_permission = 3 err_conv_not_possible = 4 err_bad_destdevice = 5 user_cancelled = 6 err_spoolerror = 7 err_temseerror = 8 err_btcjob_open_failed = 9 err_btcjob_submit_failed = 10 err_btcjob_close_failed = 11 OTHERS = 12. IF sy-subrc EQ 0. LOOP AT lt_pdf_output INTO ls_pdf_output. TRANSLATE ls_pdf_output USING lc_col1. CONCATENATE lv_buffer ls_pdf_output INTO lv_buffer. CLEAR ls_pdf_output. ENDLOOP. TRANSLATE lv_buffer USING lc_col2. DO. ls_mess_att = lv_buffer. APPEND ls_mess_att TO lt_mess_att. SHIFT lv_buffer LEFT BY lc_255 PLACES. IF lv_buffer IS INITIAL. EXIT. ENDIF. CLEAR ls_mess_att. ENDDO. ENDIF. lo_document->add_attachment( i_attachment_type = 'PDF' i_attachment_subject = 'PDF Attachment' i_att_content_text = lt_mess_att[] ). ENDIF. "Send email CALL METHOD lo_send_request->send( EXPORTING i_with_error_screen = abap_true RECEIVING result = lv_sent_to_all ). IF lv_sent_to_all = abap_true. "Commit to send email COMMIT WORK. MESSAGE text-041 TYPE 'S'. "Email sent! ENDIF. "Exception handling CATCH cx_bcs INTO lo_bcs_exception. WRITE: 'Error!', 'Error type:', lo_bcs_exception->error_type. ENDTRY. ENDFORM.
No comments:
Post a Comment