使用gmail通过电子邮件发送R中的附件

时间:2011-10-18 04:17:32

标签: python r jython

我希望使用gmail在R中发送带有附件的电子邮件。我发现sendmailR不能与gmail一起使用,因为它需要身份验证(我无法让它与gmail一起工作所以我认为这是真的,除非有人告诉我我错了,在这种情况下我会发布R输出和错误消息)。我找到了here (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上发布。我没有在那里收到回复(只是成员告诉我他们希望他们能帮忙)。如果我收到一个可行的解决方案,我也会在那里发布。

8 个答案:

答案 0 :(得分:17)

您正在R环境中运行Jython代码,因此您正在寻找一种使用Jython语言而不是R来发送附件的方法。

由于Jython基本上是Python,因此这里是一种发送带有Python附件的电子邮件的方法:How to send email attachments with Python

你只需要将这些代码整合到你的代码中。

答案 1 :(得分:6)

这里有一个有效且运作良好的回复:
http://r.789695.n4.nabble.com/Email-out-of-R-code-td3530671.html

感谢nutterb从rhelp列表中获得答案。感谢所有试图帮助我的人,并对我对蟒蛇的无知感到耐心。

答案 2 :(得分:5)

选项1:对于sendmailR,您似乎遇到了端口25的附加问题。在使用{sendmail_options(smtpPort = 587)之前,您应该可以通过sendmail()指定目标端口1}}命令。

我不确定这会解决您的其他问题,但它应该能为您提供合适的端口。


选项2:如果您想调用Python脚本,this one看起来最相关。您可能会发现最容易进行令牌替换,即使用基本脚本,放入您将要找到的字符串(即令牌)和&amp;替换(即替换为您想要的字符串),然后执行修改后的脚本。

例如,使用上面链接中的脚本(在本地目录中保存为“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通过各种包和语言传递对象的内容形成对比。

答案 3 :(得分:3)

我今天从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"))

答案 4 :(得分:2)

查看可以发送附件的包sendmailR。要让sendmail在Mac上使用gmail,需要进行一些额外的调整,但是您可以使用Google搜索找到执行此操作的说明。

答案 5 :(得分:1)

您可以为新的mailR包提供允许SMTP授权的镜头:http://rpremraj.github.io/mailR/

然后应该进行以下调用:

27/05/14:编辑以下示例,演示如何通过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"))

答案 6 :(得分:1)

使用R使用Gmail时,可以使用最佳工作示例here。 它的基本内容如下:

  1. Google Developers Console中用于管理您对Gmail API的使用的项目

  2. Jim Hester的gmailr R软件包,它封装了Gmail API(在GitHub上开发)

  3. 用于数据争论的plyr和dplyr软件包(如果您愿意,可以使用基础R执行此操作)

  4. addresses.csv包含由密钥标识的电子邮件地址的文件。在我们的例子中,学生姓名。

  5. marks.csv包含您计划发送的电子邮件的可变位的文件,包括与上述相同的标识密钥。在我们的例子中,作业标记。

  6. 脚本send-email-with-r.r

答案 7 :(得分:0)

为了方便起见,在此处粘贴上面一个link的代码

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")
}