Send email with excel attachment

Hi,

I already shared how to upload excel file in this link : https://abapdev.wordpress.com/2016/01/08/upload-excel-file-to-an-internal-table/

After upload excel file , we can send e-mail with excel attachment.

Simple abap source code to send mail  :

*&---------------------------------------------------------------------*
*& Report ZSEND_MAIL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZSEND_MAIL.

type-pools : truxs.

*data declerations >

TYPES : BEGIN OF t_sflight ,
CARRID like sflights-CARRID,
CARRNAME like sflights-CARRNAME,
CONNID like sflights-CONNID,
COUNTRYFR like sflights-COUNTRYFR,
CITYFROM like sflights-CITYFROM,
AIRPFROM like sflights-AIRPFROM,
COUNTRYTO like sflights-COUNTRYTO,
CITYTO like sflights-CITYTO,
AIRPTO like sflights-AIRPTO,
FLDATE like sflights-FLDATE,
SEATSMAX like sflights-SEATSMAX,
SEATSOCC like sflights-SEATSOCC,
END OF t_sflight.

data : gt_upload type standard table of t_sflight,
gs_upload type t_sflight,
gt_type type truxs_t_text_data.

"Mail Data
constants:
gc_tab type c value cl_bcs_convert=>gc_tab,
gc_crlf type c value cl_bcs_convert=>gc_crlf.

data send_request type ref to cl_bcs.
data document type ref to cl_document_bcs.
data recipient type ref to if_recipient_bcs.
data bcs_exception type ref to cx_bcs.

data main_text type bcsy_text.
data binary_content type solix_tab.
data size type so_obj_len.
data sent_to_all type os_boolean.
"-------------------------------------------------------------------Mail

*selection screen >
parameter : p_file type rlgrap-filename,"this is getting excel file location
mailto type ad_smtpadr OBLIGATORY."mail receiver
*Search Help for File parameter
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
* PROGRAM_NAME = SYST-CPROG
* DYNPRO_NUMBER = SYST-DYNNR
field_name = 'P_FILE'
IMPORTING
file_name = p_file.

start-of-selection.

PERFORM get_excel_data.
PERFORM create_mail_attachment.
PERFORM send_mail.

*&---------------------------------------------------------------------*
*& Form GET_EXCEL_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* p1 text
* create_persistent( ).

* -------- create and set document with attachment ---------------
* create document object from internal table with text
append 'Dear User' to main_text. "#EC NOTEXT
append '' to main_text.
append 'Flights are attached in this mail.' to main_text.
append '' to main_text.
append 'Best Regards' to main_text.
append 'IT Department' to main_text.

data : lv_subj type string." Mail Subject
data : subj_des TYPE SO_OBJ_DES.
DATA lv_date like sy-datum.
lv_date = sy-datum.
CONCATENATE lv_date+6(2) '-' lv_date+4(2) '-' lv_date+0(4)
INTO lv_subj.
CONCATENATE 'Flights ' lv_subj
INTO lv_subj SEPARATED BY space.
subj_des = lv_subj.
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = main_text
i_subject = subj_des ). "#EC NOTEXT

data :lv_OBJDES TYPE SOOD-OBJDES,
lv_str_OBJDES type string.

CONCATENATE 'Flights -' sy-datum into lv_str_OBJDES
SEPARATED BY space.
lv_OBJDES = lv_str_OBJDES.
* add the spread sheet as attachment to document object
document->add_attachment(
i_attachment_type = 'xls' "#EC NOTEXT
i_attachment_subject = lv_OBJDES "#EC NOTEXT
i_attachment_size = size
i_att_content_hex = binary_content ).

* add document object to send request
send_request->set_document( document ).

* --------- add recipient (e-mail address) -----------------------
* create recipient object
recipient = cl_cam_address_bcs=>create_internet_address( mailto ).

* add recipient object to send request
send_request->add_recipient( recipient ).

* ---------- send document ---------------------------------------
sent_to_all = send_request->send( i_with_error_screen = 'X' ).

commit work.

if sent_to_all is initial.
message i500(sbcoms) with mailto.
else.
message s022(so).
endif.

* ------------ exception handling ----------------------------------
* replace this rudimentary exception handling with your own one !!!
catch cx_bcs into bcs_exception.
message i865(so) with bcs_exception->error_type.
endtry.

ENDFORM. " SEND_MAIL
*&---------------------------------------------------------------------*
*& Form CREATE_MAIL_ATTACHMENT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* string_to_solix(
exporting
iv_string = lv_string
iv_codepage = '4103' "suitable for MS Excel, leave empty
iv_add_bom = 'X' "for other doc types
importing
et_solix = binary_content
ev_size = size ).
catch cx_bcs.
message e445(so).
endtry.

ENDFORM. " CREATE_MAIL_ATTACHMENT
*&---------------------------------------------------------------------*
*& Form CONCATENATE_STRING_TAB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LV_STR_VALUE text
* <--P_LV_STRING text
*----------------------------------------------------------------------*
FORM CONCATENATE_STRING_TAB USING P_VALUE TYPE string
CHANGING P_STRING TYPE string.

CONCATENATE P_STRING
P_VALUE
gc_tab into P_STRING.
ENDFORM. " CONCATENATE_STRING_TAB

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s