IT干货网

ABAP-HTML-MAIL

lxf 2022年03月09日 SAP 166 0

1.邮件-方法类

 2.方法

2.1 Define

 1 class ZCTR0600 definition 
 2   public 
 3   final 
 4   create public . 
 5  
 6 public section. 
 7  
 8   types: 
 9     begin of ty_html_head, 
10       hfgcolor  type wwwcolor, 
11       hbgcolor  type wwwcolor, 
12       lfgcolor  type wwwcolor, 
13       lbgcolor  type wwwcolor, 
14       size      type w3size, 
15       attrb     type w3html, 
16       tabhd     type w3head, 
17     end of ty_html_head . 
18   types: 
19     begin of ty_html_fcat, 
20     text  type w3head-text, 
21     end of ty_html_fcat . 
22   types: 
23     tt_html_fcat  type table of ty_html_fcat . 
24   types: 
25     tt_html_html  type table of w3html . 
26   types: 
27     begin of ty_mail_head, 
28         sender        type ad_smtpadr, 
29         mailtype      type so_obj_tp, 
30         subject       type so_obj_des, 
31         atta_subject  type so_obj_des, 
32         end of ty_mail_head . 
33   types TY_MAIL_RECE type ADR6 . 
34   types TY_MAIL_ATTA type RMPS_POST_CONTENT . 
35   types TY_MAIL_BODY type SOLI . 
36   types: 
37     tt_mail_rece  type table of ty_mail_rece . 
38   types: 
39     tt_mail_atta  type table of ty_mail_atta . 
40   types: 
41     tt_mail_body  type table of ty_mail_body . 
42  
43   class-methods SEND_MAIL 
44     importing 
45       value(IS_MAIL_HEAD) type TY_MAIL_HEAD optional 
46       value(IT_MAIL_BODY) type TT_MAIL_BODY optional 
47       value(IT_MAIL_ATTA) type TT_MAIL_ATTA optional 
48       value(IT_MAIL_RECE) type TT_MAIL_RECE optional 
49     exporting 
50       value(ER_ROOT) type ref to ZCX_ROOT . 
51   class-methods MAIL_HTML 
52     importing 
53       value(IS_HTML_HEAD) type TY_HTML_HEAD optional 
54       value(IT_HTML_FCAT) type TT_HTML_FCAT optional 
55       value(IT_HTML_ANY) type ANY optional 
56     exporting 
57       value(ER_ROOT) type ref to ZCX_ROOT 
58       value(ET_HTML) type TT_HTML_HTML . 
59   class-methods MAIL_ATTA 
60     importing 
61       value(IV_STRING) type STRING optional 
62     exporting 
63       value(ES_ATTA) type RMPS_POST_CONTENT 
64       value(ER_ROOT) type ref to ZCX_ROOT .
View Code

2.1 SEND_MAIL

 1   method mail_atta. 
 2   data: lv_size type so_obj_len. 
 3   try. 
 4     try. 
 5       cl_bcs_convert=>string_to_solix( 
 6         exporting 
 7           iv_string   = iv_string 
 8           iv_codepage = '4103'          "suitable for MS Excel, leave empty 
 9           iv_add_bom  = abap_true       "for other doc types 
10         importing 
11           et_solix    = es_atta-cont_hex 
12           ev_size     = lv_size 
13           ). 
14  
15       "es_atta-docsize = lv_size. 
16       catch cx_bcs into data(lr_cx_bcs). 
17       data(lv_txt) = lr_cx_bcs->get_text( ). 
18       raise exception type zcx_root 
19         exporting 
20           textid = value #( 
21                      msgid = '00' 
22                      msgno = '001' 
23                      attr1 = lv_txt 
24                      ). 
25     endtry. 
26     catch zcx_root into data(lr_root). 
27     er_root = lr_root. 
28   endtry. 
29   endmethod.
View Code

