I have searched SO and well as google and cannot seem to find a solution to my problem. I am trying to send a HTML formatted email within R using sendmailR package. The plai
I prefer to use a specialized Mail agent for this type of task than using an R package. You can for example use Mutt. Available for linux and windows.
Here I am using option -e to send a command:
writeLines(message,
p<-pipe(paste('mutt -e ','"set content_type=text/html"',
from,to,' -s ', subject))
close(p)
With the mailR package (https://github.com/rpremraj/mailR), you could send HTML emails 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)
This is possible, cf https://stackoverflow.com/a/21930556/448145 Just add:
msg <- mime_part(message)
msg[["headers"]][["Content-Type"]] <- "text/html"
sendmail(from, to, subject, msg = msg, ...)
If nothing else, you can post the html to a php page, and have php send the html email. We'll see if anyone else has a better solutions.
sendmailR cannot do this because it is hard-coded to send the message part out as text. If you look at the packages source, line 38 of sendmail.R is the following:
writeLines("Content-Type: text/plain; format=flowed\r\n", sock, sep="\r\n")
Change that to
writeLines("Content-Type: text/html; format=flowed\r\n", sock, sep="\r\n")
like you tried to do through the options and it will work.
Update: sendmailR now allows html emails (see Karl's answer below and https://stackoverflow.com/a/21930556/448145).