2014-03-26 34 views

回答

1

您需要使用Content-Type頭指定正確的MIME類型。但是,顯然作者決定對此進行硬編碼,sendmail函數提供的參數headers將不適用於Content-Type。你可以使用trace函數真正入侵它,它可以讓你動態地將內容插入到其他函數中。欲瞭解更多信息,請參閱他的Debugging tutorial

在內部功能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") 
    } 

我們將暫時代替內的sendmailR內部函數的函數writeLines改變text/plain(非HTML電子郵件)text/html。這將強制正確的MIME類型。

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('[email protected]','[email protected]','hello','<h1> Hows it going man? </h1>') 

幻數9來自使用print(as.list(body(sendmailR:::.write_mail)))和目測其中注入的代碼。

example email

+0

謝謝你即時回覆。儘管如此,我仍然無法複製您的示例。我運行你創建的函數(send_html)並查看一些問題。我在腳本中使用了我的Gmail電子郵件地址,並得到以下錯誤:socketConnection(host = server,port = port,blocking = TRUE)時出錯: 無法打開連接另外:警告消息: 在socketConnection(host = server ,port = port,blocking = TRUE): ASPMX.L.GOOGLE.COM:25無法打開 – user1607359

+0

您能否提供更多信息,例如您看到的錯誤?我試過了,它適用於我。 –

+0

這是本地SMTP服務器上安裝的問題。爲了確保,你是否用正確的「to」和「from」地址替換了第一個和第二個參數? –

0

你可以嘗試在GitHub上可用的mailR包的開發版本https://github.com/rpremraj/mailR

使用mailR,你可以在HTML格式如下發送電子郵件:

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>", 
      html = TRUE, 
      smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE), 
      authenticate = TRUE, 
      send = TRUE)