2.2 MAIL_HTML

  1   method mail_html. 
  2   data: lr_ref    type ref to data. 
  3   data: lt_header type table of w3head, 
  4         lt_fields type table of w3fields, 
  5         lt_html   type table of w3html. 
  6  
  7   try. 
  8  
  9     data: 
 10       lr_any_t  type ref to data, 
 11       lr_any_s  type ref to data. 
 12     data: 
 13       lr_dref   type ref to data, 
 14       lr_stru   type ref to cl_abap_structdescr, 
 15       lr_table  type ref to cl_abap_tabledescr, 
 16       lr_elem   type ref to cl_abap_elemdescr, 
 17       lt_para   type cl_abap_structdescr=>component_table, 
 18       lt_comp   type cl_abap_structdescr=>component_table. 
 19     field-symbols: 
 20       <fs_tab>  type any, 
 21       <ft_tab>  type standard table. 
 22  
 23     try. 
 24  
 25       lr_table ?= cl_abap_tabledescr=>describe_by_data( it_html_any ). 
 26  
 27 *      loop at lr_stru->components[] into data(ls_comps). 
 28 *        if ls_comps-type_kind = 'P' 
 29 *        or ls_comps-type_kind = 'X'. 
 30 *        else. 
 31 *          ls_comps-length = ls_comps-length / 2. 
 32 *        endif. 
 33 * 
 34 *        append initial line to lt_comp assigning field-symbol(<fs_comp>). 
 35 *        <fs_comp>-name  =   ls_comps-name. 
 36 *        <fs_comp>-type  ?=  cl_abap_elemdescr=>get_by_kind( 
 37 *                            p_type_kind = ls_comps-type_kind 
 38 *                            p_length = ls_comps-length 
 39 *                            p_decimals = ls_comps-decimals ). 
 40 *        unassign <fs_comp>. 
 41 *      endloop. 
 42 * 
 43 *      if lt_comp[] is not initial. 
 44 *        clear:lr_stru,lr_table. 
 45 *        lr_stru   =   cl_abap_structdescr=>create( lt_comp ). 
 46 *        lr_table  =   cl_abap_tabledescr=>create( lr_stru ). 
 47 * 
 48 *        create data lr_any_s type handle lr_stru. 
 49 *        assign lr_any_s->* to <fs_tab>. 
 50 * 
 51 *        create data lr_any_t type handle lr_table. 
 52 *        assign lr_any_t->* to <ft_tab>. 
 53 *      endif. 
 54  
 55       if lr_table is not initial. 
 56         create data lr_any_t type handle lr_table. 
 57         assign lr_any_t->* to <ft_tab>. 
 58       endif. 
 59  
 60       move-corresponding it_html_any to <ft_tab>[]. 
 61  
 62       catch cx_root into data(lr_rt). 
 63       data(lv_txt) = lr_rt->get_text( ). 
 64       raise exception type zcx_root 
 65         exporting 
 66           textid = value #( 
 67                      msgid = '00' 
 68                      msgno = '001' 
 69                      attr1 = lv_txt 
 70                      ). 
 71     endtry. 
 72  
 73     loop at it_html_fcat into data(ls_html_fcat). 
 74       call function 'WWW_ITAB_TO_HTML_HEADERS' 
 75         exporting 
 76           field_nr = sy-tabix 
 77           text     = ls_html_fcat-text 
 78           fgcolor  = is_html_head-hfgcolor 
 79           bgcolor  = is_html_head-hbgcolor 
 80         tables 
 81           header   = lt_header. 
 82  
 83       call function 'WWW_ITAB_TO_HTML_LAYOUT' 
 84         exporting 
 85           field_nr = sy-tabix 
 86           fgcolor  = is_html_head-lfgcolor 
 87           bgcolor  = is_html_head-lbgcolor 
 88           size     = is_html_head-size 
 89         tables 
 90           fields   = lt_fields. 
 91     endloop. 
 92  
 93     call function 'WWW_ITAB_TO_HTML' 
 94       exporting 
 95         table_attributes  = is_html_head-attrb 
 96         table_header      = is_html_head-tabhd 
 97       tables 
 98         html              = lt_html 
 99         fields            = lt_fields 
100         row_header        = lt_header 
101         itable            = <ft_tab>[]. 
102  
103     et_html[] = lt_html[]. 
104  
105     catch zcx_root into data(lr_root). 
106     er_root = lr_root. 
107   endtry. 
108   endmethod.
View Code

2.3 MAIL_ATTA

 1   method mail_atta. 
 2   data: lv_size type so_obj_len. 
 3   try. 
 4     try. 
 5       cl_bcs_convert=>string_to_solix( 
 6         exporting 
 7           iv_string   = iv_string 
 8           iv_codepage = '4103'          "suitable for MS Excel, leave empty 
 9           iv_add_bom  = abap_true       "for other doc types 
10         importing 
11           et_solix    = es_atta-cont_hex 
12           ev_size     = lv_size 
13           ). 
14  
15       "es_atta-docsize = lv_size. 
16       catch cx_bcs into data(lr_cx_bcs). 
17       data(lv_txt) = lr_cx_bcs->get_text( ). 
18       raise exception type zcx_root 
19         exporting 
20           textid = value #( 
21                      msgid = '00' 
22                      msgno = '001' 
23                      attr1 = lv_txt 
24                      ). 
25     endtry. 
26     catch zcx_root into data(lr_root). 
27     er_root = lr_root. 
28   endtry. 
29   endmethod.
View Code

