Sending emails from SAP
Thursday, September 28, 2006
Today I was trying to set up email notification in one of my programs. There was a requirement to send out an email to a designated user if any errors occurred in the program. Fortunately, I already knew which function module to use (SO_NEW_DOCUMENT_SEND_API1), so I thought it would be a piece of cake.
In the documentation for that function module there is even a piece of code that illustrates how this FM should be used. So I just copy-pasted that code in my temporary program, added my email address and ran it. It did successfully send a message to my SAP Inbox but the email was not sent. After some checking around it turned out that we simply can not send emails from the test client (apparently the Basis guys were too lazy to set it up there). OK, no big deal – I ran my program in the right environment. And – hurrah! – got a message ‘...sent successfully’.
After 2 cups of coffee I suddenly realized that I should have gotten an email by now. But there was none... Hmm... More googling and more asking around brought the following results: if there was an email waiting to be sent I would have seen it in the transaction SCOT. Also the emails are actually sent out from SAP by the program RSCONN01, which is usually scheduled as a background job to run every N minutes. My program ran successfully and the job was running OK but SCOT was showing only bunch of zeroes. What the heck is going on?! So I went to check my program again, maybe I was missing something. Indeed I was! Turns out that the bast... , I mean nice person who wrote a code example in the FM documentation forgot about a little tiny detail – it needs COMMIT WORK, otherwise nothing will happen! It will give you all the “success” messages and sy-subrc = 0 but good luck waiting for that email to arrive.
In the process (I spent like an hour on this!) I’ve also managed to strip down the code to the bare minimum: email address, subject line and email body. Here is what it boiled down to:
DATA: objcont LIKE solisti1 OCCURS 0 WITH HEADER LINE.
DATA: reclist LIKE somlreci1 OCCURS 0 WITH HEADER LINE.
DATA: doc_chng LIKE sodocchgi1.
* Email subject line
doc_chng-obj_descr = 'Email subject'.
* Email body
objcont = 'This is a line to be sent in the email body'.
APPEND objcont.
* Receiver list
reclist-receiver = 'your email address here'.
reclist-rec_type = 'U'.
APPEND reclist.
* Send the document
CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
document_data = doc_chng
commit_work = 'X'
TABLES
object_content = objcont
receivers = reclist
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
operation_no_authorization = 4
OTHERS = 99.
IF sy-subrc <> 0.
MESSAGE 'Email could not be sent' TYPE 'I'.
ELSE.
MESSAGE 'Email was sent successfully' TYPE 'I'.
ENDIF.
A few pointers:
- Email address is not case-sensitive.
- Since I have only one recipient, I check only for sy-subrc after the function module. If you have a list of recipients, use the code from the FM documentation, which checks the status of each recipient.
- Objcont here is an internal table. In my program I populate a table i_errors with the error messages and then copy the content to objcont.
You can also send attachments using this function module. As far as I understand, the attachment should be written into a file and the file name should be submitted to the FM.
I’m pretty sure you could get an email address from a user profile (user profile display is in transaction SU3) but we don’t store emails there, so we chose to use our own Z table instead.
posted by Your Friendly ABAPer @ 21:13, Direct link to this post
12 Comments:
- At 10/7/08 02:48, said...
-
Much appreciate this. I spent nearly 2 hours trying to find out why it is not sending emails. I am not a big abaper, so your tip was useful. Thanks!
- At 20/11/08 05:58, said...
-
Thanks for sharing your experience. The piece of code was really helpful to me.
Rashmi - At 6/1/09 12:27, said...
-
Well that helped.
RSCONN01 is key of course...
It is hilarious because I went through the exact same thing you did in your story.
finally learned that we have a pgm that does send emails and looking at it I noticed submit RSCONN01...
quick google to see who else went thru this mini-hell and voila, your blog
How many hits you get per day? - At 13/1/11 08:30, me said...
-
what is the rectype for cc or bcc
- At 14/1/11 15:21, said...
-
It's the same. Have you noticed a field COPY in the same structure?
- At 1/3/11 11:24, said...
-
Great, saved me a lot of trouble.
Just a question... why not add a Commit work in your example? :-)
/Tomas - At 28/3/11 23:26, said...
-
In the example above I'm passing 'X' in the COMMIT parameter. This is the same as doing an explicit COMMIT WORK afterwards.
- At 31/1/12 07:17, Serverus said...
-
Hello. I tryied to send mails from Solman using this function, but when I did, it seems the program rsconn01 is not being scheduled, so i manually executed it and when I did, 4 thousand mails were sent ( garbage mais ).
How do I see the mails that are to be sent when I execute it? I would like to ensure that there is no program running automatically generating garbage mail anymore.
Thanks for your comments. - At 31/1/12 22:08, said...
-
You can see the outgoing communications in SOST transaction. In SCOT you can also see how many messages are in queue for processing.
- At 17/5/12 15:59, said...
-
I would like to send e-mail to
mail@gmail.com ( out of SAP )
is that possible ??? - At 1/5/14 08:13, said...
-
Hello , I tried your example but this is throwing an exception, that
is " Express document "Update was terminated" received from author "XXXXXX" " and thus the commit work is directing an exception!!! please help. - At 22/4/16 05:19, said...
-
It was really helpfull .