Jan 15, 2016

ABAP: Send Email with attached Internal Table XLS file

Function module that will take any internal table as input,  convert in to XLS format
and send out emails with the attachment.

 Add the appropriate import parameters.

Add appropriate table parameters.









Source Code
====================================================
function zinttab_excl_email.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(SENDER) TYPE  AD_SMTPADR
*"     REFERENCE(SUBJECT) TYPE  SO_OBJ_DES
*"  TABLES
*"      RECEPIENTS TYPE  BCSY_SMTPA
*"      RETURN TYPE  TABLE_OF_STRINGS OPTIONAL
*"      DATA_TO_SEND
*"      EMAIL_BODY TYPE  BCSY_TEXT
*"----------------------------------------------------------------------
*Function module that will take any internal table as input,  convert in to XLS format
*and send out emails with the attachment. :SATYABRATA SAHOO
*"----------------------------------------------------------------------
  if recepients[] is initial.
    return.
  endif.

  datalo_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.
  datalo_bcs_exception  type ref to cx_bcs.
  datalv_bcs_message    type string,
        lv_send_to_all    type os_boolean,
        lv_fields(10).

  field-symbols< fs_recipient > type ad_smtpadr.

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

  datalo_struct_type  type ref to cl_abap_structdescr,
        lo_element_type type ref to cl_abap_elemdescr.
  datalt_components   type cl_abap_structdescr=>component_table,
        ls_field        type dfies.
  field-symbols type abap_componentdescr.

  datalt_binary_text  type solix_tab,
        lv_text         type string,
        lv_size         type so_obj_len.

* build the header data
  lo_struct_type ?= cl_abap_typedescr=>describe_by_datadata_to_send ).
  lt_components   lo_struct_type->get_components).
  loop at lt_components assigning .
    if sy-tabix gt 1.
      concatenate lv_text cl_bcs_convert=>gc_tab into lv_text.
    endif.
    lo_element_type ?= -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   type any,
                  type any.
  loop at data_to_send assigning .
    loop at lt_components assigning .
      if sy-tabix gt 1.
        concatenate lv_text cl_bcs_convert=>gc_tab into lv_text.
      endif.
      assign component -name of structure  to .
      lv_fields .
      condense lv_fields.
*      concatenate lv_text  into lv_text.
      concatenate lv_text lv_fields 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 .
      clearlo_send_requestlo_documentlo_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_documentlo_document ).


      lo_sender_id    cl_cam_address_bcs=>create_internet_addresssender ).

      lo_send_request->set_senderlo_sender_id ).
*     add recipients
      loop at recepients assigning .
        clear lo_recipient.
        lo_recipient cl_cam_address_bcs=>create_internet_address ).
*       add recipient with its respective attributes to send request
        lo_send_request->add_recipienti_recipient lo_recipient
                                        i_express   abap_true ).
      endloop.

      lo_send_request->set_status_attributesi_requested_status 'E'
                                              i_status_mail      'E' ).
      lo_send_request->set_send_immediatelyabap_true ).
      lv_send_to_all lo_send_request->sendi_with_error_screen abap_true ).

      if lv_send_to_all eq abap_true.
        datals_return like line of return.
        ls_return text-001"Success
        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.
endfunction.

============================================

Ref-http://techsplice.com/

How to change Transport request from Released to Modifiable

Step 1: Go to SE38 – Execute Program RDDIT076.  Step 2: Give your released requests number and execute again. Step 3: After executing, yo...