17
votes

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("[email protected]") %>%
 from("[email protected]") %>%
 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("[email protected]") %>%
 from("[email protected]") %>%
 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("[email protected]") %>%
 from("[email protected]") %>%
  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("[email protected]") %>%
 from("[email protected]") %>%
 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
1
it appears that this is related to the issue referenced here: github.com/jimhester/gmailr/issues/9Jack Case
While this does not answer your gmailr related issue, I recommend you to give mailR a shot that easily supports sending HTML formatted emails (github.com/rpremraj/mailR).Rahul Premraj
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
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
I use sendmailR successfully for that purpose, cf stackoverflow.com/questions/19844762/…Karl Forner

1 Answers

2
votes

Well - this is what I tried:

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

test_email <- mime() %>%
 to("[email protected]") %>%
 from("[email protected]") %>%
 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.