Send HTML Mail using Smartforms
Hi everyone, in this blog I will share my two cents worth in sending notification email in SAP.
In my project, I have to write a customized Web Dynpro Application to facilitate a business process for user. Most of the activities 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.
The total different emails will have to be sent is more than 10. I have to find an effective way to send out the emails.
Even though there are more than 10 email contents, more or less they have the same structures. <<Header>><<Body>><<Footer>>
E.g. <<Footer>> is always like “This is a generated computer message. Please do not reply.”
This means if I find an effective way to group the email and write the code, maintenance will be much more easier for me if user requires any changes in the
future. I start Google and search for ideas on SCN. Some of the solutions I found:
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 my criteria of structuring Email Contents for easy maintenance.
This is when I came to think of using Smartforms. Smartforms with its Control Flow and Condition will help me in achieving my objective. I just need to create a Smartform to use as Email Templates for all of my notification.
Generally, the solution will look like, I call Smartforms through my code –> pass the parameters (such as Which template I want to used? Content of that template?) –> Received back the content, do something with it –> Send Mail.
Firstly I have to create my Email Templates through Smartforms. I will structure my Email Templates using Smartforms, by creating new Folder, new Conditions.
With Conditions 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
After that, I just need to activate the Smartform. From my Web Dynpro side, let say a method in Assistance Class, I will call my Smartforms, passing the parameters indicated the template I will used, any necessary contents for the email.
After calling the Smartforms, it will return a structure of type SSFCRESCL that keeps the content in a OTF field.
In Debug mode, we noticed that the OTF data is readable, with content of the email and some weird numbers.
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.
Our job now is to convert from OTF to SOLI while still keeping the correct content of the email.
First we will convert the OTF table 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. * Implement suitable error handling here endif.
Look much more readable, isn’t it? Now all we have to do is to convert it to SOLI by deleting few redundant lines.
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.
Perfect! We can now use this content to send out the email.
lo_document = cl_document_bcs=>create_document( i_type = 'HTM' i_subject = 'Mail Subject' i_text = lt_soli ).
Written with HTML in Smartforms, out Email content can look like this.
*---Constant declaration constants: lc_sf_htm_mail type string value 'ZSF_SMART_FORM_NAME'. *---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 = iv_staff_name iv_role = iv_role iv_action = 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 = 'email@example.com' ). lo_send_request->add_recipient( i_recipient = recipient ). "Carbon Copy recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = 'firstname.lastname@example.org' ). 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.
Above is the steps I used to send HTML mail using Smartforms.The general idea is convert from data from OTF –> TLINE –> SOLI
The advantages 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 maintained easily under one Smartform.
– HTML content 🙂
This is my very first blog. Hope it helps you. Please comment and share your knowledge 🙂
Thank you for reading!