Hi all,
I thought I would post code to send an email out of R. The code uses
Grothendieck and Bellosta's interface package rJython for executing Python
from R. The code itself provides basic email functionality for email servers
requiring authentication. It should be easy to extend it (e.g., for sending
attachments). I hope it's useful.
require(rJython)
rJython <- rJython()
rJython$exec( "import smtplib" )
rJython$exec("from email.MIMEText import MIMEText")
rJython$exec("import email.utils")
mail<-c(
#Email settings
"fromaddr = 'sender email address'",
"toaddrs = 'recipient email address'",
"msg = MIMEText('This is the body of the message.')",
"msg['From'] = email.utils.formataddr(('sender name', fromaddr))",
"msg['To'] = email.utils.formataddr(('recipient name', toaddrs))",
"msg['Subject'] = 'Simple test message'",
#SMTP server credentials
"username = 'sender login'",
"password = 'sender password'",
#Set SMTP server and send email, e.g., google mail SMTP server
"server = smtplib.SMTP('smtp.gmail.com:587')",
"server.ehlo()",
"server.starttls()",
"server.ehlo()",
"server.login(username,password)",
"server.sendmail(fromaddr, toaddrs, msg.as_string())",
"server.quit()")
jython.exec(rJython,mail)
Best,
Daniel
--
View this message in context: http://r.789695.n4.nabble.com/Email-out-of-R-code-tp3530671p3530671.html
Sent from the R help mailing list archive at Nabble.com.
Email out of R (code)
7 messages · Daniel Malter, Kevin Wright, Wolfgang Raffelsberger +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110518/a63df8ed/attachment.pl>
In case you're using Unix/Linux, have a look at www.r-project.org/doc/Rnews/Rnews_2007-1.pdf (page 30 - 32) Wolfgang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wolfgang Raffelsberger, PhD IGBMC, 1 rue Laurent Fries, 67404 Illkirch Strasbourg, France Tel (+33) 388 65 3300 Fax (+33) 388 65 3276 wolfgang.raffelsberger (a t) igbmc.fr ________________________________________ De : r-help-bounces at r-project.org [r-help-bounces at r-project.org] de la part de Kevin Wright [kw.stat at gmail.com] Date d'envoi : mercredi 18 mai 2011 17:57 ? : Daniel Malter Cc : r-help at r-project.org Objet : Re: [R] Email out of R (code) How does this compare to create.post() ? Kevin
On Tue, May 17, 2011 at 3:44 PM, Daniel Malter <daniel at umd.edu> wrote:
Hi all,
I thought I would post code to send an email out of R. The code uses
Grothendieck and Bellosta's interface package rJython for executing Python
from R. The code itself provides basic email functionality for email
servers
requiring authentication. It should be easy to extend it (e.g., for sending
attachments). I hope it's useful.
require(rJython)
rJython <- rJython()
rJython$exec( "import smtplib" )
rJython$exec("from email.MIMEText import MIMEText")
rJython$exec("import email.utils")
mail<-c(
#Email settings
"fromaddr = 'sender email address'",
"toaddrs = 'recipient email address'",
"msg = MIMEText('This is the body of the message.')",
"msg['From'] = email.utils.formataddr(('sender name', fromaddr))",
"msg['To'] = email.utils.formataddr(('recipient name', toaddrs))",
"msg['Subject'] = 'Simple test message'",
#SMTP server credentials
"username = 'sender login'",
"password = 'sender password'",
#Set SMTP server and send email, e.g., google mail SMTP server
"server = smtplib.SMTP('smtp.gmail.com:587')",
"server.ehlo()",
"server.starttls()",
"server.ehlo()",
"server.login(username,password)",
"server.sendmail(fromaddr, toaddrs, msg.as_string())",
"server.quit()")
jython.exec(rJython,mail)
Best,
Daniel
--
View this message in context:
http://r.789695.n4.nabble.com/Email-out-of-R-code-tp3530671p3530671.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
I do not know. I was not aware and could hardly find any information on create.post(). From what I have seen at first glance, it seems that create.post() either opens your standard email program or web browser, which the python code does not. Instead it needs the R-library interfacing Python. I also do not know how create.post() handles server authentication (though, my blind guess would be with the settings of your email program or browser mail). To stop guessing, if you want a solid comparison, I am afraid you have to do it yourself. Best, Daniel -- View this message in context: http://r.789695.n4.nabble.com/Email-out-of-R-code-tp3530671p3533280.html Sent from the R help mailing list archive at Nabble.com.
There is also the sendmailR package if people want to compare even more... Uwe Ligges
On 18.05.2011 18:53, Daniel Malter wrote:
I do not know. I was not aware and could hardly find any information on create.post(). From what I have seen at first glance, it seems that create.post() either opens your standard email program or web browser, which the python code does not. Instead it needs the R-library interfacing Python. I also do not know how create.post() handles server authentication (though, my blind guess would be with the settings of your email program or browser mail). To stop guessing, if you want a solid comparison, I am afraid you have to do it yourself. Best, Daniel -- View this message in context: http://r.789695.n4.nabble.com/Email-out-of-R-code-tp3530671p3533280.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
As I (thought I) understood from the sendmailR manual, the package does currently not support server authentication, or does it? Daniel -- View this message in context: http://r.789695.n4.nabble.com/Email-out-of-R-code-tp3530671p3536512.html Sent from the R help mailing list archive at Nabble.com.
I took a look at sendmailR when I was trying to figure out how to send
email a few years ago. I ended up using my SQL Server dbmail facility,
which I can easily access from R using the RODBC package. In case my
scenario applies to anyone else, I will paste the simple function I
wrote to serve as an example to anyone else who may want to do the same
thing:
"email" <- function(to, subject, msg, texttype='TEXT', imp='NORMAL',
profileName='Roger Bos') {
go <- try(sqlQuery(xf, "EXECUTE msdb..sp_send_dbmail
@profile_name = '" %+% profileName %+% "',
@recipients = '" %+% to %+% "',
@body = '" %+% msg %+% "',
@subject = '" %+% subject %+% "',
@query = '',
@body_format = " %+% texttype %+% ",
@importance = '" %+% imp %+% "',
@attach_query_result_as_file = 0"))
}
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Daniel Malter
Sent: Thursday, May 19, 2011 2:04 PM
To: r-help at r-project.org
Subject: Re: [R] Email out of R (code)
As I (thought I) understood from the sendmailR manual, the package does
currently not support server authentication, or does it?
Daniel
--
View this message in context:
http://r.789695.n4.nabble.com/Email-out-of-R-code-tp3530671p3536512.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
***************************************************************
This message is for the named person's use only. It may\...{{dropped:20}}