-
Notifications
You must be signed in to change notification settings - Fork 27
/
README.Rmd
536 lines (397 loc) · 14.4 KB
/
README.Rmd
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include=FALSE}
knitr::opts_chunk$set(comment = NA)
```
# emayili <img src="man/figures/emayili-hex.png" align="right" alt="" width="120" />
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/emayili)](https://cran.r-project.org/package=emayili)
![GitHub Actions build status](https://github.com/datawookie/emayili/actions/workflows/build.yaml/badge.svg)
[![Codecov test coverage](https://img.shields.io/codecov/c/github/datawookie/emayili.svg)](https://app.codecov.io/gh/datawookie/emayili)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-green.svg)](https://lifecycle.r-lib.org/articles/stages.html)
<!-- badges: end -->
`{emayili}` is a package for sending emails from R. The design goals are:
- works on all manner of SMTP servers and
- has minimal dependencies (or dependencies which are easily satisfied).
The package name is an adaption of the Zulu word for email, _imeyili_.
The documentation for `{emayili}` can be found [here](https://datawookie.github.io/emayili/).
Not that I pretend to have many stars on this project, but these plots are quite fun.
[![Star History Chart](https://api.star-history.com/svg?repos=datawookie/emayili&type=Date)](https://star-history.com/datawookie/emayili&Date)
## Installation
Get the stable version from [CRAN](https://CRAN.R-project.org/package=emayili).
```{r eval=FALSE}
install.packages("emayili")
```
Or grab it directly from [GitHub](https://github.com/datawookie/emayili).
```{r eval=FALSE}
# Install from the master branch.
remotes::install_github("datawookie/emayili")
# Install from the development branch.
remotes::install_github("datawookie/emayili", ref = "dev")
```
## Usage
Load the library.
```{r message=FALSE}
library(emayili)
packageVersion("emayili")
```
Create a message object.
```{r}
email <- envelope()
```
### Creating a Message
The message has class `envelope`.
```{r}
class(email)
```
Add addresses for the sender and recipient.
```{r}
email <- email %>%
from("alice@yahoo.com") %>%
to("bob@google.com") %>%
cc("craig@google.com")
```
There are also `bcc()` and `reply()` functions for setting the `Bcc` and `Reply-To` fields.
You can supply multiple addresses in a variety of formats:
- as a single comma-separated string
- as separate strings; or
- as a vector of strings.
```{r multiple-addresses, eval = FALSE}
envelope() %>% to("bob@google.com, craig@google.com, erin@gmail.com")
envelope() %>% to("bob@google.com", "craig@google.com", "erin@gmail.com")
envelope() %>% to(c("bob@google.com", "craig@google.com", "erin@gmail.com"))
```
Add a subject.
```{r}
email <- email %>% subject("This is a plain text message!")
```
Add a text body.
```{r eval=FALSE}
email <- email %>% text("Hello!")
```
You can use `html()` to add an HTML body. It accepts either a vector of characters or a `tagList()` from `{htmltools}`.
```{r eval=FALSE}
library(htmltools)
email <- email %>% html(
tagList(
h2("Hello"),
p("World!")
)
)
```
Add an attachment.
```{r eval=FALSE}
email <- email %>% attachment("image.jpg")
```
You can also create the message in a single command:
```{r eval=FALSE}
email <- envelope(
to = "bob@google.com",
from = "alice@yahoo.com",
subject = "This is a plain text message!",
text = "Hello!"
)
```
Simply printing a message displays the header information.
```{r}
email
```
You can identify emails which have been sent using `{emayili}` by the presence of an `X-Mailer` header which includes both the package name and version.
If you want to see the complete MIME object, just convert to a string.
```{r include = FALSE}
as.character(email)
```
You can also call the `print()` method and specify `details = TRUE`.
```{r include = FALSE}
print(email, details = TRUE)
```
### Options
You can set the `envelope.details` option to assert that the details should always be printed.
```{r}
# Always print envelope details.
#
options(envelope.details = TRUE)
```
By default the results returned by most of the methods are invisible. You can make them visible via the `envelope.invisible` (default: `TRUE`).
```{r}
# Always show envelope.
#
options(envelope.invisible = FALSE)
```
### Interpolating Text
You can use `{glue}` syntax to interpolate content into the body of a message.
```{r}
name <- "Alice"
envelope() %>%
text("Hello {{name}}!")
```
### Rendering Markdown
You can render Markdown straight into a message.
Use either plain Markdown.
```{r}
envelope() %>%
# Render plain Markdown from a character vector.
render(
"Check out [`{emayili}`](https://cran.r-project.org/package=emayili)."
)
```
Or R Markdown.
```{r eval = FALSE}
envelope() %>%
# Render R Markdown from a file.
render("message.Rmd")
```
In both cases the function will accept either a file path or a character vector containing Markdown text.
<img src="man/figures/screenshot-email-rendered.png" style="filter: drop-shadow(5px 5px 5px black); margin-bottom: 5px;">
Interpolation also works with `render()`.
### Rendered CSS
When you render an R Markdown document the resulting HTML includes CSS from three sources:
- [Bootstrap](https://getbootstrap.com/)
- [highlightjs](https://highlightjs.org/) and
- `{rmarkdown}`.
You can control which of these propagate to the message using the `include_css` parameter which, by default, is set to `c("rmd", "bootstrap", "highlight")`.
🚨 _Note:_ Gmail doesn't like the Bootstrap CSS. If you want your styling to work on Gmail you should set `include_css = c("rmd", "highlight")`.
### Extra CSS
You can insert extra CSS into your rendered messages.
```{r eval = FALSE}
envelope() %>%
render("message.Rmd", css_files = "extra.css")
```
If you are having trouble getting this to work with Gmail then it might be worthwhile taking a look at their [CSS support](https://developers.google.com/gmail/design/css).
### Adding an Inline Image
Adding an inline image to an HTML message is possible. There are two ways to achieve this.
_1. Base64 Encoding_
First you'll need to [Base64 encode](https://en.wikipedia.org/wiki/Base64) the image.
```{r eval=FALSE}
img_base64 <- base64enc::base64encode("image.jpg")
```
Then create the HTML message body.
```{r eval=FALSE}
html_body <- sprintf('<html><body><img src="data:image/jpeg;base64,%s"></body></html>', img_base64)
```
And finally add it to the email.
```{r eval=FALSE}
email <- envelope() %>% html(html_body)
```
_Note:_ It's important that you specify the appropriate media type (`image/jpeg` for JPEG and `image/png` for PNG).
_2. Using a CID_
Unfortunately some mail clients (like Gmail) will not display Base64 encoded images. In this case using a CID is a working alternative.
First create the message body which references an image by CID.
```{r eval=FALSE}
html_body <- '<html><body><img src="cid:image"></body></html>'
```
Then attach the image and specify the `cid` argument.
```{r eval=FALSE}
email <- envelope() %>%
html(html_body) %>%
attachment(path = "image.jpg", cid = "image")
```
### Create a Server Object
Create a SMTP server object.
```{r eval=FALSE}
smtp <- server(
host = "smtp.gmail.com",
port = 465,
username = "bob@gmail.com",
password = "bd40ef6d4a9413de9c1318a65cbae5d7"
)
```
It's bad practice to include credentials in a script. A better approach would be to keep the credentials in your `.Renviron` file.
```
GMAIL_USERNAME="bob@gmail.com"
GMAIL_PASSWORD="bd40ef6d4a9413de9c1318a65cbae5d7"
```
You can then pull these variables into R using `Sys.getenv()` and then create the server object.
```{r eval=FALSE}
smtp <- server(
host = "smtp.gmail.com",
port = 465,
username = Sys.getenv("GMAIL_USERNAME"),
password = Sys.getenv("GMAIL_PASSWORD")
)
```
If you're trying to send email with a host that uses the STARTTLS security protocol (like Gmail, Yahoo! or AOL), then it will most probably be blocked due to insufficient security. In order to circumvent this, you can grant access to less secure apps. See the links below for specifics:
* [Gmail](https://myaccount.google.com/security)
* [Yahoo!](https://login.yahoo.com/account/security) and
* [AOL](https://login.aol.com/account/security).
Gmail has recently changed their authentication procedure. If you are having trouble connecting to the Gmail SMTP server then take a look at [this](https://datawookie.dev/blog/2022/03/updated-gmail-authentication/).
### Sending a Message
Send the message.
```{r eval=FALSE}
smtp(email, verbose = TRUE)
```
To see the guts of the message as passed to the SMTP server:
```{r eval=FALSE}
print(email, details = TRUE)
```
### Encryption
Both transport-level encryption and end-to-end [email encryption](https://en.wikipedia.org/wiki/Email_encryption) are supported. An SMTP connection can be initiated on port 465 (SMTPS, see [RFC 8314](https://www.rfc-editor.org/rfc/rfc8314)) or 587 (SMTP-MSA, see [RFC 6409](https://www.rfc-editor.org/rfc/rfc6409)), enabling transport-level encryption. Public-key cryptography (via GnuPG) can be used to sign and/or encrypt message contents for end-to-end encryption.
## Standards Documents
The following (draft) standards documents relate to emails:
- [RFC 2822](https://www.rfc-editor.org/rfc/rfc2822) — Internet Message Format
- [RFC 5321](https://www.rfc-editor.org/rfc/rfc5321) — Simple Mail Transfer Protocol
- [RFC 5322](https://www.rfc-editor.org/rfc/rfc5322) — Internet Message Format
- [RFC 6854](https://www.rfc-editor.org/rfc/rfc6854) — an update to RFC 5322.
## Similar Packages
There is a selection of other R packages which also send emails:
- [blastula](https://cran.r-project.org/package=blastula)
- [blatr](https://cran.r-project.org/package=blatr) (Windows)
- [gmailr](https://cran.r-project.org/package=gmailr)
- [mail](https://cran.r-project.org/package=mail)
- [mailR](https://cran.r-project.org/package=mailR)
- [sendmailR](https://cran.r-project.org/package=sendmailR)
- [ponyexpress](https://github.com/ropensci-archive/ponyexpress)
## Blog Posts
<table>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/10/emayili-support-for-gmail-sendgrid-mailgun/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/10/emayili-support-for-gmail-sendgrid-mailgun/">Support for Gmail, SendGrid & Mailgun</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/10/emayili-message-precedence/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/10/emayili-message-precedence/">Message Precedence</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/10/emayili-message-integrity/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/10/emayili-message-integrity/">Message Integrity</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/09/emayili-right-to-left/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/09/emayili-right-to-left/">Right-to-Left</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/09/emayili-styling-figures/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/09/emayili-styling-figures/">Styling Figures</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/09/emayili-managing-css/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/09/emayili-managing-css/">Managing CSS</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/09/emayili-r-markdown-parameters/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/09/emayili-r-markdown-parameters/">R Markdown Parameters</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/09/emayili-rendering-r-markdown/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/09/emayili-rendering-r-markdown/">Rendering R Markdown</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/09/emayili-rendering-plain-markdown/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/09/emayili-rendering-plain-markdown">Rendering Plain Markdown</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/09/emayili-interpolating-message-content/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/09/emayili-interpolating-message-content">Interpolating Message Content</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2021/08/emayili-rudimentary-email-address-validation/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2021/08/emayili-rudimentary-email-address-validation">Rudimentary Email Address Validation</a>
</td>
</tr>
<tr>
<td>
<img src="https://datawookie.dev/blog/2022/03/updated-gmail-authentication/featured.jpg" width="100px">
</td>
<td>
<a href="https://datawookie.dev/blog/2022/03/updated-gmail-authentication/">Updated Gmail Authentication</a>
</td>
</tr>
</table>
## Developer Notes
### Code Coverage
You can find the test coverage report at [Codecov](https://app.codecov.io/gh/datawookie/emayili). For development purposes it's more convenient to use the [`{covr}`](https://cran.r-project.org/package=covr) package.
Generate a coverage report.
```{r eval = FALSE}
library(covr)
# Tests that are skipped on CRAN should still be included in coverage report.
#
Sys.setenv(NOT_CRAN = "true")
report()
```
Calculate test coverage.
```{r eval = FALSE}
coverage <- package_coverage()
```
Coverage statistics as a data frame.
```{r eval = FALSE}
as.data.frame(coverage)
```
Show lines without coverage.
```{r eval = FALSE}
zero_coverage(coverage)
```
### Checks
Check spelling.
```{r eval = FALSE}
spelling::spell_check_package()
```
Quick local checks.
```{r eval = FALSE}
devtools::check()
```
Remote checks (take longer but more thorough).
```{r eval = FALSE}
devtools::check_win_devel()
# Check for a specific platform.
#
rhub::check(platform = "debian-gcc-devel")
rhub::check_on_windows(check_args = "--force-multiarch")
rhub::check_on_solaris()
# Check on a bunch of platforms.
#
rhub::check_for_cran()
# Check on important platforms.
#
rhub::check_for_cran(platforms = c(
"debian-gcc-release",
"ubuntu-gcc-release",
"macos-m1-bigsur-release",
"windows-x86_64-release",
NULL
))
```