-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathgmailr.Rmd
More file actions
207 lines (159 loc) · 5.74 KB
/
gmailr.Rmd
File metadata and controls
207 lines (159 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---
title: "gmailr"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{gmailr}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r}
#| include: false
can_decrypt <- gargle::secret_has_key("GMAILR_KEY")
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
error = TRUE,
purl = can_decrypt,
eval = can_decrypt
)
```
```{r}
#| eval: !expr '!can_decrypt'
#| echo: false
#| comment: NA
message("No token available. Code chunks will not be evaluated.")
```
```{r}
#| label: auth
#| include: false
gmailr:::gm_auth_testing()
```
```{r}
#| label: setup
#| message: false
library(gmailr)
```
## OAuth client
In order to use gmailr, you **must** provide your own OAuth client.
The article [Set up an OAuth client](https://gmailr.r-lib.org/dev/articles/oauth-client.html) explains how to obtain an OAuth client and how to configure it for gmailr's use.
The help topics for `?gm_auth_configure` and `?gm_default_oauth_client` will also be useful.
Unless you have reason to do otherwise, my recommendation is to place the JSON file for your OAuth client in the default location, so that it is discovered automatically.
The default location is returned by `rappdirs::user_data_dir("gmailr")`.
Alternatively, you can also make autodiscovery work by exposing the client's JSON filepath as the `GMAILR_OAUTH_CLIENT` environment variable.
If your client is configured for auto-discovery, your gmailr code should "just work", without any explicit configuration around the client:
```r
library(gmailr)
# your gmailr code ...
```
Otherwise, your code must always include a call to `gm_auth_configure()`, probably right after you attach gmailr:
```r
library(gmailr)
gm_auth_configure("path/to/your/oauth_client.json")
# your gmailr code ...
```
## Auth
Configuring an OAuth client is step 1 of 2 for getting ready to use gmailr.
Step 2 is to complete the so-called "OAuth dance".
For most folks and especially in early usage, you can just allow the OAuth dance to be triggered automatically upon first need.
You are taken to a web browser, where you must select or login as the Google user you want to use (authenticate yourself) and give your OAuth client permission to do Gmail stuff on your behalf (authorize).
The OAuth dance does not need to be repeated in subsequent sessions, because, by default, your credentials are cached locally and can be refreshed.
If, however, you want to take more control over auth, you can call `gm_auth()` explicitly and proactively.
The arguments that are most useful in practice are:
* `email`: Use this to specify the target user. This can be useful if you have
a cached token and you want code to run without an attempt to get interactive
confirmation that it's OK to use it.
* `scopes`: Following the principle of least privilege, if you have no intention
of sending email, it could be wise to deliberately use a token with a
"read only" scope.
* `token`: In a deployed setting, it can be useful to directly provide a stored
token to gmailr. This use case is documented in `vignettes("deploy-a-token")`.
Here's how a script might begin if the OAuth client can't be auto-discovered and the user needs to request non-default behaviour from `gm_auth()`:
```{r}
#| eval: false
library(gmailr)
gm_auth_configure("path/to/your/oauth_client.json")
gm_auth(
"target.user@example.com",
scopes = "gmail.readonly",
cache = "some/nice/directory/"
)
```
`gm_profile()` is a handy function to confirm that gmailr is using the intended Google identity.
```{r}
gm_profile()
```
## Compose and send an email
Create a new email with `gm_mime()` and then build it up from parts, using helper functions like `gm_to()` and `gm_subject()`.
```{r}
#| eval: false
test_email <-
gm_mime() |>
gm_to("PUT_A_VALID_EMAIL_ADDRESS_THAT_YOU_CAN_CHECK_HERE") |>
gm_subject("this is just a gmailr test") |>
gm_text_body("Can you hear me now?")
```
```{r}
#| include: false
test_email <-
gm_mime() |>
gm_to("gargle-testuser@posit.co") |>
gm_subject("this is just a gmailr test") |>
gm_text_body("Can you hear me now?")
```
You can even add a file attachment with `gm_attach_file()`:
```{r}
tmp <- tempfile("mtcars-", fileext = ".csv")
write.csv(mtcars, tmp)
test_email <- gm_attach_file(test_email, tmp)
```
When developing a message, it's a good idea to first create a draft with `gm_create_draft()`
Then you can visit your Gmail drafts in the browser and confirm that the message content and formatting is as you intend.
```{r}
d <- gm_create_draft(test_email)
```
If you're happy, you can either send that draft from the web UI or with `gm_send_draft()`:
```{r}
gm_send_draft(d)
```
Or you can send the original MIME message with `gm_send_message()`:
```{r}
#| eval: false
gm_send_message(test_email)
```
## Read email
You can retrieve email threads with `gm_threads()`:
```{r}
my_threads <- gm_threads(num_results = 10)
```
You can retrieve a specific thread with `gm_thread()`:
```{r}
# retrieve the latest thread by retrieving the first ID
latest_thread <- gm_thread(gm_id(my_threads)[[1]])
```
The messages in `latest_thread` are stored as a list.
You can then isolate a specific message and access its parts.
```{r}
my_msg <- latest_thread$messages[[1]]
gm_date(my_msg)
gm_subject(my_msg)
gm_body(my_msg)
```
If a message has attachments, you can download them all locally with `gm_save_attachments()`:
```{r}
tmp2 <- tempfile("attachments-")
dir.create(tmp2)
gm_save_attachments(my_msg, path = tmp2)
# let's take a peek
tmp2 |>
list.files(full.names = TRUE, pattern = "[.]csv$") |>
read.csv() |>
head()
```
```{r}
#| include: false
unlink(tmp)
unlink(tmp2, recursive = TRUE)
```
## Quota
The Gmail API is free to use for modest levels of activity.
To learn more about Gmail API quotas see <https://developers.google.com/gmail/api/reference/quota>.