1

我正在尝试使用 sendmailR 包发送电子邮件。参考位于 此处的示例。

我可以很好地发送电子邮件,但是当它们出现在我的邮件客户端(Outlook 2013)中时,会显示原始 HTML 代码。任何想法如何纠正这个问题?

收到的示例电子邮件。 https://dl.dropboxusercontent.com/u/3734701/Untitled_Clipping_032614_022810_PM.jpg

4

2 回答 2

1

您需要使用Content-Type标头来指定正确的 MIME 类型。但是,显然作者决定对此进行硬编码,并且headers该函数提供的参数sendmail不适用于Content-Type. 您可以使用该功能真正破解它trace,它可以让您动态地将内容插入其他功能。有关这方面的更多信息,请参阅他的调试教程

在内部函数sendmailR:::.write_mail中作者有以下代码:

 for (part in msg) {
   writeLines(sprintf("--%s", boundary), sock, sep="\r\n")
   if (inherits(part, "mime_part"))
     .write_mime_part(part, sock)
   else if (is.character(part)) { ## Legacy support for plain old string
     ## writeLines(sprintf("--%s", boundary), sock, sep="\r\n")
     writeLines("Content-Type: text/plain; format=flowed\r\n", sock, sep="\r\n")
     writeLines(part, sock, sep="\r\n")
   }

我们将在内部函数中writeLines临时替换该函数以将(非 HTML 电子邮件)更改为. 这将强制使用正确的 MIME 类型。sendmailRtext/plaintext/html

 send_html <- function(...) {
   suppressMessages(trace(sendmailR:::.write_mail, quote(
    writeLines <- function(x, ...) {
     if(grepl('^Content-Type: text/plain', x)) base::writeLines(gsub('\\/plain', '\\/html', x), ...)
     else base::writeLines(x, ...)
    }), at = 9))
    capture.output(sendmail(...))
    suppressMessages(untrace(sendmailR:::.write_mail)) # undo our hack
 }
 send_html('you@gmail.com','you@gmail.com','hello','<h1> Hows it going man? </h1>')

神奇的数字 9 来自于使用print(as.list(body(sendmailR:::.write_mail)))和观察代码的注入位置。

示例电子邮件

于 2014-03-26T21:43:49.220 回答
0

您可以尝试 github https://github.com/rpremraj/mailR上提供的 mailR 包的开发版本

使用 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),
          authenticate = TRUE,
          send = TRUE)
于 2014-05-08T20:25:53.490 回答