sendmailR パッケージを使用して電子メール メッセージを送信しようとしています。ここにある例を参照して ください。
メールは問題なく送信できますが、メール クライアント (Outlook 2013) に表示されると生の HTML コードが表示されます。これを修正する方法はありますか?
受け取ったメールの例。 https://dl.dropboxusercontent.com/u/3734701/Untitled_Clipping_032614_022810_PM.jpg
sendmailR パッケージを使用して電子メール メッセージを送信しようとしています。ここにある例を参照して ください。
メールは問題なく送信できますが、メール クライアント (Outlook 2013) に表示されると生の HTML コードが表示されます。これを修正する方法はありますか?
受け取ったメールの例。 https://dl.dropboxusercontent.com/u/3734701/Untitled_Clipping_032614_022810_PM.jpg
Content-Type
正しい MIME タイプを指定するには、ヘッダーを使用する必要があります。ただし、作成者はこれをハードコードすることにしたようで、関数が提供するheaders
パラメーターは. 関数を使用して実際にハックすることができます。これにより、コンテンツを他の関数に動的に挿入できます。詳細については、彼のDebugging tutorialを参照してください。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 タイプが強制されます。sendmailR
text/plain
text/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)))
、コードを挿入する場所を使用して観察することから得られます。
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)