Search code examples
htmlremailsendmailr

how to send html file in the body of the mail using R?


I have an html file as an result of my Rmarkdown code. Now I need to send an email with this html file directly in the body of the email, and not in the attachement. I was trying the solution from this post: "send HTML email using R" post But it seems that it works only with html text and not html file. Here what I've tried to do:

library(sendmailR)
from<-"<[email protected]>"
to<-c("<[email protected]>")
message ="./TEST.html"
subject = "Test HTML"
msg <- mime_part(message)
msg[["headers"]][["Content-Type"]] <- "file/html"
sendmail(from, to, subject, msg = msg,control=list(smtpServer="localhost"),headers=list("Content-Type"="file/html; charset=UTF-8; format=flowed"))

This trial send me an email but with "TEST.html" file attached, and not in the body directly. Obviously I'm doing something wrong, I'm struggling with this task for days, can someone help me, please?


Solution

  • Try the mailR package to easily send emails in HTML format as follows:

    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>", # can also point to local file (see next example)
              html = TRUE,
              smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
              authenticate = TRUE,
              send = TRUE)