3.调用

  1 form frm_mail_send_b  tables   ft_item    structure wa_alv 
  2                                ft_return  structure bapiret2 
  3                       using    fs_head    like      wa_alv. 
  4  
  5   constants: lc_tab   type c value cl_abap_char_utilities=>horizontal_tab. 
  6   data: lr_root       type ref to zcx_root. 
  7   data: ls_mail_head  type  zctr0600=>ty_mail_head. 
  8   data: lt_mail_body  type  zctr0600=>tt_mail_body, 
  9         lt_mail_atta  type  zctr0600=>tt_mail_atta, 
 10         lt_mail_rece  type  zctr0600=>tt_mail_rece. 
 11   data: ls_html_head  type  zctr0600=>ty_html_head. 
 12   data: lt_html_fcat  type  zctr0600=>tt_html_fcat, 
 13         lt_html_any   like  table of wa_mail, 
 14         lt_html       type  table of w3html. 
 15   data: lv_string     type  string, 
 16         ls_atta       type  zctr0600=>ty_mail_atta. 
 17  
 18   check fs_head-smtp_type = 'B'. 
 19  
 20   try. 
 21  
 22   ls_html_head-hfgcolor   =  'white'. 
 23   ls_html_head-hbgcolor   =  'blue'. 
 24   ls_html_head-lfgcolor   =  'black'. 
 25   ls_html_head-lbgcolor   =  ''. 
 26   ls_html_head-size       =  '3'. 
 27   ls_html_head-attrb-line =  'BORDER=0'. 
 28   ls_html_head-tabhd-text =  text-101. 
 29   ls_html_head-tabhd-font =  'Arial'. 
 30   ls_html_head-tabhd-size =  '2'. 
 31  
 32   loop at ft_item into data(ls_item). 
 33     append initial line to lt_html_any assigning field-symbol(<fs_html_any>). 
 34     move-corresponding ls_item to <fs_html_any>. 
 35     unassign <fs_html_any>. 
 36   endloop. 
 37  
 38   perform frm_mail_send_fcat tables lt_html_fcat. 
 39  
 40   zctr0600=>mail_html( 
 41     exporting 
 42       is_html_head  = ls_html_head 
 43       it_html_fcat  = lt_html_fcat 
 44       it_html_any   = lt_html_any 
 45     importing 
 46       er_root       = lr_root 
 47       et_html       = lt_html 
 48       ). 
 49   if lr_root is not initial. 
 50     raise exception lr_root. 
 51   endif. 
 52  
 53   loop at lt_html into data(ls_html). 
 54     lv_string = lv_string && ls_html-line. 
 55   endloop. 
 56  
 57   zctr0600=>mail_atta( 
 58     exporting 
 59       iv_string     = lv_string 
 60     importing 
 61       es_atta       = ls_atta 
 62       er_root       = lr_root 
 63       ). 
 64   if lr_root is not initial. 
 65     raise exception lr_root. 
 66   endif. 
 67  
 68   ls_mail_head-subject      = text-100. 
 69   ls_mail_head-sender       = space. 
 70   ls_mail_head-mailtype     = 'HTM'. 
 71   ls_mail_head-atta_subject = space. 
 72  
 73   append initial line to lt_mail_atta assigning field-symbol(<fs_mail_atta>). 
 74   <fs_mail_atta>-doc_type = 'HTM'. 
 75   <fs_mail_atta>-subject  = text-102. 
 76   <fs_mail_atta>-filename = text-102. 
 77   <fs_mail_atta>-objtp    = 'HTML'. 
 78   <fs_mail_atta>-cont_hex = ls_atta-cont_hex. 
 79   unassign <fs_mail_atta>. 
 80  
 81   append initial line to lt_mail_rece assigning field-symbol(<fs_mail_rece>). 
 82   <fs_mail_rece>-smtp_addr  = fs_head-smtp_addr. 
 83   unassign <fs_mail_rece>. 
 84  
 85   zctr0600=>send_mail( 
 86     exporting 
 87       is_mail_head  = ls_mail_head 
 88       it_mail_rece  = lt_mail_rece 
 89       it_mail_body  = lt_mail_body 
 90       it_mail_atta  = lt_mail_atta 
 91     importing 
 92       er_root       = lr_root 
 93       ). 
 94     if lr_root is not initial. 
 95       raise exception lr_root. 
 96     endif. 
 97     catch zcx_root into lr_root. 
 98     data(lv_err) = lr_root->get_text( ). 
 99     perform frm_get_message 
100     tables  ft_return 
101     using   'E' 
102             '00' 
103             '001' 
104             fs_head-smtp_addr 
105             lv_err 
106             space 
107             space. 
108   endtry. 
109 endform.
View Code

评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

ABAP-Logs-SLGD