如何使用R发送邮件正文中的html文件? [英] how to send html file in the body of the mail using R?

查看:311
本文介绍了如何使用R发送邮件正文中的html文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的Rmarkdown代码,我有一个html文件。现在我需要直接在电子邮件正文中发送包含此HTML文件的电子邮件,而不是在附件中。我正在尝试从这篇文章的解决方案:使用R发送HTML邮件post
但它似乎只适用于html文本而不适用于html文件。
这里我试过的:

  library(sendmailR)
from< - < ; sender@enterprise.lan>
到< -c(< reciever@enterprise.com>)
message =./ TEST.html
subject =测试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))

该试用版向我发送了一封电子邮件,但附有TEST.html文件,而不是直接在本体中。
显然我做错了什么,我在这个任务上挣扎了几天,请问有人可以帮我一下吗?

解决方案

使用 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)

这篇关于如何使用R发送邮件正文中的html文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