You need to use the Content-Type
header to specify the correct MIME type. However, apparently the author decided to hardcode this, and the headers
parameter that the sendmail
function provides will not work with Content-Type
. You can really hack around it using the trace
function, which lets you dynamically insert content into other functions. For more on this, see his Debugging tutorial.
In the internal function sendmailR:::.write_mail
the author has the following code:
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")
}
We are going to replace the function writeLines
temporarily within a sendmailR
internal function to change text/plain
(non-HTML email) to text/html
. This will force the correct MIME type.
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>')
The magic number 9 comes from using print(as.list(body(sendmailR:::.write_mail)))
and eyeballing where to inject the code.
