Search code examples
htmlremail

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.


Solution

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