01-21-2008 8:59 AM
Hi All,
Please advice me how to send an email with excel format.
Regards,
Sri
01-21-2008 9:04 AM
Hi,
plz refer to the below code..
*********************************
FORM send_mail .
DATA: l_t_objbin LIKE solisti1 OCCURS 100 WITH HEADER LINE.
DATA: i_body TYPE soli_tab WITH HEADER LINE. "#EC NEEDED
DATA: l_t_objtxt LIKE solisti1 OCCURS 100 WITH HEADER LINE.
DATA: l_t_objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.
DATA: l_t_objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE.
DATA: l_t_reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE.
DATA: w_tabln TYPE i . "for number of table lines
DATA: w_line TYPE so_text255. " Line of text
DATA: l_doc_chng LIKE sodocchgi1.
DATA: l_tab_lines LIKE sy-tabix.
DATA: l_att_lines TYPE i.
DATA: lv_lines TYPE i.
DATA : l_datvr_tmp(10) TYPE n, "sy-datum,
l_erdat_tmp(10) TYPE n.
DATA : l_eruhr_tmp(8) TYPE n, "sy-uzeit,
l_uhrvr_tmp(8) TYPE n.
*Used for delimiting the columns in Excel Sheet.
CLASS cl_abap_char_utilities DEFINITION LOAD.
CONSTANTS:
cr_mark(2) TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
CONSTANTS c_l_newline TYPE abap_char1 VALUE %_newline. "#EC NOTEXT
CLEAR : l_t_objpack,
l_t_objhead,
l_t_objbin,
l_t_objtxt,
l_t_reclist.
REFRESH: l_t_objpack,
l_t_objhead,
l_t_objbin,
l_t_objtxt,
l_t_reclist.
Creating the content of the e-mail message text
IF sy-sysid(1) = 'P'.
l_doc_chng-obj_descr = 'Message Output Status'(025).
l_doc_chng-doc_size = ( l_tab_lines - 1 ) * 255 + STRLEN( l_t_objtxt ).
ELSE.
l_doc_chng-obj_descr = 'Message Output Status TEST TEST TEST'(026).
l_doc_chng-doc_size = ( l_tab_lines - 1 ) * 255 + STRLEN( l_t_objtxt ).
ENDIF.
l_t_objtxt = 'Hi,'(027).
APPEND l_t_objtxt.
CLEAR l_t_objtxt.
APPEND l_t_objtxt.
CONCATENATE 'Please open the attachment to review details of the Message output that errored'(028)
'for Application'(043) p_kappl INTO l_t_objtxt
SEPARATED BY space.
APPEND l_t_objtxt.
CLEAR l_t_objtxt.
APPEND l_t_objtxt.
DESCRIBE TABLE i_output LINES sy-tfill .
IF sy-tfill GT 0 .
CLEAR l_t_objtxt.
APPEND l_t_objtxt.
APPEND l_t_objtxt.
APPEND l_t_objtxt.
CONCATENATE 'Note! - This is an automatically generated email by the'(029)
'SAP system. Pls do not reply to the sender.'(044)
INTO l_t_objtxt
SEPARATED BY space.
APPEND l_t_objtxt.
DESCRIBE TABLE l_t_objtxt LINES l_tab_lines.
READ TABLE l_t_objtxt INDEX l_tab_lines.
l_doc_chng-doc_size = ( l_tab_lines - 1 ) * 255 + STRLEN( l_t_objtxt )
.
Creating the entry for the email text in the packing list
CLEAR l_t_objpack-transf_bin.
l_t_objpack-head_start = 1.
l_t_objpack-head_num = 1.
l_t_objpack-body_start = 1.
l_t_objpack-body_num = l_tab_lines.
l_t_objpack-doc_type = 'RAW'.
lv_lines = l_tab_lines.
APPEND l_t_objpack.
Columns Heading for Excel Attachment.
CONCATENATE 'Ap'(030)
'Document'(012)
'Msg.'(031)
'Partner'(008)
'Role'(009)
'Created On'(010)
'Time'(037)
'Med'(032)
'Proc. date'(033)
'Prc. time'(038)
'User Name'(015)
'Status'(011)
'Number'(016)
'Error Mgmnt'(017)
'No of Sys Msgs'(018)
'AppAr'(034)
'M'(035)
'MsgNo'(036)
'MsgVarb'(021)
'MsgVarb'(021)
'MsgVarb'(021)
'MsgVarb'(021)
'Plant'(001)
'Pur Grp'(052)
'User'(053)
'Pur User'(054)
'MRP Cont.'(055)
'User'(053)
'MRP User'(056)
'Text'(039)
INTO i_body
SEPARATED BY cr_mark.
CONCATENATE ' ' i_body INTO i_body SEPARATED BY c_l_newline.
APPEND i_body TO l_t_objtxt.
CLEAR i_body.
Data to be shown in Excel Sheet.
LOOP AT i_output INTO wa_output.
WRITE wa_output-erdat TO l_erdat_tmp USING EDIT MASK '__/__/____'.
WRITE wa_output-datvr TO l_datvr_tmp USING EDIT MASK '__/__/____'.
WRITE wa_output-eruhr TO l_eruhr_tmp USING EDIT MASK '__:__:__'.
WRITE wa_output-uhrvr TO l_uhrvr_tmp USING EDIT MASK '__:__:__'.
CONCATENATE wa_output-kappl
wa_output-objky
wa_output-kschl
wa_output-parnr
wa_output-parvw
wa_output-erdat
l_erdat_tmp
wa_output-eruhr
l_eruhr_tmp
wa_output-nacha
wa_output-datvr
l_datvr_tmp
wa_output-uhrvr
l_uhrvr_tmp
wa_output-usnam
wa_output-vstat
wa_output-cmfpnr
wa_output-aplid
wa_output-msgcnt
wa_output-arbgb
wa_output-msgty
wa_output-msgnr
wa_output-msgv1
wa_output-msgv2
wa_output-msgv3
wa_output-msgv4
wa_output-werks
wa_output-ekgrp
wa_output-bname1
wa_output-pur_name
wa_output-dispo
wa_output-bname2
wa_output-mrp_name
wa_output-err_msg
INTO i_body
SEPARATED BY cr_mark.
CONCATENATE ' ' i_body INTO i_body SEPARATED BY c_l_newline.
APPEND i_body TO l_t_objtxt.
CLEAR : wa_output.
ENDLOOP.
DESCRIBE TABLE l_t_objtxt LINES l_tab_lines.
DESCRIBE TABLE l_t_objtxt LINES l_att_lines.
l_tab_lines = l_tab_lines - lv_lines.
Creating the entry for attachment in the packing list
objpack-transf_bin = 'X'.
lv_lines = lv_lines + 1.
l_t_objpack-head_start = 2.
l_t_objpack-head_num = 1.
l_t_objpack-body_start = lv_lines.
l_t_objpack-body_num = l_tab_lines.
l_t_objpack-doc_type = 'XLS'.
l_t_objpack-obj_name = 'Msg Status'(040).
l_t_objpack-obj_descr = 'Message Output Status'(025).
l_t_objpack-doc_size = l_tab_lines * 255.
APPEND l_t_objpack.
lv_lines = l_att_lines.
**RECEIVERS...
IF p_incld = c_x. "Include email to Purchasing Group on Document
LOOP AT i_ekko INTO wa_ekko.
CLEAR l_t_reclist.
* get the Purchasing group user name from table
READ TABLE i_purname INTO wa_purname WITH KEY ekgrp = wa_ekko-ekgrp
werks = wa_ekko-werks
BINARY SEARCH .
IF sy-subrc EQ 0 .
MOVE: 'X' TO l_t_reclist-express ,
'U' TO l_t_reclist-rec_type ,
wa_purname-smtp_addr TO l_t_reclist-receiver .
APPEND l_t_reclist.
ENDIF.
ENDLOOP.
Sending the document
IF NOT l_t_reclist[] IS INITIAL.
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = l_doc_chng
commit_work = 'X'
TABLES
packing_list = l_t_objpack
object_header = l_t_objhead
contents_bin = l_t_objbin
contents_txt = l_t_objtxt
receivers = l_t_reclist
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
operation_no_authorization = 4
OTHERS = 99.
IF sy-subrc <> 0.
* Do nothing
ELSE.
COMMIT WORK AND WAIT.
MESSAGE s899(8a) WITH 'Email Message sent successfully'(041)."#EC
ENDIF.
ENDIF. "IF NOT l_t_reclist[] IS INITIAL.
ENDIF. "IF p_incld = c_x.
ENDIF. "IF sy-tfill GT 0 .
************************************
hope this helps.
regards,
vikas.
plz reward if helpful..
01-21-2008 9:17 AM
hi
good
check this code
report ZMAILTEST.
parameters: p_email type somlreci1-receiver
default 'you@yourcompany.com'.
data: begin of it001 occurs 0,
bukrs type t001-bukrs,
butxt type t001-butxt,
end of it001.
data: imessage type standard table of solisti1 with header line,
iattach type standard table of solisti1 with header line,
ipacking_list like sopcklsti1 occurs 0 with header line,
ireceivers like somlreci1 occurs 0 with header line,
iattachment like solisti1 occurs 0 with header line.
start-of-selection.
select bukrs butxt into table it001 from t001.
Populate table with detaisl to be entered into .xls file
perform build_xls_data .
Populate message body text
clear imessage. refresh imessage.
imessage = 'Please find attached excel file'.
append imessage.
Send file by email as .xls speadsheet
perform send_email_with_xls tables imessage
iattach
using p_email
'Example Excel Attachment'
'XLS'
'TestFileName'
'CompanyCodes'.
************************************************************************
Form BUILD_XLS_DATA
************************************************************************
form build_xls_data .
constants: con_cret type x value '0D', "OK for non Unicode
con_tab type x value '09'. "OK for non Unicode
*If you have Unicode check active in program attributes thnen you will
*need to declare constants as follows
*class cl_abap_char_utilities definition load.
*constants:
con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB,
con_cret type c value cl_abap_char_utilities=>CR_LF.
concatenate 'BUKRS' 'BUTXT'
into iattach separated by con_tab.
concatenate con_cret iattach into iattach.
append iattach.
loop at it001.
concatenate it001-bukrs it001-butxt
into iattach separated by con_tab.
concatenate con_cret iattach into iattach.
append iattach.
endloop.
endform.
************************************************************************
Form SEND_EMAIL_WITH_XLS
************************************************************************
form send_email_with_xls tables pit_message
pit_attach
using p_email
p_mtitle
p_format
p_filename
p_attdescription.
data: xdocdata like sodocchgi1,
xcnt type i.
Fill the document data.
xdocdata-doc_size = 1.
Populate the subject/generic message attributes
xdocdata-obj_langu = sy-langu.
xdocdata-obj_name = 'SAPRPT'.
xdocdata-obj_descr = p_mtitle .
Fill the document data and get size of attachment
clear xdocdata.
read table iattach index xcnt.
xdocdata-doc_size =
( xcnt - 1 ) * 255 + strlen( iattach ).
xdocdata-obj_langu = sy-langu.
xdocdata-obj_name = 'SAPRPT'.
xdocdata-obj_descr = p_mtitle.
clear iattachment. refresh iattachment.
iattachment] = pit_attach[.
Describe the body of the message
clear ipacking_list. refresh ipacking_list.
ipacking_list-transf_bin = space.
ipacking_list-head_start = 1.
ipacking_list-head_num = 0.
ipacking_list-body_start = 1.
describe table imessage lines ipacking_list-body_num.
ipacking_list-doc_type = 'RAW'.
append ipacking_list.
Create attachment notification
ipacking_list-transf_bin = 'X'.
ipacking_list-head_start = 1.
ipacking_list-head_num = 1.
ipacking_list-body_start = 1.
describe table iattachment lines ipacking_list-body_num.
ipacking_list-doc_type = p_format.
ipacking_list-obj_descr = p_attdescription.
ipacking_list-obj_name = p_filename.
ipacking_list-doc_size = ipacking_list-body_num * 255.
append ipacking_list.
Add the recipients email address
clear ireceivers. refresh ireceivers.
ireceivers-receiver = p_email.
ireceivers-rec_type = 'U'.
ireceivers-com_type = 'INT'.
ireceivers-notif_del = 'X'.
ireceivers-notif_ndel = 'X'.
append ireceivers.
call function 'SO_DOCUMENT_SEND_API1'
exporting
document_data = xdocdata
put_in_outbox = 'X'
commit_work = 'X'
tables
packing_list = ipacking_list
contents_bin = iattachment
contents_txt = imessage
receivers = ireceivers
exceptions
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
others = 8.
thanks
mrutyun^
01-21-2008 10:12 AM
Hi,
Please refer to the link below :
http://www.sapdev.co.uk/reporting/email/attachhome.htm
Thanks,
Sriram Ponna.
01-21-2008 10:18 AM
Hi
The code below demonstrates how to send an email to an external email address(test@sapdev.co.uk),
where the data is stored within a .xls attachment
&----
*& Report ZEMAIL_ATTACH *
*& *
&----
*& Example of sending external email via SAPCONNECT *
*& *
&----
REPORT ZEMAIL_ATTACH .
TABLES: ekko.
PARAMETERS: p_email TYPE somlreci1-receiver
DEFAULT 'test@sapdev.co.uk'.
TYPES: BEGIN OF t_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,
END OF t_ekpo.
DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0,
wa_ekpo TYPE t_ekpo.
TYPES: BEGIN OF t_charekpo,
ebeln(10) TYPE c,
ebelp(5) TYPE c,
aedat(8) TYPE c,
matnr(18) TYPE c,
END OF t_charekpo.
DATA: wa_charekpo TYPE t_charekpo.
DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
WITH HEADER LINE.
DATA: it_attach TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
WITH HEADER LINE.
DATA: t_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
t_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,
t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
t_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,
t_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE,
w_cnt TYPE i,
w_sent_all(1) TYPE c,
w_doc_data LIKE sodocchgi1,
gd_error TYPE sy-subrc,
gd_reciever TYPE sy-subrc.
************************************************************************
*START_OF_SELECTION
START-OF-SELECTION.
Retrieve sample data from table ekpo
PERFORM data_retrieval.
Populate table with detaisl to be entered into .xls file
PERFORM build_xls_data_table.
************************************************************************
*END-OF-SELECTION
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 it_message
it_attach
using p_email
'Example .xls documnet attachment'
'XLS'
'filename'
' '
' '
' '
changing gd_error
gd_reciever.
Instructs mail send program for SAPCONNECT to send email(rsconn01)
PERFORM initiate_mail_execute_program.
&----
*& Form DATA_RETRIEVAL
&----
Retrieve data form EKPO table and populate itab it_ekko
----
FORM data_retrieval.
SELECT ebeln ebelp aedat matnr
UP TO 10 ROWS
FROM ekpo
INTO TABLE it_ekpo.
ENDFORM. " DATA_RETRIEVAL
&----
*& Form BUILD_XLS_DATA_TABLE
&----
Build data table for .xls document
----
FORM build_xls_data_table.
CONSTANTS: con_cret TYPE x VALUE '0D', "OK for non Unicode
con_tab TYPE x VALUE '09'. "OK for non Unicode
*If you have Unicode check active in program attributes thnen you will
*need to declare constants as follows
*class cl_abap_char_utilities definition load.
*constants:
con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB,
con_cret type c value cl_abap_char_utilities=>CR_LF.
CONCATENATE 'EBELN' 'EBELP' 'AEDAT' 'MATNR'
INTO it_attach SEPARATED BY con_tab.
CONCATENATE con_cret it_attach INTO it_attach.
APPEND it_attach.
LOOP AT it_ekpo INTO wa_charekpo.
CONCATENATE wa_charekpo-ebeln wa_charekpo-ebelp
wa_charekpo-aedat wa_charekpo-matnr
INTO it_attach SEPARATED BY con_tab.
CONCATENATE con_cret it_attach INTO it_attach.
APPEND it_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.
ld_attfilename = p_filename.
ld_sender_address = p_sender_address.
ld_sender_address_type = p_sender_addres_type.
Fill the document data.
w_doc_data-doc_size = 1.
Populate the subject/generic message attributes
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle .
w_doc_data-sensitivty = 'F'.
Fill the document data and get size of attachment
CLEAR w_doc_data.
READ TABLE it_attach INDEX w_cnt.
w_doc_data-doc_size =
( w_cnt - 1 ) * 255 + STRLEN( it_attach ).
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle.
w_doc_data-sensitivty = 'F'.
CLEAR t_attachment.
REFRESH t_attachment.
t_attachment[] = pit_attach[].
Describe the body of the message
CLEAR t_packing_list.
REFRESH t_packing_list.
t_packing_list-transf_bin = space.
t_packing_list-head_start = 1.
t_packing_list-head_num = 0.
t_packing_list-body_start = 1.
DESCRIBE TABLE it_message LINES t_packing_list-body_num.
t_packing_list-doc_type = 'RAW'.
APPEND t_packing_list.
Create attachment notification
t_packing_list-transf_bin = 'X'.
t_packing_list-head_start = 1.
t_packing_list-head_num = 1.
t_packing_list-body_start = 1.
DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.
t_packing_list-doc_type = ld_format.
t_packing_list-obj_descr = ld_attdescription.
t_packing_list-obj_name = ld_attfilename.
t_packing_list-doc_size = t_packing_list-body_num * 255.
APPEND t_packing_list.
Add the recipients email address
CLEAR t_receivers.
REFRESH t_receivers.
t_receivers-receiver = ld_email.
t_receivers-rec_type = 'U'.
t_receivers-com_type = 'INT'.
t_receivers-notif_del = 'X'.
t_receivers-notif_ndel = 'X'.
APPEND t_receivers.
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = w_doc_data
put_in_outbox = 'X'
sender_address = ld_sender_address
sender_address_type = ld_sender_address_type
commit_work = 'X'
IMPORTING
sent_to_all = w_sent_all
TABLES
packing_list = t_packing_list
contents_bin = t_attachment
contents_txt = it_message
receivers = t_receivers
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8.
Populate zerror return code
ld_error = sy-subrc.
Populate zreceiver return code
LOOP AT t_receivers.
ld_receiver = t_receivers-retrn_code.
ENDLOOP.
ENDFORM.
&----
*& Form INITIATE_MAIL_EXECUTE_PROGRAM
&----
Instructs mail send program for SAPCONNECT to send email.
----
FORM initiate_mail_execute_program.
WAIT UP TO 2 SECONDS.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.
ENDFORM. " INITIATE_MAIL_EXECUTE_PROGRAM
&----
*& Form POPULATE_EMAIL_MESSAGE_BODY
&----
Populate message body text
----
form populate_email_message_body.
REFRESH it_message.
it_message = 'Please find attached a list test ekpo records'.
APPEND it_message.
endform. " POPULATE_EMAIL_MESSAGE_BODY