Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send HTML message using gmailr

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
like image 822
Jack Case Avatar asked May 09 '15 20:05

Jack Case


People also ask

Can I send HTML email through Gmail?

You can send HTML email in Gmail. But, in the past you would have to design the email, get someone to build it, and then manipulate Gmail code to add it. Now you can create and send HTML email in Gmail by dragging, dropping and a click.

Can you send HTML files via email?

The most important thing to know about HTML email is that you can't just attach an HTML file and a bunch of images to a message, then click send. Most of the time, your recipient's email application will break all the paths to your image files by moving your images into temporary folders on the recipient's hard drive.


1 Answers

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.

like image 137
Jan Avatar answered Oct 15 '22 13:10

Jan