1

I'm trying to send email messages using the sendmailR package. Referencing the example located here.

I am able to send emails just fine but when they appear in my mail client (Outlook 2013) the raw HTML code is displayed. Any ideas how to correct this?

Example email received. https://dl.dropboxusercontent.com/u/3734701/Untitled_Clipping_032614_022810_PM.jpg

Community
  • 1
  • 1

2 Answers2

1

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.

example email

Robert Krzyzanowski
  • 9,294
  • 28
  • 24
  • Thank you for the prompt reply. I am having trouble reproducing your example though. I run the function you created (send_html) and see some issues. I use my Gmail email address in the script and get the following error: Error in socketConnection(host = server, port = port, blocking = TRUE) : cannot open the connection In addition: Warning message: In socketConnection(host = server, port = port, blocking = TRUE) : ASPMX.L.GOOGLE.COM:25 cannot be opened – user1607359 Mar 26 '14 at 22:46
  • Could you provide me some more info, like what errors you see? I tried it and it works for me. – Robert Krzyzanowski Mar 26 '14 at 22:48
  • That is an issue with the setup on your local SMTP server. And just to make sure, did you replace the first and second arguments with the correct "to" and "from" address? – Robert Krzyzanowski Mar 26 '14 at 22:49
  • I did update both addresses to my gmail user account/email. I get the following error: Error in socketConnection(host = server, port = port, blocking = TRUE) : cannot open the connection In addition: Warning message: In socketConnection(host = server, port = port, blocking = TRUE) : localhost:25 cannot be opened – user1607359 Mar 26 '14 at 22:53
  • You need to have an SMTP server running locally. For example, look at [this guide](http://www.developerfiles.com/how-to-send-smtp-mails-with-postfix-mac-os-x-10-8/) if you are on a Mac. – Robert Krzyzanowski Mar 26 '14 at 22:55
  • If I would like to use a company network SMTP server and email address...how would I specify that in your example? – user1607359 Mar 26 '14 at 22:57
  • The package's documentation says you can do `sendmail_options(smtpServer = 'your.server.com', smtpPort = 25)` – Robert Krzyzanowski Mar 26 '14 at 22:59
0

You could try the development version of the mailR package available on github https://github.com/rpremraj/mailR

Using mailR, you could send an email in HTML format as below:

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)
Rahul Premraj
  • 1,595
  • 14
  • 13