{emayili:0.4.6} Attachments with UTF-8 Filenames & Setting Sender

[This article was first published on R | datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.
# From GitHub.

Load the package.


Check that you have the current version.

[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 thanks to a contribution from Philipp Hallmeier.

# Create email and add attachment.
email <- envelope(
  from = "[email protected]",
  to = "[email protected]",
  subject = "Wenceslao Moreno"
) %>%

# Send it!

Heading over to SMTP Bucket I can inspect the structure of the received email.

Date:         Sat, 06 Mar 2021 03:57:22 GMT
To:           [email protected]
From:         [email protected]
Subject:      Wenceslao Moreno
X-Mailer:     {emayili}-0.4.6
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="3322251652a271341525323233"

Content-Type: image/jpeg
Content-Disposition: attachment; filename*=utf-8''se%C3%B1or-wences.jpg
Content-Transfer-Encoding: base64



We can confirm that the file name has been encoded correctly too. You’ll find the file name on the Content-Disposition line above.

[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 = "[email protected]",
  subject = "Hi"
) %>%
  text("Hi Bob, it's Alice. I asked Craig to send this email.") %>%
  # Craig is sending the message...
  sender("[email protected]") %>%
  # ... but it's actually from Alice.
  from("[email protected]")

# Send it!

This is what the resulting message looks like:

Date:         Sat, 06 Mar 2021 04:13:50 GMT
To:           [email protected]
Subject:      Hi
Sender:       [email protected]
From:         [email protected]
X-Mailer:     {emayili}-0.4.6
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="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.


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.

To leave a comment for the author, please follow the link and comment on their blog: R | datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)