如何在Windows中从R发送带有附件的电子邮件 [英] How to send an email with attachment from R in windows
问题描述
我安排了从Windows计算机运行的R脚本。
I have a scheduled an R script running from a windows machine.
完成后,希望该脚本自动发送带有某些日志文件的电子邮件。
After it finishes, I wish this script to automatically send an email with some log file attached.
将 shell()
与其他脚本一起使用是可能的,但是我想知道是否有更好的解决方案在R中。
谢谢。
Using shell()
with some other scripts may be possible, but I was wondering if there is a better solution within R.
Thanks.
推荐答案
sendmailR在Windows 7上对我有效。我引用了 http://cran.es.r-project.org/web/packages/sendmailR/sendmailR .pdf
sendmailR works for me on Windows 7. I referenced http://cran.es.r-project.org/web/packages/sendmailR/sendmailR.pdf
smtpServer = Outlook 2010的信息位于文件->帐户设置->帐户设置->双击您的帐户->服务器中的文本 box
smtpServer= info for Outlook 2010 is in File -> Account Settings -> Account Settings -> double click your account -> text in "Server" box
library(sendmailR)
#set working directory
setwd("C:/workingdirectorypath")
#####send plain email
from <- "you@account.com"
to <- "recipient@account.com"
subject <- "Email Subject"
body <- "Email body."
mailControl=list(smtpServer="serverinfo")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
#####send same email with attachment
#needs full path if not in working directory
attachmentPath <- "subfolder/log.txt"
#same as attachmentPath if using working directory
attachmentName <- "log.txt"
#key part for attachments, put the body and the mime_part in a list for msg
attachmentObject <- mime_part(x=attachmentPath,name=attachmentName)
bodyWithAttachment <- list(body,attachmentObject)
sendmail(from=from,to=to,subject=subject,msg=bodyWithAttachment,control=mailControl)
此外,可以通过添加另一个文件来发送多个文件mime_part到味精列表如下(我也将其压缩):
In addition, multiple files can be sent by adding another mime_part to the msg list as follows (I also condensed it):
attachmentObject <- mime_part(x="subfolder/log.txt",name="log.txt")
attachmentObject2 <- mime_part(x="subfolder/log2.txt",name="log2.txt")
bodyWithAttachment <- list(body,attachmentObject,attachmentObject2)
这篇关于如何在Windows中从R发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!