SAP ABAP: Send Email with attached Internal Table XLS file

Below you will find a quick guide of how to set up a FM in your system that will take as input an internal table, convert that table in to XLS format and send out an email with the attachment.

Z_ED_EMAIL_INTERNAL_TABLE_Import_Parameters

Add the appropriate import parameters.

Z_ED_EMAIL_INTERNAL_TABLE_Table_Parameters

Add appropriate table parameters.

  if recepients[] is initial.
    return.
  endif.

  data: lo_send_request   type ref to cl_bcs,
        lo_document       type ref to cl_document_bcs,
        lo_sender_id      type ref to if_sender_bcs,
        lo_recipient      type ref to if_recipient_bcs.
  data: lo_bcs_exception  type ref to cx_bcs.
  data: lv_bcs_message    type string,
        lv_send_to_all    type os_boolean.

  field-symbols: <fs_recipient> type ad_smtpadr.

*******************************************************************************
* build the file content

  data: lo_struct_type  type ref to cl_abap_structdescr,
        lo_element_type type ref to cl_abap_elemdescr.
  data: lt_components   type cl_abap_structdescr=>component_table,
        ls_field        type dfies.
  field-symbols: <fs_component> type abap_componentdescr.

  data: lt_binary_text  type solix_tab,
        lv_text         type string,
        lv_size         type so_obj_len.

* buidl the header data
  lo_struct_type ?= cl_abap_typedescr=>describe_by_data( data_to_send ).
  lt_components   = lo_struct_type->get_components( ).
  loop at lt_components assigning <fs_component>.
    if sy-tabix gt 1.
      concatenate lv_text cl_bcs_convert=>gc_tab into lv_text.
    endif.
    lo_element_type ?= <fs_component>-type.
    ls_field         = lo_element_type->get_ddic_field( ).
    concatenate lv_text ls_field-scrtext_l into lv_text.
  endloop.
  concatenate lv_text cl_bcs_convert=>gc_crlf into lv_text.

* build the structure of the body
  field-symbols: <fs_row>   type any,
                 <fs_field> type any.
  loop at data_to_send assigning <fs_row>.
    loop at lt_components assigning <fs_component>.
      if sy-tabix gt 1.
        concatenate lv_text cl_bcs_convert=>gc_tab into lv_text.
      endif.
      assign component <fs_component>-name of structure <fs_row> to <fs_field>.
      concatenate lv_text <fs_field> into lv_text.
    endloop.
    concatenate lv_text cl_bcs_convert=>gc_crlf into lv_text.
  endloop.

  try .
      cl_bcs_convert=>string_to_solix(
        exporting
          iv_string   = lv_text
          iv_codepage = '4103' "suitable for MS Excel, leave empty"
          iv_add_bom  = abap_true
        importing
          et_solix    = lt_binary_text
          ev_size     = lv_size ).
    catch cx_bcs into lo_bcs_exception.
      lv_bcs_message = lo_bcs_exception->get_text( ).
      append lv_bcs_message to return.
      exit.
  endtry.

*******************************************************************************
* send an email
  try .
      clear: lo_send_request, lo_document, lo_sender_id.
      lo_send_request = cl_bcs=>create_persistent( ).
      lo_document     = cl_document_bcs=>create_document(
                                  i_type    = 'RAW'
                                  i_text    = email_body[]
                                  i_subject = subject ).

      if not data_to_send[] is initial.
        lo_document->add_attachment(
                   i_attachment_type    = 'XLS'
                   i_attachment_subject = subject
                   i_attachment_size    = lv_size
                   i_att_content_hex    = lt_binary_text ).
      endif.

      lo_send_request->set_document( lo_document ).


      lo_sender_id    = cl_cam_address_bcs=>create_internet_address( sender ).

      lo_send_request->set_sender( lo_sender_id ).
*     add recipients
      loop at recepients assigning <fs_recipient>.
        clear lo_recipient.
        lo_recipient = cl_cam_address_bcs=>create_internet_address( <fs_recipient> ).
*       add recipient with its respective attributes to send request
        lo_send_request->add_recipient( i_recipient = lo_recipient
                                        i_express   = abap_true ).
      endloop.

      lo_send_request->set_status_attributes( i_requested_status = 'E'
                                              i_status_mail      = 'E' ).
      lo_send_request->set_send_immediately( abap_true ).
      lv_send_to_all = lo_send_request->send( i_with_error_screen = abap_true ).

      if lv_send_to_all eq abap_true.
        data: ls_return like line of return.
        message i161(zed) into ls_return.
        append ls_return to return.
      endif.

      commit work.

    catch cx_bcs into lo_bcs_exception.
      lv_bcs_message = lo_bcs_exception->get_text( ).
      append lv_bcs_message to return.
      exit.
  endtry.
  1. Michael Reply
    Hi! What exactly I need to put to those variants to receive XLS file attached? iv_codepage = zed01_4103_code_page "suitable for MS Excel, leave empty" i_type = zed01_email_obj_type_raw i_attachment_type = zed01_email_obj_type_xls
    • Max Titov Reply
      Good catch! I've updated the code above to reference strings instead of constants: iv_codepage = '4103' i_type = 'RAW' i_attachment_type = 'XLS'
  2. Prasad Madusanka Reply
    great tutorial. Thanks machan.....

Leave a Reply

*

captcha *