{emayili} Sending Email from Shiny

The {emayili} package for sending emails from R works well within a Shiny app. You just need to set it up right.

I’ve added a simple demonstration script to the latest version of {emayili}. You’ll find it in the inst/shiny/send-message directory.

Setup

Probably the easiest way to set this up will be to create a copy of the app in a new folder. The app gets the details of the SMTP server and the account credentials from environment variables. So you’ll need to edit an .Renviron file in the same directory as the app and add values for those variables. Something like this:

SMTP_USERNAME="bob@gmail.com"
SMTP_PASSWORD="#3&ZX9@dvP$X&4s*"
SMTP_SERVER="smtp.gmail.com"
SMTP_PORT=587

Now deploy the app to shinyapps.io.

rsconnect::deployApp()

When the deploy is complete (and assuming that it’s successful) the app should launch in your browser.

Demo

This is what the app should look like.

Demo Shiny application in browser, showing button to send a message.

It displays:

  • the IP address of the server on which it is running
  • the DNS name and port of the configured SMTP server (if these are absent then it means that the app is not picking up your environment variables).

Pressing the button will dispatch an email from the address specified in the SMTP_USERNAME environment variable to the same address.

Upon receipt the email should look something like this.

Email sent from Shiny application.

The IP address in the email will agree with that displayed in the app, confirming that it was indeed sent from the app! 🚀