如何在 Windows 中从 R 发送带有附件的电子邮件 [英] How to send an email with attachment from R in windows

查看:21
本文介绍了如何在 Windows 中从 R 发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 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 的信息在文件 -> 帐户设置 -> 帐户设置 -> 双击您的帐户 ->服务器"框中的文本

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)

另外,可以通过在msg列表中添加另一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