我希望使用 gmail 在 R 中发送一封带有附件的电子邮件。我发现 sendmailR 不能与 gmail 一起使用,因为它需要身份验证(我无法让它与 gmail 一起使用,所以我认为这是真的,除非有人告诉我我错了,在这种情况下我将发布 R输出和错误消息)。我发现了一个代码片段在这里(链接) http://r.789695.n4.nabble.com/Email-out-of-R-code-td3530671.html。正如该网站建议的那样,代码的格式不适合发送附件,但我已经可以发送电子邮件了。我想扩展此代码以发送附件(在电子邮件通信中,此代码的作者无法扩展代码以发送附件)。
我想使用 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 = '[email protected] /cdn-cgi/l/email-protection'",
"toaddrs = '[email protected] /cdn-cgi/l/email-protection'",
"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 = '[email protected] /cdn-cgi/l/email-protection'",
"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 上交叉发布的。我在那里没有收到回复(只是成员告诉我他们希望能提供帮助)。如果我收到可行的解决方案,我也会将其发布在那里。