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.
How does this compare to create.post() ? Kevin On Tue, May 17, 2011 at 3:44 PM, Daniel Malter <daniel@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@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. >[[alternative HTML version deleted]]
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.
Thanks for posting this. If anyone is interested in a short extension to include an attachment, try the function below. I make no guarantees though. Also, note that winDialog() is used in a couple places, so it may need some therapy before working outside of Windows. send.email <- function(to, from, subject, message, attachment=NULL, username, password, server="smtp.gmail.com:587", confirmBeforeSend=TRUE){ # to: a list object of length 1. Using list("Recipient" "recip at somewhere.net") will send the message to the address but # the name will appear instead of the address. # from: a list object of length 1. Same behavior as 'to' # subject: Character(1) giving the subject line. # message: Character(1) giving the body of the message # attachment: Character(1) giving the location of the attachment # username: character(1) giving the username. If missing and you are using Windows, R will prompt you for the username. # password: character(1) giving the password. If missing and you are using Windows, R will prompt you for the password. # server: character(1) giving the smtp server. # confirmBeforeSend: Logical. If True, a dialog box appears seeking confirmation before sending the e-mail. This is to # prevent me to send multiple updates to a collaborator while I am working interactively. if (!is.list(to) | !is.list(from)) stop("'to' and 'from' must be lists") if (length(from) > 1) stop("'from' must have length 1") if (length(to) > 1) stop("'send.email' currently only supports one recipient e-mail address") if (length(attachment) > 1) stop("'send.email' can currently send only one attachment") if (length(message) > 1){ stop("'message' must be of length 1") message <- paste(message, collapse="\\n\\n") } if (is.null(names(to))) names(to) <- to if (is.null(names(from))) names(from) <- from if (!is.null(attachment)) if (!file.exists(attachment)) stop(paste("'", attachment, "' does not exist!", sep="")) if (missing(username)) username <- winDialogString("Please enter your e-mail username", "") if (missing(password)) password <- winDialogString("Please enter your e-mail password", "") require(rJython) rJython <- rJython() rJython$exec("import smtplib") rJython$exec("import os") rJython$exec("from email.MIMEMultipart import MIMEMultipart") rJython$exec("from email.MIMEBase import MIMEBase") rJython$exec("from email.MIMEText import MIMEText") rJython$exec("from email.Utils import COMMASPACE, formatdate") rJython$exec("from email import Encoders") rJython$exec("import email.utils") mail<-c( #Email settings paste("fromaddr = '", from, "'", sep=""), paste("toaddrs = '", to, "'", sep=""), "msg = MIMEMultipart()", paste("msg.attach(MIMEText('", message, "'))", sep=""), paste("msg['From'] = email.utils.formataddr(('", names(from), "', fromaddr))", sep=""), paste("msg['To'] = email.utils.formataddr(('", names(to), "', toaddrs))", sep=""), paste("msg['Subject'] = '", subject, "'", sep="")) if (!is.null(attachment)){ mail <- c(mail, paste("f = '", attachment, "'", sep=""), "part=MIMEBase('application', 'octet-stream')", "part.set_payload(open(f, 'rb').read())", "Encoders.encode_base64(part)", "part.add_header('Content-Disposition', 'attachment; filename=\"%s\"' % os.path.basename(f))", "msg.attach(part)") } #SMTP server credentials mail <- c(mail, paste("username = '", username, "'", sep=""), paste("password = '", password, "'", sep=""), #Set SMTP server and send email, e.g., google mail SMTP server paste("server = smtplib.SMTP('", server, "')", sep=""), "server.ehlo()", "server.starttls()", "server.ehlo()", "server.login(username,password)", "server.sendmail(fromaddr, toaddrs, msg.as_string())", "server.quit()") message.details <- paste("To: ", names(to), " (", unlist(to), ")", "\n", "From: ", names(from), " (", unlist(from), ")", "\n", "Using server: ", server, "\n", "Subject: ", subject, "\n", "With Attachments: ", attachment, "\n", "And the message:\n", message, "\n", sep="") if (confirmBeforeSend) SEND <- winDialog("yesnocancel", paste("Are you sure you want to send this e-mail to ", unlist(to), "?", sep="")) else SEND <- "YES" if (SEND %in% "YES"){ jython.exec(rJython,mail) cat(message.details) } else cat("E-mail Delivery was Canceled by the User") } -- View this message in context: http://r.789695.n4.nabble.com/Email-out-of-R-code-tp3530671p3948061.html Sent from the R help mailing list archive at Nabble.com.