17

I want to be able to use the gmailR package to send R-generated HTML reports via email inline(not as attachments). I'm unable to even send a basic HTML email using gmailr. I've attempted the following unsuccessfully and need some help:

library(gmailr)
gmail_auth("oauth.token.json", scope = "compose")

test_email <- mime() %>%
 to("you@gmail.com") %>%
 from("me@gmail.com") %>%
 subject("This is a subject") 
test_email$body <- "I wish <b>this</b> was bold"
send_message(test_email)
RESULT: Message successfully sends, but body is plain text - not HTML


Attempt 2

test_email <- mime() %>%
 to("you@gmail.com") %>%
 from("me@gmail.com") %>%
 subject("This is a subject") %>%
 html_body("I wish <b>this</b> was bold")
test_email$body
RESULT: test_email$body is NULL


Attempt 3

test_email <- mime() %>%
 to("you@gmail.com") %>%
 from("me@gmail.com") %>%
  subject("This is a subject") 
test_email$body <- html_body("I wish <b>this</b> was bold")
RESULT: Error in mime$parts : $ operator is invalid for atomic vectors


Attempt 4

test_email <- mime() %>%
 to("you@gmail.com") %>%
 from("me@gmail.com") %>%
 subject("This is a subject") 
test_email$parts <- c(html_body("I wish <b>this</b> was bold"),text_body("plain"))
RESULT: Error in mime$parts : $ operator is invalid for atomic vectors
Pino
  • 7,468
  • 6
  • 50
  • 69
Jack Case
  • 329
  • 2
  • 13
  • 1
    it appears that this is related to the issue referenced here: https://github.com/jimhester/gmailr/issues/9 – Jack Case May 09 '15 at 21:21
  • While this does not answer your gmailr related issue, I recommend you to give mailR a shot that easily supports sending HTML formatted emails (https://github.com/rpremraj/mailR). – Rahul Premraj May 14 '15 at 19:01
  • Attempts 3 and 4 clearly are invalid. As the documentation states, the first arguments for text_body() and html_body() are a mime object, not a text string. The can be used to set the html or text body on a mime object, like so: text_body(test_email, "Plain text string") – WhiteViking Sep 06 '15 at 21:20
  • As for Attempt 2: the fact that test_email$body is NULL is probably normal. HTML email is sent as a multipart mime message, and that data is stored elsewhere in your test_email object. But what was the effect of attempt 2, did it actually send an email? send_message returns a status object, what does it return in your case for Attempt 2? – WhiteViking Sep 06 '15 at 21:25
  • I use sendmailR successfully for that purpose, cf https://stackoverflow.com/questions/19844762/how-to-send-html-email-using-r/21993248#21993248 – Karl Forner Oct 14 '15 at 15:40
  • @JackCase did you get t his to work? The code below actually doesn't work for me. I'm getting blank emails sent still. – Leah Wasser Apr 01 '16 at 16:14

1 Answers1

2

Well - this is what I tried:

library(gmailr)
gmail_auth('mysecret.json', scope = 'compose') 

test_email <- mime() %>%
 to("to@gmail.com") %>%
 from("from@gmail.com") %>%
 subject("This is a subject") %>%
 html_body("<html><body>I wish <b>this</b> was bold</body></html>")
send_message(test_email)

And voila (German gmail...) enter image description here

Seems like the trick was simply to put in real HTML - including <html> and <body> - to make gmail understand.

Jan
  • 13,738
  • 3
  • 30
  • 55