介绍一个项目中经常会遇到的场景:将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
以上。
发表评论