Author: Subrahmanya Pindiproli
Submitted: 14-May-2012
HTML Formatted attachments using ABAP
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'*-----------------------------------------------------------------------* * Program Name : ZRPT_FORMATTED_EMAIL_1 * * Author : Subrahmanya Pindiproli * * Requested By : N.A * * Date created : 2012/04/12 * * SAP Release : ECC6.0 * * Project Name : N.A * * Task # : * * Description : Formatted Emails using HTML in ABAP using the * * FM : SO_NEW_DOCUMENT_ATT_SEND_API1 * *-----------------------------------------------------------------------* * Change History * *-----------------------------------------------------------------------* * * * * *-----------------------------------------------------------------------* REPORT zrpt_formatted_email_1. *-----------------------------------------------------------------------* * D A T A D E C L A R A T I O N S * *-----------------------------------------------------------------------* DATA: t_objtxt TYPE STANDARD TABLE OF solisti1, " Message body t_objpack TYPE STANDARD TABLE OF sopcklsti1, " Packing list t_reclist TYPE STANDARD TABLE OF somlreci1, " Receipient list t_html TYPE STANDARD TABLE OF w3html. " Html DATA: lv_tmp_str TYPE string, wa_htmlline TYPE w3html. DATA : t_table TYPE STANDARD TABLE OF ssfbin, t_attachment TYPE STANDARD TABLE OF solix. DATA: wa_docdata TYPE sodocchgi1, " Document data wa_objtxt TYPE solisti1, " Message body *wa_objbin TYPE solisti1, " Attachment data wa_objpack TYPE sopcklsti1, " Packing list wa_reclist TYPE somlreci1. " Receipient list *DATA: w_tab_lines TYPE i. " Table lines * Internal Table for Data in Email Body TYPES : BEGIN OF ty_mara, matnr TYPE matnr, bismt TYPE bismt, groes TYPE groes, END OF ty_mara. DATA : t_mara TYPE STANDARD TABLE OF ty_mara, * l_count TYPE i, v_ld_sender_address LIKE soextreci1-receiver, v_ld_sender_address_type LIKE soextreci1-adr_typ, v_sent_all(1) TYPE c. * Constants CONSTANTS : c_u TYPE so_escape VALUE 'U'. "Specification of recipient type * c_x TYPE sonv-flag VALUE 'X'. *-----------------------------------------------------------------------* * S E L E C T I O N S C R E E N * *-----------------------------------------------------------------------* PARAMETERS: p_email TYPE char120 OBLIGATORY VISIBLE LENGTH 40 LOWER CASE. *-----------------------------------------------------------------------* * S T A R T O F S E L E C T I O N * *-----------------------------------------------------------------------* START-OF-SELECTION. * Get Report Data PERFORM get_data. * Process Data ( Placeholder Subroutine ) PERFORM process_data. * Creating message PERFORM create_message. * Sending Message PERFORM send_message. *&---------------------------------------------------------------------* *& Form create_message *&---------------------------------------------------------------------* * Create the Email Title, Description, Body and populate the * * receiver list. * *----------------------------------------------------------------------* FORM create_message . **1 Title, Description & Body PERFORM create_title_desc_body. **2 Receivers PERFORM fill_receivers. ENDFORM. " create_message *&---------------------------------------------------------------------* *& Form CREATE_TITLE_DESC_BODY *&---------------------------------------------------------------------* * Title, Description and body *----------------------------------------------------------------------* FORM create_title_desc_body. PERFORM itab_to_html. PERFORM build_email_body. ENDFORM. " CREATE_TITLE_DESC_BODY *&---------------------------------------------------------------------* *& Form fill_receivers *&---------------------------------------------------------------------* * Filling up the Receivers *----------------------------------------------------------------------* FORM fill_receivers . wa_reclist-receiver = p_email. wa_reclist-rec_type = c_u. APPEND wa_reclist TO t_reclist. CLEAR wa_reclist. ENDFORM. " fill_receivers *&---------------------------------------------------------------------* *& Form send_message *&---------------------------------------------------------------------* * Sending Mail *----------------------------------------------------------------------* FORM send_message . * function module to send email IF NOT t_attachment[] IS INITIAL . *Describe the body of the message CLEAR wa_objpack. REFRESH t_objpack. wa_objpack-transf_bin = space. wa_objpack-head_start = 1. wa_objpack-head_num = 0. wa_objpack-body_start = 1. DESCRIBE TABLE t_objtxt LINES wa_objpack-body_num. wa_objpack-doc_type = 'RAW'. APPEND wa_objpack TO t_objpack. *Create attachment notification wa_objpack-transf_bin = 'X'. wa_objpack-head_start = 1. wa_objpack-head_num = 1. wa_objpack-body_start = 1. *Create packing list DESCRIBE TABLE t_attachment LINES wa_objpack-body_num. wa_objpack-doc_type = 'HTML'. wa_objpack-obj_descr = 'Material Master Data'. wa_objpack-doc_size = wa_objpack-body_num * 255. APPEND wa_objpack TO t_objpack. CLEAR v_sent_all. *Call FM to send e-mail with attachment CALL FUNCTION 'SO_DOCUMENT_SEND_API1' EXPORTING document_data = wa_docdata put_in_outbox = 'X' sender_address = v_ld_sender_address sender_address_type = v_ld_sender_address_type commit_work = 'X' IMPORTING sent_to_all = v_sent_all TABLES packing_list = t_objpack contents_hex = t_attachment contents_txt = t_objtxt receivers = t_reclist EXCEPTIONS too_many_receivers = 1 document_not_sent = 2 document_type_not_exist = 3 operation_no_authorization = 4 parameter_error = 5 x_error = 6 enqueue_error = 7 OTHERS = 8. IF sy-subrc NE 0. WRITE: 'Sending Failed'. ELSE. WRITE: 'Sending Successful'. ENDIF. ENDIF. ENDFORM. " send_message *&---------------------------------------------------------------------* *& Form GET_DATA *&---------------------------------------------------------------------* * A core subroutine to fetch the data from the corresponding * * database tables. * *----------------------------------------------------------------------* FORM get_data . REFRESH t_mara. SELECT matnr bismt groes UP TO 10 ROWS FROM mara INTO TABLE t_mara. IF sy-subrc EQ 0. SORT t_mara BY matnr. ENDIF. ENDFORM. " GET_DATA *&---------------------------------------------------------------------* *& Form PROCESS_DATA *&---------------------------------------------------------------------* * This is a placeholder subroutine and can be used for processing* * the report data and getting into final internal tables before * * starting the HTML formatting and emailing the data * *----------------------------------------------------------------------* FORM process_data . ENDFORM. " PROCESS_DATA *&---------------------------------------------------------------------* *& Form ITAB_TO_HTML *&---------------------------------------------------------------------* * This is a subroutine which uses standard SAP Function Modules * * WWW_ITAB_TO_HTML_HEADERS, WWW_ITAB_TO_HTML_LAYOUT and * * WWW_ITAB_TO_HTML * *----------------------------------------------------------------------* FORM itab_to_html. DATA: t_header TYPE STANDARD TABLE OF w3head WITH HEADER LINE, " Header t_fields TYPE STANDARD TABLE OF w3fields WITH HEADER LINE, " Fields fs_header TYPE w3head, w_head TYPE w3head. DATA: t_fcat TYPE lvc_t_fcat, " Fieldcatalog wa_fcat LIKE LINE OF t_fcat. *-Populate Fieldcatalog REFRESH t_fcat. CLEAR wa_fcat. wa_fcat-coltext = 'Material Number'. APPEND wa_fcat TO t_fcat. wa_fcat-coltext = 'Old Material No.'. APPEND wa_fcat TO t_fcat. wa_fcat-coltext = 'Size/Dimensions'. APPEND wa_fcat TO t_fcat. *-Fill the Column headings and Properties LOOP AT t_fcat INTO wa_fcat. w_head-text = wa_fcat-coltext. *-Populate the Column Headings CALL FUNCTION 'WWW_ITAB_TO_HTML_HEADERS' EXPORTING field_nr = sy-tabix text = w_head-text fgcolor = 'black' bgcolor = 'red' TABLES header = t_header. *-Populate Column Properties CALL FUNCTION 'WWW_ITAB_TO_HTML_LAYOUT' EXPORTING field_nr = sy-tabix fgcolor = 'black' size = '7' TABLES fields = t_fields. ENDLOOP. *-Title of the Display fs_header-text = 'Automated Emails from SAP Applications'. fs_header-font = 'Arial'. fs_header-size = '2'. *-Preparing the HTML from Intenal Table REFRESH t_html. CALL FUNCTION 'WWW_ITAB_TO_HTML' EXPORTING table_header = fs_header TABLES html = t_html fields = t_fields row_header = t_header itable = t_mara. LOOP AT t_html INTO wa_htmlline. CONCATENATE lv_tmp_str wa_htmlline INTO lv_tmp_str. ENDLOOP . **** Convert HTML data in pit_attach-content to RAW data format IF NOT lv_tmp_str IS INITIAL. CALL FUNCTION 'SSFH_STRING_TO_TABUTF8' EXPORTING cstr_input_data = lv_tmp_str codepage = '4110' * IMPORTING * OSTR_INPUT_DATA_L = TABLES ostr_input_data = t_table EXCEPTIONS conversion_error = 1 internal_error = 2 OTHERS = 3. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. t_attachment[] = t_table[]. REFRESH: t_table[]. ENDIF. ENDFORM. "ITAB_TO_HTML *&---------------------------------------------------------------------* *& Form BUILD_EMAIL_BODY *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM build_email_body . *...Title wa_docdata-obj_name = 'This is the Email Subject'. *...Description wa_docdata-obj_descr = 'Description of Email with a max length of 50 Chars'. wa_objtxt-line = 'Dear Sir / Madam'. APPEND wa_objtxt TO t_objtxt. wa_objtxt-line = 'This is an Automated Email from SAP Applications. '. APPEND wa_objtxt TO t_objtxt. CLEAR wa_objtxt. APPEND wa_objtxt TO t_objtxt. CLEAR wa_objtxt. APPEND wa_objtxt TO t_objtxt. CLEAR wa_objtxt. APPEND wa_objtxt TO t_objtxt. * Signature with background color wa_objtxt-line = 'Best Regards,'. APPEND wa_objtxt TO t_objtxt. wa_objtxt-line = 'Subrahmanya Pindiproli'. APPEND wa_objtxt TO t_objtxt. wa_objtxt-line = 'Principal Consultant - ERP'. APPEND wa_objtxt TO t_objtxt. ENDFORM. " BUILD_EMAIL_BODY