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

时间:2015-08-17 16:32:13

标签: python r email pdf jython

我正在尝试使用jython包在R中通过gmail发送带附件的电子邮件。我意识到jython是R中的python语言,问题是我不懂python,因此希望精通该语言的人可以帮助我。

为什么我要使用jython?因为SO-sendmailR上看起来很流行的其他电子邮件包不适用于gmail,因为它需要身份验证。

我使用的源代码是here。正如该人在链接中所说,原始代码是为没有附件的电子邮件构建的。我能够完美地运行这个版本没有错误(即能够通过R中的jython发送没有附件的电子邮件)。

但是,当我尝试添加一些脚本来包含附件时,它不起作用。 这是我尝试格式化此代码以处理附件(登录/电子邮件详细信息消失):

rJython <- rJython()
rJython$exec( "import smtplib" ) 
rJython$exec("from email.MIMEMultipart import MIMEMultipart")
rJython$exec("from email.MIMEBase import MIMEBase")
rJython$exec("from email.MIMEText import MIMEText")
rJython$exec("import email.utils") 
rJython$exec("import smtplib")
rJython$exec("import os")
rJython$exec("from email.Utils import COMMASPACE, formatdate")
rJython$exec("from email import Encoders")



mail<-c( 
  #Email settings 
  "fromaddr = '@gmail.com'", 
  "toaddrs  = '@gmail.com'", 
  "msg = MIMEMultipart('This is the body of the message.')", 
  "msg['From'] = email.utils.formataddr(('', fromaddr))", 
  "msg['To'] = email.utils.formataddr(('', toaddrs))", 
  "msg['Subject'] = 'Monitor'", 

  #SMTP server credentials 
  "username = ''", 
  "password = ''", 

  #Attach file
  "files = 'E:/R/R_Data/output/S.pdf'",
  "msg.attach(MIMEText('Your message contents'))", 

  "for f in files:", 
  "    part = MIMEBase('application', 'octet-stream')", 
  "    part.set_payload( open(f, 'rb').read() )", 
  "    Encoders.encode_base64(part)", 
  "    part.add_header('Content-Disposition', 'attachment;
  "    filename=\"S.pdf\"' % os.path.basename(files))", 
  "    msg.attach(part)", 

  #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) 

当我运行它时,我收到以下错误:

Error in jython.exec(rJython, mail) : [Errno 2] ENOENT: 'E'

我的理解是Python中的这个错误意味着“没有这样的文件或目录”。

我可能做错的事情: 1.我是否错误地找到了我的附件目录? 2.代码是否无效,因为我只在目录中指定一个要附加的文件? 3.代码可以处理.pdfs吗? 4.代码的其他部分是否不正确?

版本详情:       平台x86_64-pc-mingw32
      拱x86_64
      os mingw32
      system x86_64,mingw32
      状态
      专业2       未成年人13.1
      2011年       月07日       第08天       svn rev 56322
      语言R
      version.string R版本2.13.1(2011-07-08)

非常感谢您的帮助。 甲

2 个答案:

答案 0 :(得分:0)

以下是Google帐户和 mailR 包的示例:

library("mailR")

mail.from <- "yourmail@gmail.com"
mail.to <- "recipent@gmail.com"
mail.subject <- "subject"

smtp.host.name <- "smtp.gmail.com"  # if you are using gmail smtp keep this
smtp.host.port <- 465               # if you are using gmail smtp keep this
smtp.user.name <- "yourAccountGmail@gmail.com"
smtp.host.passwd <- "yourpass"
smtp.ssl <- TRUE                    # if you are using gmail smtp keep this

smtp = list(host.name = smtp.host.name, port = smtp.host.port,
              ssl=smtp.ssl, user.name = smtp.user.name,
              passwd = smtp.host.passwd)

send.mail(from = mail.from,
            to = mail.to,
            subject = mail.subject,
            body = "BodyText",
            attach.files = c("path/to/your_file.ext"),
            smtp = smtp,
            authenticate = TRUE,
            send = TRUE)

此外,首先为您的Google帐户启用SMTP访问权限。 https://www.google.com/settings/security/lesssecureapps

答案 1 :(得分:0)

ENOENT表示file not found - 您的代码只占用files的第一个字符,当然 - 它不是有效的文件名。要使用jython通过带有PDF附件的Gmail成功发送电子邮件,您可以使用:

library(rJython)
rJython <- rJython()
rJython$exec( "import smtplib" ) 
rJython$exec("from email.MIMEMultipart import MIMEMultipart")
rJython$exec("from email.MIMEBase import MIMEBase")
rJython$exec("from email.MIMEText import MIMEText")
rJython$exec("import email.utils") 
rJython$exec("import smtplib")
rJython$exec("import os")
rJython$exec("from email.Utils import COMMASPACE, formatdate")
rJython$exec("from email import Encoders")

mail<-c( 
  #Email settings 
  "fromaddr = '@gmail.com'", 
  "toaddrs  = '@gmail.com'", 
  "msg = MIMEMultipart('This is the body of the message.')", 
  "msg['From'] = email.utils.formataddr(('', fromaddr))", 
  "msg['To'] = email.utils.formataddr(('', toaddrs))", 
  "msg['Subject'] = 'Monitor'", 

  #SMTP server credentials 
  "username = ''", 
  "password = ''", 

  #Attach file
  "files = ['E:/R/R_Data/output/S.pdf']",
  "msg.attach(MIMEText('Your message contents'))", 

  "for f in files:", 
  "    part = MIMEBase('application', 'octet-stream')", 
  "    part.set_payload( open(f, 'rb').read() )", 
  "    Encoders.encode_base64(part)", 
  "    part.add_header('Content-Disposition', 'attachment', filename='S.pdf')",
  "    msg.attach(part)", 

  #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) 

如另一个答案所述,你首先得到allow less secure apps to access your Gmail。然后结果将是:

enter image description here