根据this相关问题中提供的说明,我能够发送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")))
答案 0 :(得分:8)
使用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)
答案 1 :(得分: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);
}
可以像这样使用:
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邮件的最深入见解here。
答案 2 :(得分:4)
请注意,sendmailR
的当前版本通过msg
列出mime_type
个对象来支持附件,即您现在
sendmail( from,to,subject,
msg=list(mime_part("Here's an attachment for you!"),
mime_part(attachmentFileName)), control, headers)`
答案 3 :(得分:3)
我会放弃使用R来做到这一点。存在用于在Python中执行此操作的工作,跨平台,稳定的解决方案,您可以从R。
调用Python如果我必须在Python程序中使用混合效果模型,我会调用R来执行它 - 如果我想在R中执行像发送电子邮件这样的系统任务,我会调用Python来执行此操作。如果你还不知道,它值得学习。
答案 4 :(得分:1)
以下是为每日批处理作业设置的示例,例如使用带有多个附件(一个CSV,一个PDF)的R(包sendmailR)中的sendmail()进行设置:
在文件名中设置要引用的日期信息:
> 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)