Two new features in the {emayili}
(0.4.6) package for easily sending emails from R.
Package Setup
If you have not already installed the package, then grab it from CRAN or GitHub.
# From CRAN.
install.packages("emayili")
# From GitHub.
remotes::install_github("datawookie/emayili")
Load the package.
library(emayili)
Check that you have the current version.
packageVersion("emayili")
[1] ‘0.4.6’
Let’s quickly set up an SMTP server. We’ll use SMTP Bucket, which is incredibly convenient for testing.
SMTP_SERVER = "mail.smtpbucket.com"
SMTP_PORT = 8025
smtp <- server(host = SMTP_SERVER, port = SMTP_PORT)
UTF-8 Characters in Attachment Filenames
It’s now possible to attach files with names that include non-ASCII characters. Suppose I wanted to send this image (source) of Wenceslao Moreno.
The file name contains an accented character, ñ.
ACCENTED_ATTACHMENT <- "señor-wences.jpg"
Attaching this file now works.
# Create email and add attachment.
#
email <- envelope(
from = "alice@gmail.com",
to = "bob@gmail.com",
subject = "Wenceslao Moreno"
) %>%
attachment(ACCENTED_ATTACHMENT)
# Send it!
#
smtp(email)
Heading over to SMTP Bucket I can inspect the structure of the received email.
Date: Sat, 06 Mar 2021 03:57:22 GMT
To: bob@gmail.com
From: alice@gmail.com
Subject: Wenceslao Moreno
X-Mailer: {emayili}-0.4.6
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="3322251652a271341525323233"
--3322251652a271341525323233
Content-Type: image/jpeg
Content-Disposition: attachment; filename*=utf-8''se%C3%B1or-wences.jpg
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEASABIAAD//gBJRmlsZSBzb3VyY2U6IGh0dHA6Ly9jb21tb25zLndpa2lt
ZWRpYS5vcmcvd2lraS9GaWxlOlNlbm9yd2VuY2VzMTkzNS5qcGf/2wBDAAYEBQYFBAYGBQYHBwYI
ChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcH
BwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo
KCgoKCj/wAARCAAfABYDAREAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAwYIBwT/xAAnEAAC
AQMDAwQDAQAAAAAAAAABAgMEBREABiEHEkETIjFRYXGBI//EABkBAAMAAwAAAAAAAAAAAAAAAAED
BAACBf/EAB8RAAMAAgEFAQAAAAAAAAAAAAABAgMRMRIhIjJRYf/aAAwDAQACEQMRAD8AcNn9Wp91
Xmtjtu2Xa00sqxyzmoUSqrE4fsI5HGSAePzqXI1Hsxs7rg0yaijpux1LdgOAoLE4/raxVsDC0cMb
IWU96nGB7hj986OwJfpiVFum10dgo9wzJJLZqv03lmhYI8ZHsaNwCCxU+PIxjPxpDwt3rfcasiUj
Zunq/tmmsFwa1X6mW6JF/hHJE+e9hlcqRnyM/XnnjTJit8C9oN0wue57qk9Rf0iSFlBjf0DA5bgn
KZ5U54bj45Gj1TwmZomjo8Ke/wA0+z7vUxRUVVPFWweswCiaNgGQZ8vGWX9gaZmTnznk1jT7MoWs
6YbbqqyTcNyo3muQAlSMTkRx9vK4UcE+SOQST8+Y1ncx0bHuVVbGuz1TVjLJTuz9seOQR9ffzqeJ
pU9Gz0QdPSNR1MQmcdrYYMnPGfnBxrtkZb2zK2Su6f2WWqmaSpqLdC0kj5JZmjGSf6dcHL45Hr6W
T6o7tppNEMTwehiFQQXDe7JyBj9Dn86pxtU20wUz/9k=
--3322251652a271341525323233--
We can confirm that the file name has been encoded correctly too. You’ll find the file name on the Content-Disposition
line above.
URLdecode("se%C3%B1or-wences.jpg")
[1] "señor-wences.jpg"
Note: I actually sent a scaled version of the image!
Setting Sender Field
Sometimes the person who is sending the email is actually sending it on behalf of somebody else. Thanks to Robert Overman it’s now possible to do this using the sender()
function.
# Create email and specify the sender.
#
email <- envelope(
to = "bob@gmail.com",
subject = "Hi"
) %>%
text("Hi Bob, it's Alice. I asked Craig to send this email.") %>%
#
# Craig is sending the message...
#
sender("craig@gmail.com") %>%
#
# ... but it's actually from Alice.
#
from("alice@gmail.com")
# Send it!
#
smtp(email)
This is what the resulting message looks like:
Date: Sat, 06 Mar 2021 04:13:50 GMT
To: bob@gmail.com
Subject: Hi
Sender: craig@gmail.com
From: alice@gmail.com
X-Mailer: {emayili}-0.4.6
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="972f1d1f191e162e2320162ef182"
--972f1d1f191e162e2320162ef182
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Hi Bob, it's Alice. I asked Craig to send this email.
--972f1d1f191e162e2320162ef182--
In practice the Sender
field would most often be used when a mail agent (with its own SMTP credentials) is sending messages on behalf of users.