ABAP 使用Smartforms发送HTML邮件

来源:互联网 发布:vmware 6.0 配置网络 编辑:程序博客网 时间:2024/06/05 09:32

Send HTML Mail usingSmartforms

发送HTML邮件使用Smartforms

 

Hi everyone, in this blog I will share my twocents worth in sending notification email in SAP.
大家好,在这篇博客,我将分享两份用SAP发送通知邮件示例。

 

Businessscenarios业务场景

In my project, I have to write a customizedWeb Dynpro Application to facilitate a business process for user. Most of theactivities in this business process is     about Approval,with different layers of Approver. After each Approver makes an action(Approve/Reject), notification emails have to be sent to relevant parties.

 在项目中,我们需要写一个自定义的Web Dynpro应用程序,以方便用户的业务流程。此业务流程的活动大多是有关批准,与不同层次的审批者。每个审批人(批准/拒绝)后,通过电子邮件发送到相关人士。

 

The total different emails willhave to be sent is morethan 10. I have to find an effective way to sendout the emails.

完全不同的电子邮件将被发送超过10次。我们必须要找到一个有效的方式来发送电子邮件。

 

Approaches方法

Even though there are more than 10 emailcontents, more or less they have the same structures.

即使有10个以上不同内容的电子邮件,或多或少的它们具有相同的结构。

 

<<Header>><<Body>><<Footer>>

E.g. <<Footer>> isalways like "Thisis a generated computer message. Please do not reply."

例如“信尾”大多都会这样“这是一个计算机自动生成的信息。请不要回复。”

 

This means if I find an effective way togroup the email and write the code, maintenance will be much more easier for meif user requires any changes in the future. I start Google and search for ideason SCN. Some of the solutions I found:

这意味着,如果我们找到一个有效的方式进行分组,电子邮件和编写代码,维护更容易对我们来说,不管用户将来需求如何变化。我开始用Google在SCN搜素我的想法的一些解决方案,我发现:

E.g. - Write email content in the code,make use of Text Elements

      - Use SO10 to write email content

例如 – 用代码写邮件内容必须使用文本元素

-         电子邮件的内容

All of the solutions I found did not meet mycriteria of structuring Email Contents for easy maintenance.

所有的解决方案,我发现不符合我的标准.并不便于维护。

This is when I came to think of usingSmartforms. Smartforms with its Control Flow and Condition will help me in achievingmy objective. I just need to create a Smartform to use as Email Templates forall of my notification.

这时我想到使用Smartforms。 Smartforms的控制流和条件,将帮助我实现想法。我只需要创建一个Smartform,使用电子邮件模板传递我的信息.

 

Generally, the solution will look like, I callSmartforms through my code --> pass the parameters (such as Which template Iwant to used? Content of that      template?) -->Received back the content, do something with it --> Send Mail.

 

一般情况下,该方案像这样,通过代码调用Smartforms- >传递参数(如我想用哪个模板) - >收到的内容,用它做什么- >发送邮件。

 

Technicaldetails技术细节

Firstly I have to create my Email Templates throughSmartforms. I will structure my Email Templates using Smartforms, by creatingnew Folder, new Conditions.

首先,通过Smartforms创建电子邮件模板,架构架构,创建新的文件夹,新的条件。


WithConditions tab, we can decide which content to be shown:

在条件选项卡中,我们可以决定哪些内容显示:


We can even make use of HTML Tag to write HTML Mail Content,e.g. <br/> for line break, <a> tag for URL

我们甚至可以使用HTML标记来写HTML邮件的内容,例如<BR/>线,<A>标签的URL


After that, I just need to activate theSmartform. From my Web Dynpro side, let say a method in Assistance Class, Iwill call my Smartforms, passing the parameters indicated the template I willused, any necessary contents for the email.

然后,我们需要激活SmartformWebDynpro侧。设定援助类的方法,调用Smartform。传递参数的模板,把有必要的内容写入邮件

After calling the Smartforms,it will return a structure of type SSFCRESCL thatkeeps the content in a OTF field.

后调用的Smartforms的,它会返回一个结构,保持内容的类型SSFCRESCL一个OTF领域中。


In Debug mode, we noticed that the OTF data is readable, with content of the email and some weirdnumbers.

在调试模式下,我们注意到,OTF数据是可读的,内容的电子邮件和一些奇怪的数字。


We need to send a HTML email, using method cl_document_bcs=>create_document. The i_text parameter is a table of type SOLI_TAB, and the underlying data structure is just Character.

我们需要发送HTML格式的电子邮件,使用方法cl_document_bcs=> create_document i_text参数是一个表型SOLI_TAB的,底层的数据结构是一个字符。

Our job now is to convert from OTF to SOLI while still keeping the correct content of theemail.

 我们现在的任务是从OTF转换到SOLI,同时仍然保持正确的内容.

First we will convert the OTF table to TLINE

 首先,我们将转换为OTFTLINE

[csharp] view plaincopy
  1. 1.       call function 'CONVERT_OTF'    
  2. 2.          exporting    
  3. 3.            format                = 'ASCII'    
  4. 4.            max_linewidth    = 132    
  5. 5.          tables    
  6. 6.            otf                    = ls_job_output-otfdata    
  7. 7.            lines                = lt_lines    
  8. 8.          exceptions    
  9. 9.            err_max_linewidth         = 1    
  10. 10.         err_format                     = 2    
  11. 11.         err_conv_not_possible = 3    
  12. 12.         err_bad_otf                   = 4    
  13. 13.         others                           = 5.    
  14. 14.     if sy-subrc <> 0.    
  15. 15.     * Implement suitable error handling here    
  16. 16.     endif.    


Look much more readable, isn't it? Now all we have to dois to convert it to SOLI bydeleting few redundant lines.

 看更具有可读性,不是吗?现在,所有我们需要做的是将其转换为SOLI删除一些多余的线条。

[csharp] view plaincopy
  1. 1.       DELETE lt_lines WHERE tdline EQ space.    
  2. 2.       LOOP AT lt_lines INTO ls_line.    
  3. 3.          ls_soli = ls_line-tdline.    
  4. 4.          APPEND ls_soli TO lt_soli.    
  5. 5.          CLEAR ls_soli.    
  6. 6.       ENDLOOP.    


Perfect! We can now use this content to send out theemail.

完美!现在,我们可以使用这些内容发送电子邮件了 

[csharp] view plaincopy
  1. 1.            lo_document = cl_document_bcs=>create_document(    
  2. 2.                i_type        = 'HTM'    
  3. 3.                i_subject   = 'Mail Subject'    
  4. 4.                i_text         = lt_soli ).    

Written with HTML in Smartforms, out Email content canlook like this.


SourceCode源代码

[csharp] view plaincopy
  1. constants: lc_sf_htm_mail type string value 'ZSF_SMART_FORM_NAME'.    
  2.   data  ev_msg.  
  3.     
  4. *---Data declaration    
  5.   data: lo_send_request type ref to cl_bcs,    
  6.         lo_document     type ref to cl_document_bcs,    
  7.         recipient       type ref to if_recipient_bcs,    
  8.         bcs_exception   type ref to cx_bcs.    
  9.     
  10.   data: lt_lines type table of tline,    
  11.         ls_line  type tline,    
  12.         lt_soli  type soli_tab,    
  13.         ls_soli  type soli.    
  14.     
  15.     
  16.   data: lv_fname      type rs38l_fnam,    
  17.         ls_job_output type ssfcrescl. "Structure to return value at the end of form printing    
  18.     
  19.     
  20.   data: ls_ctrl_form  type ssfctrlop, "Smart Form Control Structure    
  21.         ls_output_opt type ssfcompop. "Smart Form Transfer Options    
  22.     
  23.     
  24. *---Pass data to Smartforms to receive itab of HTML Email    
  25.     
  26.     
  27.   "Spool parameters    
  28.   ls_output_opt-tdimmed = 'X'.      "Print Immediately (Print Parameters)    
  29.   ls_output_opt-tddelete = 'X'.     "Delete After Printing (Print Parameters)    
  30.   ls_output_opt-tdlifetime = 'X'.   "Spool Retention Period (Print Parameters)    
  31.   ls_output_opt-tddest = 'LOCL'.    "Spool: Output device    
  32.   ls_output_opt-tdprinter = 'SWIN'. "Spool: Device type name    
  33.     
  34.     
  35.   ls_ctrl_form-no_dialog = 'X'.    "SAP Smart Forms: General Indicator    
  36.   ls_ctrl_form-preview = 'X'.      "Print preview    
  37.   ls_ctrl_form-getotf = 'X'.       "Return of OTF table. No printing, display, or faxing    
  38.   ls_ctrl_form-langu = 'EN'.       "Language key    
  39.   ls_ctrl_form-device = 'PRINTER'. "Output device    
  40.     
  41.     
  42.   "Get Smart Form Function Module Name    
  43.   call function 'SSF_FUNCTION_MODULE_NAME'    
  44.     exporting    
  45.       formname           = lc_sf_htm_mail    
  46.     importing    
  47.       fm_name            = lv_fname    
  48.     exceptions    
  49.       no_form            = 1    
  50.       no_function_module = 2    
  51.       others             = 3.    
  52.     
  53.     
  54.   if sy-subrc <> 0.    
  55.     ev_msg = 'Error Sending Email'.    
  56.           return.    
  57.   endif.    
  58.     
  59.     
  60.   "Call Smart Form Function Module    
  61.   call function lv_fname    
  62.     exporting    
  63.       control_parameters = ls_ctrl_form    
  64.       output_options     = ls_output_opt    
  65.       iv_staff_name      = 'wu'  "iv_staff_name    
  66.       iv_role            = 'wu'  "iv_role    
  67.       iv_action          = 'wu'    "iv_action    
  68.     importing    
  69.       job_output_info    = ls_job_output    
  70.     exceptions    
  71.       formatting_error   = 1    
  72.       internal_error     = 2    
  73.       send_error         = 3    
  74.       user_canceled      = 4    
  75.       others             = 5.    
  76.     
  77.     
  78.   if ls_job_output-otfdata is initial.    
  79.     ev_msg = 'Error Sending Email'.    
  80.           return.    
  81.   endif.    
  82.     
  83.   "Convert OTF to TLINE    
  84.   call function 'CONVERT_OTF'    
  85.     exporting    
  86.       format                = 'ASCII'    
  87.       max_linewidth         = 132    
  88.     tables    
  89.       otf                   = ls_job_output-otfdata    
  90.       lines                 = lt_lines    
  91.     exceptions    
  92.       err_max_linewidth     = 1    
  93.       err_format            = 2    
  94.       err_conv_not_possible = 3    
  95.       err_bad_otf           = 4    
  96.       others                = 5.    
  97.     
  98.     
  99.   if sy-subrc <> 0.    
  100.     ev_msg = 'Error Sending Email'.    
  101.           return.    
  102.   endif.    
  103.     
  104.     
  105.   "Remove empty lines    
  106.   delete lt_lines where tdline eq space.    
  107.     
  108.     
  109.   "Convert itab of HTML Email to itab of sending format in class CL_BCS    
  110.   loop at lt_lines into ls_line.    
  111.     ls_soli = ls_line-tdline.    
  112.     append ls_soli to lt_soli.    
  113.     clear ls_soli.    
  114.   endloop.    
  115.     
  116.     
  117.   "Instantinate CL_BCS and specify options    
  118.   try .    
  119.       "Create persistent    
  120.       lo_send_request = cl_bcs=>create_persistent( ).    
  121.     
  122.     
  123.       lo_document = cl_document_bcs=>create_document(    
  124.           i_type        = 'HTM'    
  125.           i_subject     = 'Email Subject'    
  126.           i_text        = lt_soli ).    
  127.     
  128.     
  129.       if iv_attached_pdf is not initial.    
  130.         "Attach PDF    
  131.         lv_pdf_size = xstrlen( iv_attached_pdf ).    
  132.         lv_pdf_content = cl_document_bcs=>xstring_to_solix( ip_xstring = iv_attached_pdf ).    
  133.     
  134.     
  135.         lo_document->add_attachment(    
  136.            i_attachment_type     = 'PDF'    
  137.            i_attachment_subject  = 'PDF Attachment'    
  138.            i_attachment_size     = lv_pdf_size    
  139.            i_att_content_hex     = lv_pdf_content ).    
  140.     
  141.     
  142.       endif.    
  143.     
  144.     
  145.       lo_send_request->set_document( lo_document ).    
  146.     
  147.     
  148.       "Send to    
  149.       recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = ' name@company.com ' ).    
  150.       lo_send_request->add_recipient( i_recipient = recipient ).    
  151.     
  152.     
  153.       "Carbon Copy    
  154.       recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = 'name@company.com' ).    
  155.       lo_send_request->add_recipient( i_recipient = recipient i_copy = 'X' ).    
  156.     
  157.     
  158.       lo_send_request->set_send_immediately( i_send_immediately = abap_true ).    
  159.     
  160.     
  161.       lv_sent_flag = lo_send_request->send( i_with_error_screen = 'X' ).    
  162.     
  163.     
  164.       if lv_sent_flag eq abap_false.    
  165.         ev_msg = 'Error Sending Email'.    
  166.                     return.    
  167.       endif.    
  168.     
  169.     
  170.     catch cx_bcs into bcs_exception.    
  171.       ev_msg = bcs_exception->get_longtext( ).    
  172.     
  173.     
  174.   endtry.    


Conclusion结论

Aboveis the steps I used to send HTML mail using Smartforms.The general idea isconvert from data from OTF --> TLINE --> SOLI

Theadvantages of using this technique are:

-Email Template is well structured. Using Smartforms Flows and Conditions,Folder, Form Interface, all of my 10 notification emails can be maintainedeasily under one Smartform.

-HTML content

Thisis my very first blog. Hope it helps you. Please comment and share yourknowledge

Thankyou for reading!

Cheers!

以上是我用来发送HTML邮件使用Smartforms.The的总体思路是转换数据OTF- > TLINE - > SOLI

使用这种技术的优点是:

 

-电子邮件模板结构良好。利用流动和条件Smartforms,文件夹,表单界面,可以保持我的所有10个通知邮件很容易在一个Smartform。

-HTML内容


0 0
原创粉丝点击