r-lib / gmailr

Access the Gmail RESTful API from R.
https://gmailr.r-lib.org
Other
229 stars 56 forks source link

HTML email using GMAILR - body is blank #57

Closed lwasser closed 5 years ago

lwasser commented 8 years ago

hi @jimhester - this is in response to your twitter suggestion. I am having a hard time getting body_html to properly send email in r using GMAILR. I have tried numerous different approaches and the result is always the same - an email with no body content when i use html_body.

I presume that I am doing something wrong however i can not figure out what that might be. I just installed GMAILR the other day. I am running R 3.2.3.

suppressPackageStartupMessages(library(gmailr))
# custom json file
gmail_auth("data-institute-email.json", scope = 'compose')

## the code below sends a nice text formatted email
## the $body slot is populated correctly in test_email
test_email <- mime(
  To = "leahwasser@gmail.com",
  From = "neondataskills@gmail.com",
  Subject = "this is just a gmailr test",
  body = "Can you hear me now?")

The next chunk returns a blank email and the $body slot in test_email is also blank

# this returns a BLANK email
test_email <- mime(
  To = "leahwasser@gmail.com",
  From = "neondataskills@gmail.com",
  Subject = "this is just a gmailr test",
  html_body = "Can <b>you hear</b> me now?", type = "text/html")

# this also sends a blank email
test_email <- mime(
  To = "leahwasser@gmail.com",
  From = "neondataskills@gmail.com",
  Subject = "this is just a gmailr test",
  html_body = "<html><body>Can <b>you hear</b> me now?</body></html>")

to send the email (this works fine)

ret_val <- send_message(test_email)

Any suggestions that you have area greatly appreciated! Leah

jimhester commented 8 years ago

Try the following which works for me

test_email <- mime(
  To = "leahwasser@gmail.com",
  From = "neondataskills@gmail.com",
  Subject = "this is just a gmailr test")
test_email <- html_body(test_email, "Can <b>you</b> hear me now?")
create_draft(test_email)
lwasser commented 8 years ago

Thank you @jimhester for the feedback. create_draft returned the following error:

> create_draft(test_email)
Error in UseMethod("from") : 
  no applicable method for 'from' applied to an object of class "gmail_draft"

My code:

# From Jim Hester
test_email <- mime(
  To = "leahwasser@gmail.com",
  From = "neondataskills@gmail.com",
  Subject = "this is just a gmailr test")
test_email <- html_body(test_email, "Can <b>you</b> hear me now?")

create_draft(test_email)
ret_val <- send_message(test_email)

What did I miss?

jimhester commented 8 years ago

Try installing the latest version from GitHub.

# install.packages("devtools")
install_github("jimhester/gmailr")
lwasser commented 8 years ago

OK -- progress! I was able to create a draft once successfully. However, i have not been able to send_message successfully and now after one draft i am getting the error:

Error in loadNamespace(name) : there is no package called ‘crayon’

both when i send_message and create_draft

I triied to install the package crayon but when i do i get another odd output from both send and create draft as follows:

 create_draft(test_email)
Id: r-5335522915222148839
To: 
From: 
Date: 
Subject: 

> send_message(test_email)
Id: 153d3bae42b75989
To: 
From: 
Date: 
Subject: 
lwasser commented 8 years ago

Hi @jimhester

One other note. When I install from github and try to authenticate, it says it can't find OAuth2 in the browser.

So this line

gmail_auth("data-institute-email.json", scope = 'compose')

Fails. When I install from cran I'm able to authenticate with no problem. Leah

jimhester commented 8 years ago

Try installing from GitHub again, I have fixed a few things which should make this work better for you.

You need to use the secret_file parameter to read the secret from the json. Alternatively the github version packages a secret file so you can use that without needing to supply a file.

gmail_auth(secret_file = "data-institute-email.json", scope = 'compose')

I am currently getting a Bounce error when trying to send the email, but the message still seems to go through... Creating a draft with create_draft() then sending that does work however, I am not sure what is going wrong with directly sending...

jimhester commented 8 years ago

Ok https://github.com/jimhester/gmailr/commit/d5db6a718c3d4fa7e30543732d1ac86170f5fc33 now sends without Bouncing, so try updating again and let me know if it doesn't work.

