我希望使用 gmail 在 R 中发送一封带有附件的电子邮件。我发现 sendmailR 不能与 gmail 一起使用,因为它需要身份验证(我无法让它与 gmail 一起使用,所以我认为这是真的,除非有人告诉我我错了,在这种情况下我会发布 R输出和错误消息)。我找到了一个代码片段 here (LINK) .正如该网站所暗示的那样,该代码未格式化为发送附件,但我已将其用于发送电子邮件。我想扩展此代码以发送附件(在电子邮件通信中,此代码的作者无法扩展代码以发送附件)。
我想使用 gmail 使用 R 发送电子邮件。我是 Windows 7 用户,使用 R 的 2.14 beta 版本。
发送电子邮件但不发送附件的代码:
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 上交叉发布的。我在那里没有收到回复(只是成员告诉我他们希望他们能提供帮助)。如果我收到一个可行的解决方案,我也会在那里发布。
最佳答案
您正在 R 环境中运行 Jython 代码,因此您正在寻找一种使用 Jython 语言而不是 R 语言发送附件的方法。
由于 Jython 基本上是 Python,因此这里有一种使用 Python 发送带有附件的电子邮件的方法:How to send email attachments with Python .
您只需将这些代码一起破解到您的代码中。
关于python - 使用 gmail 在 R 中通过电子邮件发送附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7802360/