top of page

SAP ABAP - Send e-mail with excel file ( Simple )

Updated: Aug 24, 2022


PARAMETERS: p_email   TYPE somlreci1-receiver
DEFAULT 'test@test.com.tr'.

TYPES: BEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         maktx TYPE makt-maktx,
         meins TYPE mara-meins,
         mtart TYPE mara-mtart,

END OF ty_mara.
DATA: gt_mara TYPE TABLE OF ty_mara.



DATA:   gt_message TYPE TABLE OF solisti1.
DATA:   gt_attach TYPE TABLE OF solisti1.

DATA: gt_packing_list  TYPE TABLE OF sopcklsti1,
      gt_contents      TYPE TABLE OF solisti1,
      gt_receivers     TYPE TABLE OF somlreci1,
      gt_attachment    TYPE TABLE OF solisti1,
      gt_object_header TYPE TABLE OF solisti1,
      gv_cnt           TYPE i,
      gv_sent_all(1)   TYPE c,
      gs_doc_data      LIKE sodocchgi1,
      gv_error         TYPE sy-subrc,
      gv_reciever      TYPE sy-subrc.


START-OF-SELECTION.

PERFORM data_retrieval.

PERFORM build_xls_data_table.



END-OF-SELECTION.
* Populate message body text
PERFORM populate_email_message_body.

* Send file by email as .xls speadsheet
PERFORM send_file_as_email_attachment
TABLES gt_message
                                      gt_attach
USING p_email
'Example .xls doc attachment'
'XLS'
'EXCEL FILENAME'
' '
' '
' '
CHANGING gv_error
                                      gv_reciever.


PERFORM initiate_mail_execute_program.


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
SELECT mara~matnr,
         makt~maktx,
         mara~meins,
         mara~mtart
FROM mara
  INNER JOIN makt
ON mara~matnr = makt~matnr
AND makt~spras = @sy-langu
INTO TABLE @gt_mara.
ENDFORM.                    " DATA_RETRIEVAL


*&---------------------------------------------------------------------*
*&      Form  BUILD_XLS_DATA_TABLE
*&---------------------------------------------------------------------*
*       Build data table for .xls document
*----------------------------------------------------------------------*
FORM build_xls_data_table.

APPEND INITIAL LINE TO gt_attach REFERENCE INTO DATA(lr_attach).
CONCATENATE 'MATNR' 'MAKTX' 'MEINS' 'MTART'
INTO lr_attach->* SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
CONCATENATE cl_abap_char_utilities=>cr_lf lr_attach->*  INTO lr_attach->*.


LOOP AT gt_mara REFERENCE INTO DATA(lr_mara).
APPEND INITIAL LINE TO gt_attach REFERENCE INTO lr_attach.
CONCATENATE lr_mara->matnr
                lr_mara->maktx
                lr_mara->meins
                lr_mara->mtart
INTO lr_attach->* SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
CONCATENATE cl_abap_char_utilities=>cr_lf lr_attach->* INTO lr_attach->*.

ENDLOOP.
ENDFORM.                    " BUILD_XLS_DATA_TABLE


*&---------------------------------------------------------------------*
*&      Form  SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------*
*       Send email
*----------------------------------------------------------------------*
FORM send_file_as_email_attachment TABLES pit_message
                                          pit_attach
USING p_email
                                          p_mtitle
                                          p_format
                                          p_filename
                                          p_attdescription
                                          p_sender_address
                                          p_sender_addres_type
CHANGING p_error
                                          p_reciever.


DATA: ld_error               TYPE sy-subrc,
        ld_reciever            TYPE sy-subrc,
        ld_mtitle              LIKE sodocchgi1-obj_descr,
        ld_email               LIKE  somlreci1-receiver,
        ld_format              TYPE  so_obj_tp,
        ld_attdescription      TYPE  so_obj_nam,
        ld_attfilename         TYPE  so_obj_des,
        ld_sender_address      LIKE  soextreci1-receiver,
        ld_sender_address_type LIKE  soextreci1-adr_typ,
        ld_receiver            LIKE  sy-subrc.

  ld_email   = p_email.
  ld_mtitle = p_mtitle.
  ld_format              = p_format.
  ld_attdescription      = p_attdescription