Berdasarkan pengalaman saya selama menjadi konsultan SAP ABAP, masih banyak yang kesulitan untuk membuat sebuah program untuk mengirim email dari SAP berupa FILE PDF. Berikut merupakan sample code (subrutin) untuk mengirim email berupa attachement / lampiran PDF.
Deklarasi Variable:
DATA: finaa TYPE finaa, fsabe TYPE fsabe, itcpo TYPE itcpo, fimsg TYPE fimsg.
Buat subrutin untuk convert OTF:
FORM convert_advice TABLES it_otfdata STRUCTURE itcoo n_objcont TYPE soli_tab e_solix TYPE solix_tab. DATA: ld_hformat(10) TYPE c, doc_size(12) TYPE c, hltlines TYPE i, so10_lines TYPE i, htabix LIKE sy-tabix, lp_fle1(2) TYPE p, lp_fle2(2) TYPE p, lp_off1 TYPE p, linecnt TYPE p, hfeld(500) TYPE c, lt_hotfdata LIKE itcoo OCCURS 1 WITH HEADER LINE, htline LIKE tline OCCURS 1 WITH HEADER LINE, x_objcont TYPE soli_tab WITH HEADER LINE, x_objcont_line LIKE soli, ld_binfile TYPE xstring, lt_solix TYPE solix_tab , wa_soli TYPE soli, wa_solix TYPE solix, i TYPE i, n TYPE i. FIELD-SYMBOLS: ptr_hex> TYPE solix. * convert data LOOP AT it_otfdata INTO lt_hotfdata. APPEND lt_hotfdata. ENDLOOP. ld_hformat = finaa-textf. IF ld_hformat IS INITIAL OR ld_hformat = 'PDF'. ld_hformat = 'PDF'. "PDF as default ELSE. ld_hformat = 'ASCII'. ENDIF. CALL FUNCTION 'CONVERT_OTF' EXPORTING format = ld_hformat IMPORTING bin_filesize = doc_size bin_file = ld_binfile TABLES otf = lt_hotfdata lines = htline EXCEPTIONS err_max_linewidth = 1 err_format = 2 err_conv_not_possible = 3 OTHERS = 4. n = xstrlen( ld_binfile ). WHILE i < n. wa_solix-line = ld_binfile+i. APPEND wa_solix TO lt_solix. i = i + 255. ENDWHILE. e_solix[] = lt_solix[]. IF ld_hformat <> 'PDF'. LOOP AT htline. x_objcont = htline-tdline. APPEND x_objcont TO n_objcont. ENDLOOP. ENDIF. ENDFORM.
Buat Subrutin untuk convert ITF to ASCII
FORM convert_itf USING it_lines TYPE soli_tab CHANGING ct_text_mail TYPE soli_tab. DATA : x_objcont TYPE soli_tab WITH HEADER LINE, x_objcont_line LIKE soli, hltlines TYPE i, so10_lines TYPE i, htabix LIKE sy-tabix, lp_fle1(2) TYPE p, lp_fle2(2) TYPE p, lp_off1 TYPE p, linecnt TYPE p, hfeld(500) TYPE c, ltxt_tdtab_c256(256) OCCURS 5 WITH HEADER LINE, ltxt_tdtab_x256 TYPE tdtab_x256, ls_tdtab_x256 TYPE LINE OF tdtab_x256. FIELD-SYMBOLS TYPE c. * convert gt_lines to destination format CALL FUNCTION 'CONVERT_ITF_TO_ASCII' EXPORTING tabletype = 'BIN' IMPORTING x_datatab = ltxt_tdtab_x256 TABLES itf_lines = it_lines EXCEPTIONS invalid_tabletype = 1 OTHERS = 2. LOOP AT ltxt_tdtab_x256 INTO ls_tdtab_x256. ASSIGN ls_tdtab_x256 TO CASTING. ltxt_tdtab_c256 = . APPEND ltxt_tdtab_c256. ENDLOOP. IF cl_abap_char_utilities=>charsize > 1. DATA tab_c256(256) OCCURS 5 WITH HEADER LINE. DATA : i TYPE i, ld_appended(1) TYPE c. LOOP AT ltxt_tdtab_c256. i = sy-tabix MOD 2. ld_appended = space. IF i = 1. " uneven tab_c256 = ltxt_tdtab_c256. ELSE. tab_c256+128 = ltxt_tdtab_c256. " even APPEND tab_c256. ld_appended = 'X'. ENDIF. ENDLOOP. IF ld_appended = space. APPEND tab_c256. " append last line. ENDIF. ltxt_tdtab_c256[] = tab_c256[]. ENDIF. * convert to 255 for call to cl_bcs DESCRIBE TABLE ltxt_tdtab_c256 LINES hltlines. DESCRIBE FIELD ltxt_tdtab_c256 LENGTH lp_fle1 IN CHARACTER MODE. DESCRIBE FIELD x_objcont_line LENGTH lp_fle2 IN CHARACTER MODE. LOOP AT ltxt_tdtab_c256. htabix = sy-tabix. MOVE ltxt_tdtab_c256 TO hfeld+lp_off1. IF htabix = hltlines. lp_fle1 = strlen( ltxt_tdtab_c256 ). ENDIF. lp_off1 = lp_off1 + lp_fle1. IF lp_off1 GE lp_fle2. CLEAR x_objcont. x_objcont = hfeld(lp_fle2). APPEND x_objcont. SHIFT hfeld BY lp_fle2 PLACES. lp_off1 = lp_off1 - lp_fle2. ENDIF. IF htabix = hltlines. IF lp_off1 GT 0. CLEAR x_objcont. x_objcont = hfeld(lp_off1). APPEND x_objcont. ENDIF. ENDIF. ENDLOOP. ct_text_mail[] = x_objcont[]. ENDFORM.
Buat subrutin untuk send mail with attachement
FORM send_mail_with_attachm TABLES it_otfdata STRUCTURE itcoo it_advice STRUCTURE solix it_lines TYPE soli_tab USING id_call_from_pdf CHANGING cd_error LIKE boole-boole. DATA: so10_lines TYPE i, lt_hotfdata LIKE itcoo OCCURS 1 WITH HEADER LINE, htline LIKE tline OCCURS 1 WITH HEADER LINE, n_objcont TYPE soli_tab, ld_address LIKE finaa-intad, ld_addr TYPE adr6-smtp_addr, send_request TYPE REF TO cl_bcs, document TYPE REF TO cl_document_bcs, attachment TYPE REF TO cl_document_bcs, sender TYPE REF TO cl_sapuser_bcs, internet_recipient TYPE REF TO if_recipient_bcs, internet_sender TYPE REF TO if_sender_bcs, bcs_exception TYPE REF TO cx_bcs, sent_to_all TYPE os_boolean, lt_solix TYPE solix_tab, BEGIN OF ls_tmp, "1640757 type LIKE sxaddrtype-addr_type VALUE 'INT', "1640757 address LIKE soextreci1-receiver, "1640757 END OF ls_tmp. "1640757 DESCRIBE TABLE it_lines LINES so10_lines. DATA lt_text_mail TYPE soli_tab. CLEAR lt_text_mail[]. IF so10_lines > 0. * convert gt_lines PERFORM convert_itf USING it_lines[] CHANGING lt_text_mail[]. * the result is now in lt_text_mail[] ENDIF. TRY. send_request = cl_bcs=>create_persistent( ). IF finaa-mail_status_attr = space. send_request->set_status_attributes( i_requested_status = 'N' i_status_mail = 'N' ). ELSE. send_request->set_status_attributes( i_requested_status = finaa-mail_status_attr i_status_mail = finaa-mail_status_attr ). ENDIF. * create sender IF finaa-mail_send_addr <> space. ld_addr = finaa-mail_send_addr. internet_sender = cl_cam_address_bcs=>create_internet_address( i_address_string = ld_addr ). CALL METHOD send_request->set_sender EXPORTING i_sender = internet_sender. ELSE. DATA: ld_originator TYPE uname. IF finaa-intuser <> space. ld_originator = finaa-intuser. ELSEIF fsabe-usrnam IS INITIAL. ld_originator = sy-uname. ELSE. ld_originator = fsabe-usrnam. ENDIF. sender = cl_sapuser_bcs=>create( ld_originator ). CALL METHOD send_request->set_sender EXPORTING i_sender = sender. ENDIF. * create recipients ld_address = finaa-intad. * LOOP AT i_reclist. * ld_address = i_reclist-receiver. * WHILE ld_address(1) = space. * SHIFT ld_address BY 1 PLACES. * ENDWHILE. * SPLIT ld_address AT ' ' INTO ls_tmp-address ld_address. "1640757 * CALL FUNCTION 'SX_INTERNET_ADDRESS_TO_NORMAL' "1640757 * EXPORTING address_unstruct = ls_tmp "1640757 * IMPORTING address_normal = ls_tmp "1640757 * EXCEPTIONS error_address = 2 "1640757 * error_group_address = 3. "1640757 * CHECK sy-subrc = 0. "1640757 * "1640757 * ld_addr = ls_tmp-address. "1640757 * internet_recipient = * cl_cam_address_bcs=>create_internet_address( * i_address_string = ld_addr ). * CALL METHOD send_request->add_recipient * EXPORTING * i_recipient = internet_recipient. * ENDLOOP. WHILE ld_address <> space. WHILE ld_address(1) = space. SHIFT ld_address BY 1 PLACES. ENDWHILE. SPLIT ld_address AT ' ' INTO ls_tmp-address ld_address. "1640757 CALL FUNCTION 'SX_INTERNET_ADDRESS_TO_NORMAL' "1640757 EXPORTING address_unstruct = ls_tmp "1640757 IMPORTING address_normal = ls_tmp "1640757 EXCEPTIONS error_address = 2 "1640757 error_group_address = 3. "1640757 CHECK sy-subrc = 0. "1640757 "1640757 ld_addr = ls_tmp-address. "1640757 internet_recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = ld_addr ). CALL METHOD send_request->add_recipient EXPORTING i_recipient = internet_recipient. ENDWHILE. document = cl_document_bcs=>create_document( i_type = 'TXT' i_text = lt_text_mail i_subject = itcpo-tdtitle ). IF id_call_from_pdf IS INITIAL. PERFORM convert_advice TABLES it_otfdata n_objcont lt_solix. ELSE. lt_solix[] = it_advice[]. ENDIF. IF finaa-textf = 'PDF' OR finaa-textf = space. attachment = cl_document_bcs=>create_document( i_type = 'PDF' i_hex = lt_solix i_subject = itcpo-tdtitle ). ELSE. attachment = cl_document_bcs=>create_document( i_type = 'RAW' i_text = n_objcont i_subject = itcpo-tdtitle ). ENDIF. IF finaa-mail_sensitivity <> space. * 'P' is confidential, * 'F' is functional document->set_sensitivity( finaa-mail_sensitivity ). ENDIF. IF finaa-mail_importance <> space. document->set_importance( finaa-mail_importance ). ENDIF. CALL METHOD document->add_document_as_attachment EXPORTING im_document = attachment. send_request->set_document( document ). IF finaa-mail_send_prio <> space. send_request->set_priority( finaa-mail_send_prio ). ENDIF. IF itcpo-tdsenddate IS NOT INITIAL. DATA : l_timestamp TYPE bcs_sndat, l_tzone TYPE timezone. l_tzone = sy-zonlo. CONVERT DATE itcpo-tdsenddate TIME itcpo-tdsendtime INTO TIME STAMP l_timestamp TIME ZONE l_tzone. send_request->send_request->set_send_at( l_timestamp ). ENDIF. IF finaa-mail_outbox_link <> space. send_request->send_request->set_link_to_outbox( EXPORTING i_link_to_outbox = 'X' ). ENDIF. sent_to_all = send_request->send( i_with_error_screen = space ). IF sent_to_all = space. fimsg-msgno = '750'. fimsg-msgv1 = sy-subrc. * PERFORM MESSAGE USING '750'. ENDIF. CATCH cx_bcs INTO bcs_exception. fimsg-msgno = '750'. fimsg-msgv1 = sy-subrc. * PERFORM MESSAGE USING '750'. cd_error = 'X'. ENDTRY. ENDFORM.