<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d34883048\x26blogName\x3dYour+Friendly+ABAPer\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://friendlyabaper.blogspot.com/search\x26blogLocale\x3den_US\x26v\x3d2\x26homepageUrl\x3dhttp://friendlyabaper.blogspot.com/\x26vt\x3d-8534208955155839123', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

Sending emails from SAP

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, Anonymous Anonymous 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, Anonymous Anonymous said...

Thanks for sharing your experience. The piece of code was really helpful to me.
Rashmi

 
At 6/1/09 12:27, Anonymous Anonymous 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, Blogger me said...

what is the rectype for cc or bcc

 
At 14/1/11 15:21, Anonymous YourFriendlyABAPer said...

It's the same. Have you noticed a field COPY in the same structure?

 
At 1/3/11 11:24, Anonymous Anonymous 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, Anonymous YourFriendlyABAPer 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, Blogger 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, Anonymous YourFriendlyABAPer 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, Anonymous Anonymous said...

I would like to send e-mail to
mail@gmail.com ( out of SAP )
is that possible ???

 
At 1/5/14 08:13, Anonymous Anonymous 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, Anonymous Anonymous said...

It was really helpfull .

 

Post a Comment

<< Home