3MW (Automate E-mails With R & {blastula})

Guten Tag!

Many greetings from Munich, Germany. One part of automation is alerting people that long-running jobs finished. And a classical way to do that is to send people an email. But how do we do that automatically at the end of a long-running script?

Well, today I’m going to show you how you can do that with the {blastula} package. But first, let’s get to our regular announcements.

Data-driven Word documents

Remember how I’ve taught you how to create Word documents using the {officer} package? You do? Fantastic! Then you might be also happy to hear that I’ve put together a video version of these newsletters. You can check it out on YouTube:

3 more video lessons

The text cleaning part of my video course is making a lot of progress. This week, I’ve uploaded three more videos about

  • using the negate argument of many {stringr} functions,

  • removing unwanted text chunks before visualizing your data, and

  • trimming and padding strings for cleanup after manipulating them or for creating custom IDs.

You can find all of the information on the video course at

And now let’s get back to sending out e-mails:

Creating an email

Clearly, the first thing that you need to do is to create an email. For that, {blastula} gives you the compose_email() function. And for your convenience you can use Markdown notation using the md() function.

If you run this, you’ll see that the HTML email message is displayed in RStudio’s viewer window.

Adding data

You might also want to fill your email with data. Let’s create a fake dataset to do so:

Then, we can assemble an HTML table, e.g. with {gt} 

And then we can stick everything together with glue(). Just make sure to convert your {gt} table to raw html.

Get email credentials

Nice. We have a semi-good-lookin email. Let’s send that to someone. For that, we have to create “smtp credentials” first.

There are multiple ways to do that. You can

  • create a credentials file with create_smtp_creds_file(),

  • store credentials in your computer’s key-value vault with create_smtp_creds_key(), or

  • retrieve your passwort using environment variables with creds_envvar().

They all work pretty much the same but here we will go for the one that uses environment variables. This one is the easiest to implement for these kind of automations we’re running.

If you’re using gmail, outlook or office365 as your email provider, then it’s particularly simple. You’ll just have to provide creds_envvar() with

  • your user name (typically e-mail),

  • the name of the env-variable that stores your password, and

  • your provider.

In this case, I’m using gmail as a provider and I store my password in a variable called SMTP_PASSWORD.

Here, I’m also using an env-variable for my e-mail but that’s purely optional. Also, it is worth noting that for gmails you cannot use your regular password. You will have to create a designated app password.

Also, if you’re using some other email provider. Then, you’ll have to specify the host, port and use_ssl arguments instead of the provider argument. The information for these arguments is something that you have to retrieve from your provider.

Sending out the email

Finally, sending out the email is pretty straighforward. Just take your output of compose_email() and pass it to smtp_send().

Hoooray 🥳 We sent out e-mails using R. How cool is that!?! 😲 As always, if you have any questions, or just want to reach out, feel free to contact me by replying to this mail or finding me on Linkedin.

See you next week,
Albert 👋

Enjoyed this newsletter? Here are other ways I can help you:

Reply

or to participate.