1, 业务场景
在项目中,我们需要写一个自定义的Web Dynpro应用程序,以方便用户的业务流程。此业务流程的活动大多是有关批准,与不同层次的审批者。每个审批人(批准/拒绝)后,通过电子邮件发送到相关人士。完全不同的电子邮件将被发送超过10次。我们必须要找到一个有效的方式来发送电子邮件。
2, 解决方法
即使有10个以上不同内容的电子邮件,或多或少的它们具有相同的结构。例如“信尾”大多都会这样“这是一个计算机自动生成的信息。请不要回复。” 这意味着,如果我们找到一个有效的方式进行分组,电子邮件和编写代码,维护更容易对我们来说,不管用户将来需求如何变化。这时我想到使用Smartforms。 Smartforms的控制流和条件以及文本元素(text element),将帮助我实现想法。我只需要创建一个Smartform,使用电子邮件模板传递我的信息.
一般情况下,该方案像这样,通过代码调用Smartforms- >传递参数(如我想用哪个模板) - >收到的内容,用它做什么- >发送邮件。
3, 技术细节
首先,通过Smartforms创建电子邮件模板,架构架构,创建新的文件夹,新的条件。
在条件选项卡中,我们可以决定哪些内容显示:
我们甚至可以使用HTML标记来写HTML邮件的内容,例如<BR/>线,<A>标签的URL
然后,我们需要激活Smartform在WebDynpro侧。设定援助类的方法,调用Smartform。传递参数的模板,把有必要的内容写入邮件.
后调用的Smartforms的,它会返回一个结构,保持内容的类型SSFCRESCL一个OTF领域中。
在调试模式下,我们注意到,OTF数据是可读的,内容的电子邮件和一些奇怪的数字。
我们需要发送HTML格式的电子邮件,使用方法cl_document_bcs=> create_document。 i_text参数是一个表型SOLI_TAB的,底层的数据结构是一个字符。我们现在的任务是从OTF转换到SOLI,同时仍然保持正确的内容. 首先,我们将转换为OTF表TLINE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
call function 'CONVERT_OTF' exporting format = 'ASCII' max_linewidth = 132 tables otf = ls_job_output-otfdata lines = lt_lines exceptions err_max_linewidth = 1 err_format = 2 err_conv_not_possible = 3 err_bad_otf = 4 others = 5. if sy-subrc <> 0. * Implement suitable error handling here endif. |
看更具有可读性,不是吗?现在,所有我们需要做的是将其转换为SOLI删除一些多余的线条。
1 2 3 4 5 6 |
DELETE lt_lines WHERE tdline EQ space. LOOP AT lt_lines INTO ls_line. ls_soli = ls_line-tdline. APPEND ls_soli TO lt_soli. CLEAR ls_soli. ENDLOOP. |
完美!现在,我们可以使用这些内容发送电子邮件了
1 2 3 4 |
lo_document = cl_document_bcs=>create_document( i_type = 'HTM' i_subject = 'Mail Subject' i_text = lt_soli ). |
4, 完整实例
下面是完整的例子,和上面的讲解略有不同,
smartform部分:
其中 <br/><br/>代表换行。
完整程序:
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 |
REPORT ztest_html_mail. CONSTANTS: lc_sf_htm_mail TYPE tdsfname VALUE 'ZTEST_PDF'. DATA ev_msg. *---Data declaration DATA: lo_send_request TYPE REF TO cl_bcs, lo_document TYPE REF TO cl_document_bcs, recipient TYPE REF TO if_recipient_bcs, bcs_exception TYPE REF TO cx_bcs. DATA:g_mailto TYPE ad_smtpadr. DATA: lt_lines TYPE TABLE OF tline, ls_line TYPE tline, lt_soli TYPE soli_tab, ls_soli TYPE soli. DATA:lv_sent_flag TYPE c. DATA: lv_fname TYPE rs38l_fnam, ls_job_output TYPE ssfcrescl. "Structure to return value at the end of form printing DATA: ls_ctrl_form TYPE ssfctrlop, "Smart Form Control Structure ls_output_opt TYPE ssfcompop. "Smart Form Transfer Options *---Pass data to Smartforms to receive itab of HTML Email "Spool parameters ls_output_opt-tdimmed = 'X'. "Print Immediately (Print Parameters) ls_output_opt-tddelete = 'X'. "Delete After Printing (Print Parameters) ls_output_opt-tdlifetime = 'X'. "Spool Retention Period (Print Parameters) ls_output_opt-tddest = 'LOCL'. "Spool: Output device *ls_output_opt-tdprinter = 'SWIN'. "Spool: Device type name ls_ctrl_form-no_dialog = 'X'. "SAP Smart Forms: General Indicator ls_ctrl_form-preview = 'X'. "Print preview ls_ctrl_form-getotf = 'X'. "Return of OTF table. No printing, display, or faxing ls_ctrl_form-langu = 'EN'. "Language key ls_ctrl_form-device = 'PRINTER'. "Output device "Get Smart Form Function Module Name CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' EXPORTING formname = lc_sf_htm_mail IMPORTING fm_name = lv_fname EXCEPTIONS no_form = 1 no_function_module = 2 OTHERS = 3. IF sy-subrc <> 0. ev_msg = 'Error Sending Email'. RETURN. ENDIF. "Call Smart Form Function Module CALL FUNCTION lv_fname EXPORTING control_parameters = ls_ctrl_form output_options = ls_output_opt IMPORTING job_output_info = ls_job_output EXCEPTIONS formatting_error = 1 internal_error = 2 send_error = 3 user_canceled = 4 OTHERS = 5. IF ls_job_output-otfdata IS INITIAL. ev_msg = 'Error Sending Email'. RETURN. ENDIF. "Convert OTF to TLINE CALL FUNCTION 'CONVERT_OTF' EXPORTING format = 'ASCII' max_linewidth = 132 TABLES otf = ls_job_output-otfdata lines = lt_lines EXCEPTIONS err_max_linewidth = 1 err_format = 2 err_conv_not_possible = 3 err_bad_otf = 4 OTHERS = 5. IF sy-subrc <> 0. ev_msg = 'Error Sending Email'. RETURN. ENDIF. "Remove empty lines DELETE lt_lines WHERE tdline EQ space. "Convert itab of HTML Email to itab of sending format in class CL_BCS LOOP AT lt_lines INTO ls_line. ls_soli = ls_line-tdline. APPEND ls_soli TO lt_soli. CLEAR ls_soli. ENDLOOP. * 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. "Instantinate CL_BCS and specify options TRY . "Create persistent lo_send_request = cl_bcs=>create_persistent( ). lo_document = cl_document_bcs=>create_document( i_type = 'HTM' i_subject = 'Email Subject' i_text = lt_soli ). lo_send_request->set_document( lo_document ). "Send to recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = g_mailto ). lo_send_request->add_recipient( i_recipient = recipient ). "Carbon Copy * recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = g_mailto ). * lo_send_request->add_recipient( i_recipient = recipient i_copy = 'X' ). lo_send_request->set_send_immediately( i_send_immediately = abap_true ). lv_sent_flag = lo_send_request->send( i_with_error_screen = 'X' ). IF lv_sent_flag EQ abap_false. ev_msg = 'Error Sending Email'. RETURN. ELSE. COMMIT WORK. ENDIF. CATCH cx_bcs INTO bcs_exception. ev_msg = bcs_exception->get_longtext( ). ENDTRY. |
运行后,收到邮件如下:
以上。
发表评论