Sending Smart Forms in HTML Format
Sending Smart Forms in HTML Format
OF HTML FORMAT.
Yeah there are already documents available on this topic. Only reason why i am posting this document is the
difference in the code. There are some classes availabe, which will be very useful in the process of sending
smart forms in the HTML format.
this report program will give you an idea on how to convert smart form into html format and send it as an email
along with the images(if smart form consists any ).
There is no need of manipulating much on images content. All we need to do is just covert the image into
binary content. There is a class in SAP CRM
cl_crm_email_data , which has one method send_email, which will help us to make this process quite easy.
We can easily embed the images in to the email content. Observe this class closely, may be you can get more
ideas.
Eventhough , i used appropriate comments in the program. It would be better if i tell you some importants
points here only. There is some piece of code in the report program regarding image maniplation. the
basic logic is , if there is one image in the smart form, then there will be one img tag in the converted html
output. This img tag will have one attribute called 'src'. The one possible value for this attribute will be like
this"cid:xxxxxx" .here 'xxxxxx' is image name.
This image name and the value for content id, which we will send to one internal table(see the report
program) , should be same. This will help the email client to find out the embedded image in the email.
i have written this program assuming that smart form will have only one image.you can modify it according to
your requirements. i hope users already have some basic knowledge on XSF format. please refere to below
link if you want to know more about XSF fomrat and parameters we need to send to convert smart form into
HTML format.
http://help.sap.com/SAPHELP_NW04s/helpdata/EN/a5/28d3b6d26211d4b646006094192fe3/content.htm
For the people, who want to investigate furhther on this topic. There is one report program available in SE38
transaction.
SF_XSF_DEMO_MAIL. kindly check it.
I hope it helps you.
Any suggestions are welcome.
REPORT zsmartform_send_mail.
TYPES: BEGIN OF ty_cmail_html_parser,
part
TYPE string,
tag
TYPE string,
attributes
TYPE tihttpnvp,
is_end_tag
TYPE xfeld,
ref_tag_index
TYPE i,
END OF ty_cmail_html_parser.
TYPES: lt_mail_html_parset TYPE STANDARD TABLE OF ty_cmail_html_parser.
TYPE
tsfixml,
lt_archive
TYPE
tsfdara,
lt_graphics
TYPE
tsf_xsf_gr,
lt_part
TYPE
lt_imgsrc
lt_result
TYPE
lt_imageid
match_result_tab,
lt_mail_body
"image ids
TYPE crmt_email_mime_struc,
lt_to
TYPE
crmt_email_recipients,
lt_cc
TYPE
crmt_email_recipients.
*****************local structure***********************************
DATA: ls_output_options
TYPE ssfcompop,
"output options.
ls_xsfparam_line
TYPE ssfxsfp,
ls_job_output_info
TYPE ssfcrescl,
"Joboutput information.
ls_xmloutput
TYPE ssfxmlout,
ls_control_parameters
ls_part
TYPE ssfctrlop,
TYPE REF TO
ls_graphic
TYPE
ssf_xsf_gr,
ls_gr_raw
TYPE
bapiconten,
ls_attribute
TYPE REF TO
ls_imgsrc
ls_imageinfo
"image information
"business service document file content
ihttpnvp,
ls_result
ls_mail_body
"find result.
ls_recep
LIKE LINE OF
"message id.
lt_to.
****************local variables****************************************
DATA: lv_function_name
lv_xstring
TYPE
TYPE
lv_html_xstring
rs38l_fnam,
xstring,
TYPE
lo_conv
lv_html_temp
lv_len
TYPE
TYPE
lv_img_src
lv_activity
i,
TYPE
"html data.
"Length of the string.
string,
TYPE REF TO
lv_imagename
"Xstring data.
string,
TYPE
lo_create_mail
"Xstring data.
xstring,
TYPE REF TO
cl_crm_email_data,
sysuuid_x,
TYPE
string.
***************************constants*************************************
DATA :
lc_flag
lc_outmode
TYPE c
VALUE 'X',
"flag constant
TYPE c
VALUE 'A',
"Application mode
TYPE string
VALUE 'GRAPHICS',
VALUE 'EXTRACT',
VALUE 'CONTENT-ID',
lc_enable
VALUE 'ENABLE',
TYPE string
"parameter
"Parameter
"content id
"enable
lc_img
TYPE string
VALUE 'img',
lc_src
TYPE string
VALUE 'src',
lc_text_html
lc_body_html
TYPE string
VALUE 'body.htm'.
"image tag
"body html .
= lv_form
IMPORTING
fm_name
= lv_function_name
EXCEPTIONS
no_form
=1
no_function_module = 2
OTHERS
= 3.
IF sy-subrc IS INITIAL.
"filling the necesary parameters to get the smartform data
"in XSF (XML OUPUT FOR SMART FORMS) format.
"(XML output for Smart Form) FORMAT.
CLEAR ls_output_options.
ls_output_options-xsf
= lc_flag.
ls_output_options-xsfcmode = lc_flag.
ls_xsfparam_line-name = lc_graphics.
ls_xsfparam_line-value = lc_extract.
" Formatting ON
"#EC NOTEXT
"#EC NOTEXT
"#EC NOTEXT
"#EC NOTEXT
= ls_output_options
user_settings
=''
IMPORTING
job_output_info
= ls_job_output_info
EXCEPTIONS
formatting_error = 1
internal_error
=2
send_error
=3
user_canceled
OTHERS
=4
= 5.
ENDLOOP.
"converting from XSTRING format to STRING format.
lo_conv = cl_abap_conv_in_ce=>create( input = lv_html_xstring ).
lo_conv->read( IMPORTING data = lv_html_temp ).
"now we have our smart form data in the hmtl format .
"we need to extract image information from the html string so that later
"we can embed the image.
"parse the html string tag by tag.
CLEAR lt_part[].
CALL METHOD cl_crm_html_parser=>parse_html_string
EXPORTING
iv_html
= lv_html_temp
iv_all_tags_lower_case = lc_flag
IMPORTING
et_parts
= lt_part.
"finding the source property value of the image (IMG SRC) in the
"html string. so later we can use it when embedding the
"image.
CLEAR ls_part.
LOOP AT lt_part REFERENCE INTO ls_part WHERE tag = lc_img.
CLEAR ls_attribute.
READ TABLE ls_part->attributes REFERENCE INTO ls_attribute
WITH TABLE KEY name = lc_src.
IF sy-subrc IS INITIAL.
IF ls_attribute->value(1) = '"'.
lv_len = STRLEN( ls_attribute->value ) - 2.
lv_img_src = ls_attribute->value+1(lv_len).
ELSE.
lv_img_src = ls_attribute->value.
ENDIF.
REPLACE ALL OCCURRENCES OF 'cid:' IN lv_img_src WITH space.
APPEND lv_img_src TO lt_imgsrc.
ENDIF.
ENDLOOP.
CLEAR lv_len.
lv_len = 1.
"converting each image content into binary format.
LOOP AT lt_graphics INTO ls_graphic.
CLEAR : ls_gr_raw,
lv_html_xstring.
LOOP AT ls_graphic-content INTO ls_gr_raw.
CONCATENATE lv_html_xstring ls_gr_raw-line INTO lv_html_xstring IN BYTE MODE.
CLEAR ls_gr_raw.
ENDLOOP.
CLEAR lv_imagename.
CONCATENATE ls_graphic-graphics '.bmp' INTO lv_imagename.
"embedding the images inside the body.
"here content id which is there in the html string and content id that\
"we are sending in this internal table should be same.
CLEAR ls_mail_body.
ls_mail_body-is_attachment = lc_flag.
ls_mail_body-content_bin = lv_html_xstring.
CLEAR lv_img_src.
READ TABLE lt_imgsrc INTO lv_img_src INDEX lv_len.
IF sy-subrc IS INITIAL.
REPLACE FIRST OCCURRENCE OF lv_img_src IN lv_html_temp WITH lv_imagename.
ENDIF.
ls_mail_body-content_id = lv_imagename.
ls_mail_body-file_name = lv_imagename.
ls_mail_body-mime_type = ls_graphic-httptype.
APPEND ls_mail_body TO lt_mail_body.
lv_len = lv_len + 1.
clear lv_html_xstring.
ENDLOOP.
"filling body of the mail.
CLEAR ls_mail_body.
ls_mail_body-content_ascii = lv_html_temp.
ls_mail_body-mime_type = lc_text_html.
ls_mail_body-file_name = lc_body_html.
APPEND ls_mail_body TO lt_mail_body.
"filling to email address
CLEAR ls_recep.
ls_recep-address = lv_mail .
APPEND ls_recep TO lt_to.
"create mail object.
CREATE OBJECT lo_create_mail.
"moving the subject.
MOVE 'smart form as html mail' TO lo_create_mail->subject.
"moving the body
MOVE lt_mail_body TO lo_create_mail->body.
"moving to addresses
MOVE lt_to TO lo_create_mail->to.
"send the email.
"calling method to send the email
CALL METHOD cl_crm_email_utility_base=>send_email
EXPORTING
iv_mail_data
= lo_create_mail
RECEIVING
ev_send_request_id = lv_activity.
COMMIT WORK.
ENDIF.