emayili is a package for sending emails from R. The design goals are:
The package name is an adaption of the Zulu word for email, imeyili.
Get the stable version from CRAN.
Or grab it directly from GitHub.
First create a message object.
The message has class
Add addresses for the sender and recipient.
Add a subject.
email <- email %>% subject("This is a plain text message!")
Add a text body. You can use
html() to add an HTML body.
email <- email %>% text("Hello!")
Add an attachment.
email <- email %>% attachment("image.jpg")
You can also create the message in a single command:
email <- envelope( to = "email@example.com", from = "firstname.lastname@example.org", subject = "This is a plain text message!", text = "Hello!" )
Simply printing a message displays the header information.
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.
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 the image.
img_base64 <- base64enc::base64encode("image.jpg")
Then create the HTML message body.
html_body <- sprintf('<html><body><img src="data:image/jpeg;base64,%s"></body></html>', img_base64)
And finally add it to the email.
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.
html_body <- '<html><body><img src="cid:image"></body></html>'
Then attach the image and specify the
Create a SMTP server object and send the message.
smtp <- server(host = "smtp.gmail.com", port = 465, username = "email@example.com", password = "bd40ef6d4a9413de9c1318a65cbae5d7") smtp(email, verbose = TRUE)
To see the guts of the message as passed to the SMTP server:
print(email, details = TRUE)
If you’re trying to send email with a host that uses the STARTTLS security protocol (like Google Mail, 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:
The following (draft) standards documents relate to emails: