ABAP 使用Smartforms发送HTML邮件

来源:互联网 发布:check it 编辑:程序博客网 时间:2024/06/05 19: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

1.       call function 'CONVERT_OTF'  2.          exporting  3.            format                = 'ASCII'  4.            max_linewidth    = 132  5.          tables  6.            otf                    = ls_job_output-otfdata  7.            lines                = lt_lines  8.          exceptions  9.            err_max_linewidth         = 1  10.         err_format                     = 2  11.         err_conv_not_possible = 3  12.         err_bad_otf                   = 4  13.         others                           = 5.  14.     if sy-subrc <> 0.  15.     * Implement suitable error handling here  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删除一些多余的线条。

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


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

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

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

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


SourceCode源代码

constants: lc_sf_htm_mail type string value 'ZSF_SMART_FORM_NAME'.    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: lt_lines type table of tline,          ls_line  type tline,          lt_soli  type soli_tab,          ls_soli  type soli.        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        iv_staff_name      = 'wu'  "iv_staff_name        iv_role            = 'wu'  "iv_role        iv_action          = 'wu'    "iv_action      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.        "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 ).            if iv_attached_pdf is not initial.          "Attach PDF          lv_pdf_size = xstrlen( iv_attached_pdf ).          lv_pdf_content = cl_document_bcs=>xstring_to_solix( ip_xstring = iv_attached_pdf ).              lo_document->add_attachment(             i_attachment_type     = 'PDF'             i_attachment_subject  = 'PDF Attachment'             i_attachment_size     = lv_pdf_size             i_att_content_hex     = lv_pdf_content ).            endif.            lo_send_request->set_document( lo_document ).            "Send to        recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = ' name@company.com ' ).        lo_send_request->add_recipient( i_recipient = recipient ).            "Carbon Copy        recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = 'name@company.com' ).        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.        endif.          catch cx_bcs into bcs_exception.        ev_msg = bcs_exception->get_longtext( ).        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内容

这是我的第一个博客。希望它可以帮助你。请评论和分享您的知识

感谢您的阅读!

干杯!

原创粉丝点击