且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用R发送邮件正文中的html文件?

更新时间:2022-10-29 20:59:44

使用 mailR 包轻松发送HTML格式的电子邮件如下:

  send.mail(from =sender@gmail.com,
to = c(recipient1@gmail.com,recipient2@gmail.com),
subject = 电子邮件的主题,
body =< html> apache徽标 - < 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),
authenticate = TRUE,
send = TRUE)


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<-"<sender@enterprise.lan>"
to<-c("<reciever@enterprise.com>")
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?

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

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>", # 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)