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("[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
6
  • 1
    it appears that this is related to the issue referenced here: github.com/jimhester/gmailr/issues/9 Commented May 9, 2015 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 (github.com/rpremraj/mailR). Commented May 14, 2015 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") Commented Sep 6, 2015 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? Commented Sep 6, 2015 at 21:25
  • I use sendmailR successfully for that purpose, cf stackoverflow.com/questions/19844762/… Commented Oct 14, 2015 at 15:40

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.