lwasser commented 8 years ago

Thank you. Please tell me if am doing this wrong. I got an error on the install. So I uninstalled gmailr and made sure it was gone in my library dir on my mac. I then installed from github and got the following message / error (at the very bottom):

Downloading` GitHub repo jimhester/gmailr@master
from URL https://api.github.com/repos/jimhester/gmailr/zipball/master
Installing gmailr
Downloading GitHub repo gaborcsardi/argufy@173245a7e
from URL https://api.github.com/repos/gaborcsardi/argufy/zipball/173245a7e
Installing argufy
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file  \
  --no-environ --no-save --no-restore CMD INSTALL  \
  '/private/var/folders/x9/0p6djscn2890hpkvmlz4nx0nn42rsp/T/Rtmpatr7rD/devtools6021099596c/gaborcsardi-argufy-173245a'  \
  --library='/Library/Frameworks/R.framework/Versions/3.2/Resources/library'  \
  --install-tests 

* installing *source* package ‘argufy’ ...
** R
** inst
** tests
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (argufy)
Skipping 1 unavailable package: argufy
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file  \
  --no-environ --no-save --no-restore CMD INSTALL  \
  '/private/var/folders/x9/0p6djscn2890hpkvmlz4nx0nn42rsp/T/Rtmpatr7rD/devtools6028b6cef9/jimhester-gmailr-d5db6a7'  \
  --library='/Library/Frameworks/R.framework/Versions/3.2/Resources/library'  \
  --install-tests 

* installing *source* package ‘gmailr’ ...
** R
** tests
** preparing package for lazy loading
** help
Error : /private/var/folders/x9/0p6djscn2890hpkvmlz4nx0nn42rsp/T/Rtmpatr7rD/devtools6028b6cef9/jimhester-gmailr-d5db6a7/man/id.gmail_messages.Rd: Sections \title, and \name must exist and be unique in Rd files
ERROR: installing Rd objects failed for package ‘gmailr’
* removing ‘/Library/Frameworks/R.framework/Versions/3.2/Resources/library/gmailr’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/3.2/Resources/library/gmailr’
Error: Command failed (1)
jimhester commented 8 years ago

Sorry that was an inadvertent error, sorry for the confusion. It should now be fixed.

lwasser commented 8 years ago

Ok... it installed perfectly but i'm still getting the following error (it authenticates and then throws an error when i try to create draft or send_email.

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error: .onLoad failed in loadNamespace() for 'crayon', details:
  call: get0(oNam, envir = ns)
  error: lazy-load database '/Library/Frameworks/R.framework/Versions/3.2/Resources/library/memoise/R/memoise.rdb' is corrupt
In addition: Warning message:
In get0(oNam, envir = ns) : internal error -3 in R_decompress1
> send_message(test_email)
Error: .onLoad failed in loadNamespace() for 'crayon', details:
  call: get0(oNam, envir = ns)
  error: lazy-load database '/Library/Frameworks/R.framework/Versions/3.2/Resources/library/memoise/R/memoise.rdb' is corrupt
In addition: Warning messages:
1: In get0(oNam, envir = ns) : restarting interrupted promise evaluation
2: In get0(oNam, envir = ns) : internal error -3 in R_decompress1

the path there is where my packages are stored so is this error on my end? Thank you!

jimhester commented 8 years ago

Something strange is going on. Does this happen if you start an entirely new session? You may want to try reinstalling the crayon package from CRAN as well.

lwasser commented 8 years ago

ok! Progress. Iyou are right - something was weird with my session. I restarted. Now i'm not able to use gmail_auth after i install from github. i get the following information in the browser if this helps:

scope=https://www.googleapis.com/auth/gmail.compose
response_type=code
redirect_uri=http://localhost:1410
state=H8JTSXEbMZ
client_id=data-institute-email.json

However, if i install from cran. run gmail_auth AND then install from github, i can send an html email successfully! it does ask me to authenticate a few other items (create draft, etc) when i use create_draft but then it works.

Is there something there that is different in gmail_auth in the git version compared to the cran version?

Thank you so much for working through this with me!

abhishek1608 commented 6 years ago

@jimhester is there any way we can send whole html file by placing the directory?