通过 gmail 在 R 中发送附件(使用 jython)

Emailing attachments in R via gmail (using jython)

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

我为什么要使用 jython?因为在 SO 上似乎很流行的另一个电子邮件包 - sendmailR - 不适用于 gmail,因为它需要身份验证。

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

但是,当我尝试添加一些脚本来包含附件时,它不起作用。 这是我尝试格式化此代码以处理附件(login/email 详细信息被删除):

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 这个错误的意思是 "no such file or directory".

我可能做错的事情: 1. 是不是附件目录放错了? 2. 代码不工作是因为我在要附加的目录中只指定了一个文件吗? 3.代码不能处理.pdf吗? 4. 是不是其他地方的代码不对?

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

非常感谢您的帮助。 A.

下面是 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

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

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。那么结果将是: