【问题标题】:Emailing attachments in R via gmail (using jython)通过 gmail 在 R 中通过电子邮件发送附件(使用 jython)
【发布时间】:2015-11-10 09:16:29
【问题描述】:

我正在尝试使用 jython 包通过 gmail 在 R 中发送带有附件的电子邮件。我意识到 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
操作系统 mingw32
系统 x86_64、mingw32
状态
专业2
未成年人 13.1
2011 年
07月
第 8 天
svn 版本 56322
语言 R
version.string R 版本 2.13.1 (2011-07-08)

非常感谢您的帮助。 A.

【问题讨论】:

标签: python r email pdf jython


【解决方案1】:

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

【讨论】:

  • 完美运行,非常感谢您的帮助!非常感谢。
【解决方案2】:

以下是 google 帐户和 ma​​ilR 包的示例:

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

【讨论】:

    猜你喜欢
    • 2011-12-09
    • 2016-07-19
    • 2012-01-07
    • 2023-03-19
    • 1970-01-01
    • 2011-06-08
    • 2017-05-12
    相关资源
    最近更新 更多