11

我搜索了 SO 和谷歌,似乎无法找到解决我的问题的方法。我正在尝试使用 sendmailR 包在 R 中发送 HTML 格式的电子邮件。纯文本电子邮件工作得很好,但无法从纯文本切换到 HTML。

我的代码:

require(sendmailR)
from <- "alertbot@companyname.com"
message = "<HTML><body><b>Hello</b></body></HTML>"
to = c("me@companyname.com")
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"))

我确实收到了电子邮件,但它是纯文本的,并且电子邮件正文包含消息,而不是 HTML 格式的文本。请帮忙。

4

5 回答 5

16

这是可能的,cf https://stackoverflow.com/a/21930556/448145 只需添加:

msg <- mime_part(message)
msg[["headers"]][["Content-Type"]] <- "text/html"
sendmail(from, to, subject, msg = msg, ...)
于 2014-02-24T16:23:55.097 回答
6

sendmailR不能这样做,因为它是硬编码的,将消息部分作为文本发送出去。如果您查看包源,sendmail.R 的第 38 行如下:

writeLines("Content-Type: text/plain; format=flowed\r\n", sock, sep="\r\n")

将其更改为

writeLines("Content-Type: text/html; format=flowed\r\n", sock, sep="\r\n")

就像你试图通过选项做的那样,它会起作用。

更新sendmailR现在允许 html 电子邮件(请参阅下面 Karl 的回答和https://stackoverflow.com/a/21930556/448145)。

于 2013-11-07T19:58:08.673 回答
4

使用 mailR 包(https://github.com/rpremraj/mailR),您可以轻松发送 HTML 电子邮件,如下所示:

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>",
          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)
于 2014-05-15T07:49:07.687 回答
3

我更喜欢使用专门的邮件代理来处理这类任务,而不是使用 R 包。例如,您可以使用Mutt。适用于 linux 和 windows。

在这里,我使用选项 -e 发送命令:

writeLines(message,
           p<-pipe(paste('mutt -e ','"set content_type=text/html"',
                          from,to,' -s ', subject))
close(p)
于 2013-11-07T20:10:29.370 回答
0

如果不出意外,您可以将 html 发布到 php 页面,并让 php 发送 html 电子邮件。我们会看看其他人是否有更好的解决方案。

于 2013-11-07T19:56:58.173 回答