sendmailR(第2部分):将文件作为邮件附件发送

Geo*_*tas 21 email smtp r email-attachments

按照相关问题中提供的说明,我能够发送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")))
Run Code Online (Sandbox Code Playgroud)

Rah*_*raj 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)
Run Code Online (Sandbox Code Playgroud)

编辑(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)
Run Code Online (Sandbox Code Playgroud)


mbq*_*mbq 7

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

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);
}
Run Code Online (Sandbox Code Playgroud)

可以像这样使用:

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="..."))
Run Code Online (Sandbox Code Playgroud)

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

编辑(现在不太相关):可以在此处找到有关如何制作MIME消息的最深入见解.


the*_*mel 6

请注意,sendmailR通过msg列出mime_type对象列表可以立即使用当前版本的支持附件,即现在

sendmail( from,to,subject,
          msg=list(mime_part("Here's an attachment for you!"), 
          mime_part(attachmentFileName)), control, headers)`
Run Code Online (Sandbox Code Playgroud)


小智 5

下面是一个为日常批处理作业设置的示例,例如在 R 中使用 sendmail() 进行设置(可通过 sendmailR 包获得),并带有多个附件(一个 CSV、一个 PDF):

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

> 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"
Run Code Online (Sandbox Code Playgroud)

现在,在本文末尾创建 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"                     
Run Code Online (Sandbox Code Playgroud)

在此指定邮件服务器:

> mailControl=list(smtpServer="mail.whateveryourmailserveris.com")
Run Code Online (Sandbox Code Playgroud)

定义附件 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="")
Run Code Online (Sandbox Code Playgroud)

定义附件 1 对象:

> attachmentObject1 <- mime_part(x=attachmentPath1,name=attachmentName1)
Run Code Online (Sandbox Code Playgroud)

定义附件 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="")
Run Code Online (Sandbox Code Playgroud)

定义附件 2 对象:

> attachmentObject2 <- mime_part(x=attachmentPath2,name=attachmentName2)
Run Code Online (Sandbox Code Playgroud)

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

> 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"
Run Code Online (Sandbox Code Playgroud)

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

> sendmail(from=from, to=to, subject=subject, msg=bodyWithAttachment, control=mailControl)
Run Code Online (Sandbox Code Playgroud)