[英]Email an attachment in R with gmail
我希望使用gmail在R中发送带有附件的电子邮件。 我发现sendmailR不能与gmail一起使用,因为它需要身份验证(我无法让它与gmail一起工作所以我认为这是真的,除非有人告诉我我错了,在这种情况下我会发布R输出和错误消息)。 我在这里找到了一个代码片段(LINK) 。 由于该网站建议代码未格式化为发送附件,但我已经让它发送电子邮件。 我想扩展此代码以发送附件(在电子邮件通信中,此代码的作者无法扩展代码以发送附件)。
我想使用gmail发送带有R的电子邮件。 我是一个拥有2.14 beta版R的Windows 7用户。
发送电子邮件但不发送附件的代码:
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 = 'bigbird@gmail.com'",
"toaddrs = 'oscarthegrouch@gmail.com'",
"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 = 'bigbird@gmail.com'",
"password = 'pw'",
#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)
请注意,此消息在talkstats.com上发布。 我没有在那里收到回复(只是成员告诉我他们希望他们能帮忙)。 如果我收到一个可行的解决方案,我也会在那里发布。
这里有一个有效且运行良好的响应:
http://r.789695.n4.nabble.com/Email-out-of-R-code-td3530671.html
感谢nutterb从rhelp列表中得到答案。 感谢所有试图帮助我的人,并对我对蟒蛇的无知感到耐心。
选项1:对于sendmailR
,您似乎遇到了端口25的附加问题。在使用sendmail()
命令之前,您应该能够通过sendmail_options(smtpPort = 587)
指定目标端口。
我不确定这会解决您的其他问题,但它应该为您提供正确的端口。
选项2:如果您想调用Python脚本,那么这个脚本看起来最相关。 您可能会发现最容易进行令牌替换,即使用基本脚本,放入您将简单找到的字符串(即令牌)和替换(即用您想要的字符串替换),然后执行修订后的脚本。
例如,使用上面链接中的脚本(在本地目录中保存为“sendmail_base.py”):
BasePy = scan("sendmail_base.py", what = "character", sep = "\n")
OutPy = gsub("your_email@gmail.com", "yourRealEmailAddress", InFile)
OutPy = gsub("your_password", "yourRealPassword", OutFile)
依此类推,将标题,收件人等替换为您要使用的文本字符串,以及附件文件名的规范相同。 最后,您可以将输出保存到新的Python文件并执行它:
cat(OutPy, file = "sendmail_new.py", sep = "\n")
system("chmod u+x sendmail_new.py; ./sendmail_new.py")
虽然这是一种非常天真的方法,但它很简单,脚本的调试只涉及检查输出Python程序是否正常工作以及R是否生成正确的输出Python程序。 这与调试R通过各种包和语言传递对象的内容形成对比。
我今天从rhelp listserve中看到了这段代码。 也许它会有所帮助:
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@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")
现在我尝试一下:
x<-paste(getwd(),"/Eercise 6.doc",sep="")
send.email(list("bigbird@gmail.com"), list("bigbird@gmail.com"), 'dogs',
'I sent it!', attachment=x, 'bigbird@gmail.com', 'mypassword',
server="smtp.gmail.com:587", confirmBeforeSend=FALSE)
和错误:
> x<-paste(getwd(),"/Eercise 6.doc",sep="")
>send.email(list("bigbird@gmail.com"), list("bigbird@gmail.com"), 'dogs',
+ 'I sent it!', attachment=x, 'bigbird@gmail.com', 'mypassword',
+ server="smtp.gmail.com:587", confirmBeforeSend=FALSE)
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
SyntaxError: ("mismatched character '\\n' expecting '''", ('<string>', 15,
52, "\tpart.add_header('Content-Disposition', 'attachment;\n"))
看看sendmailR
包可以发送附件。 要使sendmail
在Mac上使用gmail需要一些额外的摆弄,但你可以找到使用谷歌搜索来执行此操作的说明。
您可以为新的mailR包提供允许SMTP授权的镜头: http ://rpremraj.github.io/mailR/
然后应该执行以下调用:
2014年5月27日:编辑以下示例,演示如何通过R发送附件:
send.mail(from = "sender@gmail.com",
to = c("recipient1@gmail.com", "recipient2@gmail.com"),
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
authenticate = TRUE,
send = TRUE,
attach.files = c("./download.log", "upload.log"),
file.names = c("Download log", "Upload log"), # optional parameter
file.descriptions = c("Description for download log", "Description for upload log"))
有关使用R使用R的最佳工作示例,请点击此处 。 它的基本内容如下:
Google Developers Console中的一个项目,用于管理您对Gmail API的使用
Jim Hester创建的gmailr R软件包,它封装了Gmail API(在GitHub上开发)
用于数据争用的plyr和dplyr包(如果您愿意,可以使用base R执行此操作)
addresses.csv包含由密钥标识的电子邮件地址的文件。 在我们的例子中,学生姓名。
marks.csv包含您计划发送的电子邮件的可变位的文件,包括与上面相同的标识密钥。 在我们的例子中,作业标记。
脚本send-email-with-rr
为方便起见,在此处粘贴上面一个链接的代码
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@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")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.