介紹一個項目中經常會遇到的場景:將smartform轉化成PDF,然後再把PDF作爲郵件的附件發送出去。
1, 創建smartform
Tcode:smartforms創建一個簡單的smartform
添加一個文本節點,
2, 創建程序
創建一個abap程序,簡單邏輯如下:
1,生成smartform保存到spool中,注意控制參數(control_parameters)和輸出選項(output_options )的設定
2,調用RSPO_GET_ATTRIBUTES_SPOOLJOB ()取得spool的屬性
3,將spool中smartform轉換成PDF
4,通過類cl_bcs將PDF作爲附件發送郵件
代碼如下:其中有很多錯誤處理沒有寫,大家自己完善吧。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
REPORT ztest_smartform_mail.
DATA: g_fm_name TYPE rs38l_fnam. DATA: git_spoolids TYPE tsfspoolid, gwa_spool TYPE rspoid. DATA: gwa_result TYPE ssfcrescl, gwa_output TYPE ssfcompop, g_filename TYPE string, g_bin_filesize TYPE i, git_lines TYPE STANDARD TABLE OF tline, gwa_control TYPE ssfctrlop.
* for mail DATA send_request TYPE REF TO cl_bcs. DATA document TYPE REF TO cl_document_bcs. DATA recipient TYPE REF TO if_recipient_bcs. DATA bcs_exception TYPE REF TO cx_bcs. DATA sent_to_all TYPE os_boolean. DATA pdf_size TYPE so_obj_len. DATA pdf_content TYPE solix_tab. DATA pdf_xstring TYPE xstring. DATA:g_mailto TYPE ad_smtpadr.
DATA rq TYPE tsp01. DATA bin_size TYPE i. DATA dummy TYPE TABLE OF rspoattr.
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' EXPORTING formname = 'ZTEST_PDF' * VARIANT = ' ' * DIRECT_CALL = ' ' IMPORTING fm_name = g_fm_name EXCEPTIONS no_form = 1 no_function_module = 2 OTHERS = 3. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. gwa_control-no_dialog = 'X'. gwa_control-langu = sy-langu. gwa_output-tdnewid = 'X'. gwa_output-tdimmed = ' '. gwa_output-tddelete = ' '.
CALL FUNCTION g_fm_name EXPORTING control_parameters = gwa_control output_options = gwa_output IMPORTING job_output_info = gwa_result EXCEPTIONS formatting_error = 1 internal_error = 2 send_error = 3 user_canceled = 4 OTHERS = 5.
MOVE gwa_result-spoolids TO git_spoolids.
* get mail address CALL FUNCTION 'EFG_GEN_GET_USER_EMAIL' EXPORTING i_uname = sy-uname IMPORTING e_email_address = g_mailto EXCEPTIONS not_qualified = 1 user_not_found = 2 address_not_found = 3 OTHERS = 4. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF.
LOOP AT git_spoolids INTO gwa_spool. *&---------------------------------------------------------------------* *& Create PDF *&---------------------------------------------------------------------* * Create PDF Content * 1) get attributes of spool request * 2) convert spool request to PDF dependent on document type *----------------------------------------------------------------------* * ------------ get attributes of spool request --------------------- CALL FUNCTION 'RSPO_GET_ATTRIBUTES_SPOOLJOB' EXPORTING rqident = gwa_spool IMPORTING rq = rq TABLES attributes = dummy EXCEPTIONS no_such_job = 1 OTHERS = 2. IF sy-subrc <> 0. * error handling CONTINUE. ENDIF.
* --- convert spool request into PDF, dependent on document type --- IF rq-rqdoctype = 'OTF' OR rq-rqdoctype = 'SMART'. CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF' EXPORTING src_spoolid = gwa_spool no_dialog = 'X' pdf_destination = 'X' no_background = 'X' IMPORTING pdf_bytecount = bin_size bin_file = pdf_xstring EXCEPTIONS err_no_otf_spooljob = 1 err_no_spooljob = 2 err_no_permission = 3 err_conv_not_possible = 4 err_bad_dstdevice = 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 <> 0. * error handling CONTINUE. ENDIF. ELSEIF rq-rqdoctype = 'LIST'. CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF' EXPORTING src_spoolid = gwa_spool no_dialog = 'X' pdf_destination = 'X' no_background = 'X' IMPORTING pdf_bytecount = bin_size bin_file = pdf_xstring 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 <> 0. * error handling CONTINUE. ENDIF. ELSE. * error handling CONTINUE. ENDIF. pdf_size = bin_size.
*&---------------------------------------------------------------------* *& Send mail *&---------------------------------------------------------------------*
TRY.
* -------- create persistent send request ------------------------ send_request = cl_bcs=>create_persistent( ).
* -------- create and set document ------------------------------- pdf_content = cl_document_bcs=>xstring_to_solix( pdf_xstring ).
document = cl_document_bcs=>create_document( i_type = 'PDF' i_hex = pdf_content i_length = pdf_size i_subject = '郵件題目' ). "#EC NOTEXT
* add document object to send request send_request->set_document( document ).
* --------- add recipient (e-mail address) -----------------------
* create recipient object recipient = cl_cam_address_bcs=>create_internet_address( g_mailto ).
* add recipient object to send request send_request->add_recipient( recipient ).
* ---------- send document --------------------------------------- sent_to_all = send_request->send( i_with_error_screen = 'X' ).
COMMIT WORK.
IF sent_to_all IS INITIAL. * error handling CONTINUE. ELSE. MESSAGE 'send mail successfully' TYPE 'S'. ENDIF.
* ------------ exception handling ---------------------------------- * replace this rudimentary exception handling with your own one !!! CATCH cx_bcs INTO bcs_exception.
ENDTRY.
ENDLOOP. |
運行後,郵件會暫時保存在SOST中,
手動觸發後會發送到郵箱裏,也可以等待自動觸發,具體等待多久要看SOST中的配置。
收到的郵件就是下面這個樣子。。
打開PDF
以上。