FM to send html mail including html table.


importparameters

tables

FUNCTION ZTB_SEND_HTML_MAIL.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(I_SUBJECT) TYPE  STRING OPTIONAL
*"     REFERENCE(I_SENDER) TYPE  STRING
*"  TABLES
*"      IT_BODY TYPE  STRING_TABLE OPTIONAL
*"      IT_TABLE_HEADER TYPE  STRING_TABLE OPTIONAL
*"      IT_TABLE_ITEM TYPE  STRING_TABLE OPTIONAL
*"      IT_END TYPE  STRING_TABLE OPTIONAL
*"      IT_CC TYPE  STRING_TABLE OPTIONAL
*"      IT_RECEIPENTS TYPE  STRING_TABLE
*"      ET_RETURN TYPE  STRING_TABLE OPTIONAL
*"----------------------------------------------------------------------
* Mail Data
data : send_request     type ref to cl_bcs                ,
       document         type ref to cl_document_bcs       ,
       text             type bcsy_text                    ,
       sender_id        type ref to if_sender_bcs         ,
       recipient        type ref to if_recipient_bcs      ,
       bcs_exception    type ref to cx_bcs                ,
       sent_to_all      type os_boolean                   ,
       conlength        type i                            ,
       conlengths       type so_obj_len                   ,
       result_content   type string                       ,
       content_length   type w3param-cont_len             ,
       content_type     type w3param-cont_type            ,
       return_code      type w3param-ret_code             ,
       listobject       type table of abaplist            ,
       html_wa          type w3html                       ,
       html             type standard table of w3html     ,
       wa_rec           type ad_smtpadr                   ,
       bcs_message      type string                       ,
       tmp_str          type string                       .
data : sender           type ad_smtpadr                   ,
       subject          type so_obj_des                   ,
       recepients       type bcsy_smtpa                   ,
       return           type table_of_strings             ,
       lt_receipents    type bcsy_smtpa                   ,
       lt_cc            type bcsy_smtpa                   ,
       e_subrc          type subrc                        ,
       lv_subject       type so_obj_des                   ,
       lt_html          type soli_tab                     ,
       ls_html          type soli.
data:  begin of itab occurs 0                             ,
       line(15)                                           ,
       end of itab                                        ,
       ls_itab       like line of itab                    ,
       lv_string     type string                          ,
       lv_tabix      type sy-tabix                        .
* Conversions
  sender = i_sender.
* Mail Subject.
  lv_subject = i_subject.
* Mail Body
  refresh lt_html.
  ls_html = '<html> <body style="background-color:#FFFFFF;">'.
  append ls_html to lt_html. clear ls_html.
  loop at it_body into lv_string.
    ls_html = '<p>'.
    append ls_html to lt_html. clear ls_html.
    ls_html = lv_string.
    append ls_html to lt_html. clear ls_html.
    ls_html = '</p>'.
    append ls_html to lt_html. clear ls_html.
  endloop.
* Table Header
  if it_table_header[] is not initial.
    read table it_table_header into lv_string index 1.
    if lv_string is not initial.
    split lv_string at'|' into table itab.
    endif.
    if itab[] is not initial.
      clear: ls_html, lv_string.
      concatenate '<table style="MARGIN: 10px; font-size:10px;'
                  '" bordercolor="#90EE90" width="750" '
                  'border="1"><tbody><tr>' into lv_string.
      ls_html = lv_string. clear lv_string.
      append ls_html to lt_html.
    endif.
    loop at itab into ls_itab.
      clear ls_html.
      ls_html = '<th bgcolor="#CCCCCC">'.
      append ls_html to lt_html.
      clear ls_html.
      ls_html = ls_itab.
      append ls_html to lt_html.
      clear ls_html.
      ls_html = '</th>'.
      append ls_html to lt_html.
    endloop.
  endif.
 "Table Item
 if it_table_item[] is not initial.
    clear: itab, itab[].
    loop at it_table_item into lv_string.
      split lv_string at'|' into table itab.
      if itab[] is not initial.
         lv_tabix = 1.
         loop at itab into ls_itab.
          clear ls_html.
          if lv_tabix = 1.
             concatenate '<tr style="background-color:#eeeeee;"><td>' 
             ls_itab '</td>' into ls_html.
          else.
              clear ls_html.
              concatenate '<td>' ls_itab '</td>' into ls_html.
          endif.
          append ls_html to lt_html.
          lv_tabix = lv_tabix + 1.
         endloop.
      endif.
    endloop.
    clear ls_html.
    ls_html = '</tbody> </table>'.
    append ls_html to lt_html.
 endif.
" Conclusion
 loop at it_end into lv_string.
    ls_html = '<p>'.
    append ls_html to lt_html. clear ls_html.
    ls_html = lv_string.
    append ls_html to lt_html. clear ls_html.
    ls_html = '</p>'.
    append ls_html to lt_html. clear ls_html.
 endloop.
 "End of mail..
 clear ls_html.
 ls_html = '</body> </html>'.
 append ls_html to lt_html.
 "Receipents conv.
 loop at it_receipents into lv_string.
    clear wa_rec. wa_rec = lv_string.
    append wa_rec to lt_receipents.
 endloop.
 "CC conv.
 loop at it_cc into lv_string.
    clear wa_rec. wa_rec = lv_string.
    append wa_rec to lt_cc.
 endloop.
try.
   clear send_request .
   send_request = cl_bcs=>create_persistent( ).
   clear document .
   document = cl_document_bcs=>create_document( 
                                               i_type    = 'HTM'
                                                i_text    = lt_html
                                                i_length  = conlengths
                                                i_subject = lv_subject ).
   call method send_request->set_document( document ).
   clear sender_id .
   sender_id =
          cl_cam_address_bcs=>create_internet_address( sender ).
   call method send_request->set_sender
     exporting
       i_sender = sender_id.
   clear wa_rec .
   loop at lt_receipents into wa_rec .
     clear recipient .
     recipient = cl_cam_address_bcs=>create_internet_address(
     wa_rec ).
     call method send_request->add_recipient
       exporting
         i_recipient = recipient
         i_express   = 'X'.
   endloop .
   loop at lt_cc into wa_rec.
     clear recipient .
     recipient = cl_cam_address_bcs=>create_internet_address(
     wa_rec ).
     call method send_request->add_recipient
       exporting
         i_recipient = recipient
         i_express   = 'X'
         i_copy      = 'X'.
     endloop.
      call method send_request->set_status_attributes
        exporting
          i_requested_status = 'E'
          i_status_mail      = 'E'.
      call method send_request->set_send_immediately( 'X' ).
      call method send_request->send(
      exporting
      i_with_error_screen = 'X'
      receiving
      result = sent_to_all ).
      if sent_to_all = 'X'.
        append 'Mail sent successfully ' to et_return .
        e_subrc = 0.
      else.
        e_subrc = 4.
      endif.
      commit work.
    catch cx_bcs into bcs_exception.
      bcs_message = bcs_exception->get_text( ).
      append bcs_message to et_return .
      exit.
  endtry.
ENDFUNCTION.

Leave a comment

Your email address will not be published. Required fields are marked *