22

按照这个相关问题中提供的指示,我能够发送 html 格式的邮件。现在的问题是:我应该如何修改以下代码,以便将一个或多个文件(任何类型)附加到此消息?

library(sendmailR)

from <- "<sendmailR@myserver.mycompany.com>"
to <- c("<someone@mycompany.com>","<anotherone@mycompany.com>")
subject <- iconv("Message Title", to = "utf8")

msg <- "<hr size='2' width='33%' style='text-align: left;'><font size='2'>
  <i>This email was sent automatically using <a href='http://finzi.psych.upenn.edu/R/library/sendmailR/html/00Index.html' rel='nofollow' target='_blank'>sendmailR</a>.<br>
  Please do not reply directly to this e-mail.</i></font>"

msg <- iconv(msg, to = "utf8")

sapply(to,function(x) sendmail(from, x, subject, msg, control=list(smtpServer="###.###.###.###"), headers=list("Content-Type"="text/html; charset=UTF-8; format=flowed")))
4

5 回答 5

9

使用 mailR 包(https://github.com/rpremraj/mailR),您可以轻松发送 HTML 电子邮件并附加文件,如下所示:

send.mail(from = "sender@gmail.com",
          to = c("recipient1@gmail.com", "recipient2@gmail.com"),
          subject = "Subject of the email",
          body = "<html>The apache logo - <img src=\"http://www.apache.org/images/asf_logo_wide.gif\"></html>",
          html = TRUE,
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
          attach.files = c("./download.log", "upload.log"),
          authenticate = TRUE,
          send = TRUE)

编辑(2014-05-13):

mailR 已更新为允许不同的字符编码。下面是一个以 UTF-8 格式发送消息的示例。

send.mail(from = "Sender Name <sender@gmail.com>",
          to = "recipient@gmail.com",
          subject = "A quote from Gandhi",
          body = "In Hindi :  थोडा सा अभ्यास बहुत सारे उपदेशों से बेहतर है।
                  English translation: An ounce of practice is worth more than tons of preaching.",
          encoding = "utf-8",
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = T),
          authenticate = TRUE,
          send = TRUE)
于 2014-05-08T20:29:46.287 回答
8

一个工作(至少对我来说)功能:

sendMessage<-function(contents,subject,from,to,attMIME,attachment,control){    
   msg<-list(contents,sendmailR:::.file_attachment(attachment,attachment,attMIME));
   sendmail(from=from,to=to,subject=subject,msg=msg,control=control);
}

可以这样使用:

png('a.png');hist(rnorm(700));dev.off()
sendMessage('Here you have a nice histogram:',
'Nice picture',
'from@example.com',
'to@example.com',
'image/png',
'a.png',list(smtpServer="..."))

请注意,此示例发送的邮件可能会被标记为垃圾邮件,因为它是一个简短的文本和一张大图片——但对于较大的邮件,比如说,它应该通过的 pdf 附件。如果没有,您可以考虑添加消息的文本版本。

编辑(现在不太相关):关于如何制作 MIME 消息的最深刻见解可以在这里找到。

于 2010-08-26T08:31:54.343 回答
6

请注意,当前版本的sendmailR支持附件开箱即用msg的对象列表mime_type,即你现在

sendmail( from,to,subject,
          msg=list(mime_part("Here's an attachment for you!"), 
          mime_part(attachmentFileName)), control, headers)`
于 2013-01-17T09:43:19.390 回答
5

这是一个为日常批处理作业设置的示例,例如在 R 中使用 sendmail() 设置多个附件(一个 CSV,一个 PDF)(与包 sendmailR 一起提供):

设置要在文件名中引用的日期信息:

> yesterday_date_stuff  <- new.env()
> yesterday_date_stuff[['month']] <- strftime(Sys.Date()-1, format="%m")
> yesterday_date_stuff[['day']] <- strftime(Sys.Date()-1, format="%d")
> yesterday_date_stuff[['year']] <- strftime(Sys.Date()-1, format="%y")
> yesterday_date_stuff$month
[1] "03"
> yesterday_date_stuff$day
[1] "29"
> yesterday_date_stuff$year
[1] "17"

现在在本文结尾处为 sendmail() 函数创建一些所需的信息:

> from <- "youremail@whateveryourmailserveris.com"
> to <- c("person_A_to_send_email_to@whatever.com", "person_B_to_send_email_to@whatever.com", "person_C_to_send_email_to@whatever.com")
> subject <- paste("whatever you want subject line to read for batch job analyzing data for ", yesterday_date_stuff$month, "/", yesterday_date_stuff$day, "/", yesterday_date_stuff$year, sep="")
> body <- "Text to insert into the body of your email"                     

在此处指定邮件服务器:

> mailControl=list(smtpServer="mail.whateveryourmailserveris.com")

定义附件 1 路径和名称:

> attachmentPath1 <- paste("file1name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".csv", sep="")
> attachmentName1 <- paste("file1name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".csv", sep="")

定义附件 1 对象:

> attachmentObject1 <- mime_part(x=attachmentPath1,name=attachmentName1)

定义附件 2 路径和名称:

> attachmentPath2 <- paste("file2name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".pdf", sep="")
> attachmentName2 <- paste("file2name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".pdf", sep="")

定义附件 2 对象:

> attachmentObject2 <- mime_part(x=attachmentPath2,name=attachmentName2)

现在将电子邮件正文与您的附件结合起来:

> bodyWithAttachment <- list(body,attachmentObject1, attachmentObject2)
> bodyWithAttachment
[[1]]
[1] "Text to insert into the body of your email"

[[2]]
<environment: 0x000000004efff188>
attr(,"class")
[1] "mime_part"

[[3]]
<environment: 0x00000000407a1b68>
attr(,"class")
[1] "mime_part"

使用 sendmail() 函数发送电子邮件:

> sendmail(from=from, to=to, subject=subject, msg=bodyWithAttachment, control=mailControl)
于 2017-03-30T18:30:28.390 回答
3

我会放弃为此使用 R。存在用于在 Python 中执行此操作的工作、跨平台、稳定的解决方案,您可以从 R 调用 Python。

如果我必须在 Python 程序中拟合混合效果模型,我会调用 R 来完成它——如果我想做一个系统任务,比如在 R 中发送电子邮件,我会调用 Python 来完成它。如果你还不知道它值得学习。

于 2010-08-26T07:14:18.033 回答