Following the directions provided in this related question, I was able to send html formated mail messages. Now the question is this: How should I modify the following code, in order to attach one or more files (of any type) to this message?
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")))
With the mailR package (https://github.com/rpremraj/mailR), you could send HTML emails and additionally attach files with ease as below:
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)
Edit (2014-05-13):
mailR has been updated to allow different character encodings. Below is an example to send the message as 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)
A working (for me at least) function:
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);
}
Can be used like this:
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="..."))
Be warned that message sent by this example will probably be marked as spam, since it is a short text and a big picture -- nevertheless for larger messages and, let's say, pdf attachments it should go through. If not, you may consider adding also a text version of the message.
EDIT (less relevant now): The most deep insight on how to make MIME messages can be found here.
Note that current versions of sendmailR
support attachments out of the box by making msg
a list of mime_type
objects, i.e. you'd now
sendmail( from,to,subject,
msg=list(mime_part("Here's an attachment for you!"),
mime_part(attachmentFileName)), control, headers)`
I would give up on using R for this. Working, cross-platform, stable solutions for doing this in Python exist, and you can call Python from R.
If I had to fit a mixed effects model in a Python program I'd call R to do it - if I want to do a systems task like send email in R I'll call Python to do it. Its worth learning if you don't know it yet.
Here's an example that is setup for a daily batch job like setting using sendmail() in R (available with the package sendmailR) with multiple attachments (one CSV, one PDF):
Setting up date information to reference in the file names:
> 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"
Now create some of the needed information for sendmail() function at the end of this post:
> 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"
Specify mail server here:
> mailControl=list(smtpServer="mail.whateveryourmailserveris.com")
Define attachment 1 path and name:
> 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="")
Define attachment 1 object:
> attachmentObject1 <- mime_part(x=attachmentPath1,name=attachmentName1)
Define attachment 2 path and name:
> 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="")
Define attachment 2 object:
> attachmentObject2 <- mime_part(x=attachmentPath2,name=attachmentName2)
Now combine the body of the email with your attachments:
> 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"
Send the email using sendmail() function:
> sendmail(from=from, to=to, subject=subject, msg=bodyWithAttachment, control=mailControl)