‎2007 Sep 17 7:58 AM
Hi,
I am working on a report prog where I need to send the outout through email when program is executed in background. is it technically possible? Is yes then How?
‎2007 Sep 17 8:06 AM
Hi,
Use the below code
****************************************
Check the mail in T-code SBWP
To check the send mail status T-Code SOST
***********************************************
TABLES:pa0001.
DATA: v_ans,
v_body_lines TYPE i.
Local declarations
CONSTANTS:
c_note_00 TYPE solisti1 VALUE 'NOTE',
c_note_01 TYPE solisti1 VALUE
'Do not reply to this email, instead please contact the compensation ',
c_note_02 TYPE solisti1 VALUE
'specialist (see above for contact details). This will help avoid ',
c_note_03 TYPE solisti1 VALUE
'unnecessarily delaying your request.'.
DATA: it_packing_list TYPE TABLE OF sopcklsti1,
wa_packing_list LIKE LINE OF it_packing_list,
it_receivers TYPE TABLE OF somlreci1,
wa_receivers LIKE LINE OF it_receivers,
it_mailbody TYPE TABLE OF solisti1,
wa_mailbody LIKE LINE OF it_mailbody.
DATA: it_doc TYPE sodocchgi1.
DATA: v_sent_all TYPE boolean.
START-OF-SELECTION.
mail header
it_doc-obj_descr = 'Hello'.
Add the recipients email address
CLEAR wa_receivers.
REFRESH it_receivers.
wa_receivers-receiver = 'XXXXX@XXXX.COM'.
wa_receivers-rec_type = 'U'.
wa_receivers-com_type = 'INT'.
wa_receivers-notif_del = 'X'.
wa_receivers-notif_ndel = 'X'.
APPEND wa_receivers TO it_receivers.
wa_receivers-receiver = 'XXXXX@XXXX.COM'.
wa_receivers-rec_type = 'U'.
wa_receivers-com_type = 'INT'.
wa_receivers-notif_del = 'X'.
wa_receivers-notif_ndel = 'X'.
APPEND wa_receivers TO it_receivers.
Mail Body
CLEAR wa_mailbody.
REFRESH it_mailbody.
wa_mailbody-line = 'Hi All,'.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = ' '.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = 'This is a test mail'.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = ' '.
APPEND wa_mailbody TO it_mailbody.
DO 3 TIMES.
wa_mailbody-line = ' '.
APPEND wa_mailbody TO it_mailbody.
ENDDO.
wa_mailbody-line = c_note_00.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = c_note_01.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = c_note_02.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = c_note_03.
APPEND wa_mailbody TO it_mailbody.
Describe the body of the message
CLEAR wa_packing_list.
REFRESH it_packing_list.
wa_packing_list-transf_bin = space.
wa_packing_list-head_start = 1.
wa_packing_list-head_num = 0.
wa_packing_list-body_start = 1.
DESCRIBE TABLE it_mailbody LINES v_body_lines.
wa_packing_list-body_num = v_body_lines.
wa_packing_list-doc_type = 'RAW'.
APPEND wa_packing_list TO it_packing_list.
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = it_doc
put_in_outbox = 'X'
commit_work = 'X'
IMPORTING
sent_to_all = v_sent_all
TABLES
packing_list = it_packing_list
contents_txt = it_mailbody
receivers = it_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.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
WRITE:/ 'Mail has been sent to the receivers sucessfully'.
ENDIF.
‎2007 Sep 17 8:03 AM
Hi Nilesh,
Yes it is definitely possible to send the email through the Report generated in SE38.
You will have to use the function module SO_NEW_DOCUMENT_SEND_API1 to do that.
Also, keep in mind to read the documentation first as one needs to define some of the import and export parameters to be used here is a special format as mentioned in the documentation.
In case you have any further clarifications,do let me know.
Regards,
Puneet Jhari.
‎2007 Sep 17 8:06 AM
Hi,
Use the below code
****************************************
Check the mail in T-code SBWP
To check the send mail status T-Code SOST
***********************************************
TABLES:pa0001.
DATA: v_ans,
v_body_lines TYPE i.
Local declarations
CONSTANTS:
c_note_00 TYPE solisti1 VALUE 'NOTE',
c_note_01 TYPE solisti1 VALUE
'Do not reply to this email, instead please contact the compensation ',
c_note_02 TYPE solisti1 VALUE
'specialist (see above for contact details). This will help avoid ',
c_note_03 TYPE solisti1 VALUE
'unnecessarily delaying your request.'.
DATA: it_packing_list TYPE TABLE OF sopcklsti1,
wa_packing_list LIKE LINE OF it_packing_list,
it_receivers TYPE TABLE OF somlreci1,
wa_receivers LIKE LINE OF it_receivers,
it_mailbody TYPE TABLE OF solisti1,
wa_mailbody LIKE LINE OF it_mailbody.
DATA: it_doc TYPE sodocchgi1.
DATA: v_sent_all TYPE boolean.
START-OF-SELECTION.
mail header
it_doc-obj_descr = 'Hello'.
Add the recipients email address
CLEAR wa_receivers.
REFRESH it_receivers.
wa_receivers-receiver = 'XXXXX@XXXX.COM'.
wa_receivers-rec_type = 'U'.
wa_receivers-com_type = 'INT'.
wa_receivers-notif_del = 'X'.
wa_receivers-notif_ndel = 'X'.
APPEND wa_receivers TO it_receivers.
wa_receivers-receiver = 'XXXXX@XXXX.COM'.
wa_receivers-rec_type = 'U'.
wa_receivers-com_type = 'INT'.
wa_receivers-notif_del = 'X'.
wa_receivers-notif_ndel = 'X'.
APPEND wa_receivers TO it_receivers.
Mail Body
CLEAR wa_mailbody.
REFRESH it_mailbody.
wa_mailbody-line = 'Hi All,'.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = ' '.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = 'This is a test mail'.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = ' '.
APPEND wa_mailbody TO it_mailbody.
DO 3 TIMES.
wa_mailbody-line = ' '.
APPEND wa_mailbody TO it_mailbody.
ENDDO.
wa_mailbody-line = c_note_00.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = c_note_01.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = c_note_02.
APPEND wa_mailbody TO it_mailbody.
wa_mailbody-line = c_note_03.
APPEND wa_mailbody TO it_mailbody.
Describe the body of the message
CLEAR wa_packing_list.
REFRESH it_packing_list.
wa_packing_list-transf_bin = space.
wa_packing_list-head_start = 1.
wa_packing_list-head_num = 0.
wa_packing_list-body_start = 1.
DESCRIBE TABLE it_mailbody LINES v_body_lines.
wa_packing_list-body_num = v_body_lines.
wa_packing_list-doc_type = 'RAW'.
APPEND wa_packing_list TO it_packing_list.
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = it_doc
put_in_outbox = 'X'
commit_work = 'X'
IMPORTING
sent_to_all = v_sent_all
TABLES
packing_list = it_packing_list
contents_txt = it_mailbody
receivers = it_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.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
WRITE:/ 'Mail has been sent to the receivers sucessfully'.
ENDIF.
‎2007 Sep 17 8:12 AM
Hi,
Definitely , you can use:
if sy-batch = 'X'. " program running in batch
perform send_mail.
endif.
Here is a sample program for email:
data: objpack like sopcklsti1 occurs 2 with header line.
data: objhead like solisti1 occurs 1 with header line.
data: objbin like solisti1 occurs 10 with header line.
data: objtxt like solisti1 occurs 10 with header line.
data: reclist like somlreci1 occurs 5 with header line.
data: doc_chng like sodocchgi1.
data: tab_lines like sy-tabix.
data: sent_to_all like sonv-flag.
data : begin of itab occurs 0,
pernr like p0001-pernr,
name like p0002-vorna,
end of itab.
Creation of the document to be sent
File Name
doc_chng-obj_name = 'SENDFILE'.
Mail Subject
concatenate 'Type your title here'
space
into doc_chng-obj_descr.
Mail Contents
objtxt = 'Dear text'.
append objtxt.
objtxt = 'Type your text here'.
append objtxt.
append objtxt.
describe table objtxt lines tab_lines.
*
Creation of the entry for the document
clear objpack-transf_bin. "Will get content from content_text
objpack-head_start = 1. "Reads given row number in object header
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = 'RAW'.
append objpack.
Prepare content for attachment
*DO 3 TIMES.
*itab-pernr = 182.
*itab-name = 'test '.
*APPEND itab.
*itab-pernr = 1000.
*itab-name = 'test'.
*APPEND itab.
*ENDDO.
describe table itab lines tab_lines.
*
Populate attachment content
*LOOP AT itab.
if sy-tabix = 1.
concatenate 'Field1'
'Field2'
'Field3'
CL_ABAP_CHAR_UTILITIES=>NEWLINE
INTO objbin-line
SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
APPEND objbin.
ENDIF.
CONCATENATE itab-pernr
itab-name
'3rd row'
INTO objbin-line.
*CONDENSE objbin-line.
*APPEND objbin.
*
*ENDLOOP.
describe table objbin lines tab_lines.
Creation of attachment in xls form,
Creation of the entry for the compressed attachment
*objpack-transf_bin = 'X'. "Will get content from content_bin
*objpack-head_start = 1.
*objpack-head_num = 1.
*objpack-body_start = 1.
*objpack-body_num = tab_lines.
**objpack-doc_type = 'EXT'.
**objpack-obj_name = 'WEBSITE'.
**objpack-obj_descr = 'QuickLogirest.XLS'. "
*objpack-doc_size = tab_lines * 255.
*APPEND objpack.
target recipent
clear reclist.
reclist-receiver = 'receiver@domain.com'.
*reclist-express = 'X'.
reclist-rec_type = 'U'.
append reclist.
copy recipents
*CLEAR reclist.
***reclist-receiver = 'receiver@domain.com'.
**reclist-express = 'X'.
*reclist-rec_type = 'U'.
*reclist-copy = 'X'.
*APPEND reclist.
Sending the document
*CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
*EXPORTING
*document_data = doc_chng
*put_in_outbox = 'X'
*commit_work = 'X'
*TABLES
*packing_list = objpack
*object_header = objhead
*contents_bin = objbin
*contents_txt = objtxt
*receivers = reclist
*EXCEPTIONS
*too_many_receivers = 1
*document_not_sent = 2
*operation_no_authorization = 4
*OTHERS = 99.
.
if sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
if sy-subrc = 0.
write:'Apparently successful'.
else.
write:'Error while sending mail!'.
endif.
Regards,
Reward if helpful.
‎2007 Sep 17 8:19 AM
Hi,
PARAMETERS: psubject(40) type c default 'Hello',
p_email(40) type c default 'test@sapdev.co.uk' .
data: it_packing_list like sopcklsti1 occurs 0 with header line,
it_contents like solisti1 occurs 0 with header line,
it_receivers like somlreci1 occurs 0 with header line,
it_attachment like solisti1 occurs 0 with header line,
gd_cnt type i,
gd_sent_all(1) type c,
gd_doc_data like sodocchgi1,
gd_error type sy-subrc.
data: it_message type standard table of SOLISTI1 initial size 0
with header line.
***********************************************************************
*START-OF-SELECTION.
START-OF-SELECTION.
Perform populate_message_table.
*Send email message, although is not sent from SAP until mail send
*program has been executed(rsconn01)
PERFORM send_email_message.
*Instructs mail send program for SAPCONNECT to send email(rsconn01)
perform initiate_mail_execute_program.
&----
*& Form POPULATE_MESSAGE_TABLE
&----
Adds text to email text table
----
form populate_message_table.
Append 'Email line 1' to it_message.
Append 'Email line 2' to it_message.
Append 'Email line 3' to it_message.
Append 'Email line 4' to it_message.
endform. " POPULATE_MESSAGE_TABLE
&----
*& Form SEND_EMAIL_MESSAGE
&----
Send email message
----
form send_email_message.
Fill the document data.
gd_doc_data-doc_size = 1.
Populate the subject/generic message attributes
gd_doc_data-obj_langu = sy-langu.
gd_doc_data-obj_name = 'SAPRPT'.
gd_doc_data-obj_descr = psubject.
gd_doc_data-sensitivty = 'F'.
Describe the body of the message
clear it_packing_list.
refresh it_packing_list.
it_packing_list-transf_bin = space.
it_packing_list-head_start = 1.
it_packing_list-head_num = 0.
it_packing_list-body_start = 1.
describe table it_message lines it_packing_list-body_num.
it_packing_list-doc_type = 'RAW'.
append it_packing_list.
Add the recipients email address
clear it_receivers.
refresh it_receivers.
it_receivers-receiver = p_email.
it_receivers-rec_type = 'U'.
it_receivers-com_type = 'INT'.
it_receivers-notif_del = 'X'.
it_receivers-notif_ndel = 'X'.
append it_receivers.
Call the FM to post the message to SAPMAIL
call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
exporting
document_data = gd_doc_data
put_in_outbox = 'X'
importing
sent_to_all = gd_sent_all
tables
packing_list = it_packing_list
contents_txt = it_message
receivers = it_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.
Store function module return code
gd_error = sy-subrc.
Get it_receivers return code
loop at it_receivers.
endloop.
endform. " SEND_EMAIL_MESSAGE
&----
*& Form INITIATE_MAIL_EXECUTE_PROGRAM
&----
Instructs mail send program for SAPCONNECT to send email.
----
form initiate_mail_execute_program.
wait up to 2 seconds.
if gd_error eq 0.
submit rsconn01 with mode = 'INT'
with output = 'X'
and return.
endif.
endform. " INITIATE_MAIL_EXECUTE_PROGRAM