Rohit Das
Rohit Das

Reputation: 2042

How to send HTML email using R

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 plain text email works just fine, but then am unable to switch from plain text to HTML.

My code:

require(sendmailR)
from <- "[email protected]"
message = "<HTML><body><b>Hello</b></body></HTML>"
to = c("[email protected]")
subject = "Test Monitor Alert"

sendmail(from, to, subject, msg = msg,control=list(smtpServer="smtp-gw1.wal-mart.com"),headers=list("Content-Type"="text/html; charset=UTF-8; format=flowed"))

I do get the email, but its in plain text and the email body contains the message as is instead of the HTML formatted text. Please help.

Upvotes: 11

Views: 15191

Answers (6)

Ben
Ben

Reputation: 1154

Revisiting this conversation, I used the mime_part_html command from the sendmailR package to implement HTML in my email:

mymsg<-mime_part_html(paste0("Dear Genius:<p>Blah blah, and also you're amazing!"))
)

And then:

sendmail_options(smtpPort="25") # with port 25 do NOT send credentials
sendmail("[email protected]",to=c("[email protected]"),
  subject=paste("Another email from me!"),
  msg=mymsg,
  control=list(smtpServer="10.q.x.y", verbose = TRUE)         
  )

Upvotes: 0

Christopher Louden
Christopher Louden

Reputation: 7592

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).

Upvotes: 6

Rahul Premraj
Rahul Premraj

Reputation: 1595

With the mailR package (https://github.com/rpremraj/mailR), you could send HTML emails with ease as below:

send.mail(from = "[email protected]",
          to = c("[email protected]", "[email protected]"),
          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)

Upvotes: 4

Karl Forner
Karl Forner

Reputation: 4414

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, ...)

Upvotes: 16

agstudy
agstudy

Reputation: 121618

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)

Upvotes: 3

AwokeKnowing
AwokeKnowing

Reputation: 8236

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.

Upvotes: 0

Related Questions