Creating & Sending Transactional Emails with Node.js and Mailgun — Part 1

Building and testing responsive HTML emails the pain free way

Jake Allen
The Readme
5 min readNov 6, 2016

--

We recently rolled out a new suite of transactional emails at Hintsy, and we discovered a few helpful workflows and tools we’d like to share. In Part 1, we’ll walk through a modern workflow for coding your transactional emails. In Part 2, we’ll cover how to send your shiny new emails with Node.js and the Mailgun API (with code samples, of course!).

Our final “Order Shipped” transactional email.

Email development is still a pain. It comes from the fact that emails still need to meet the outdated specs of a wide variety of email clients. That means:

  • Using old-school HTML tables
  • Inlining (almost) all of your CSS using style attributes
  • Avoiding any new web standards (like CSS3, for example)
  • A variety of devices, browsers, and email clients to test against

While some framework-like solutions exist for email development (link Ink or Litmus), there isn’t much out there for those who want to quickly spin up a new project without the technical overhead of learning (or paying for, in Litmus’ case) a large framework.

In short, emails are not something developers typically jump at the chance to build. So let’s see what we can do about that.

A Grunt Email Workflow

The first step towards saner email development is getting your dev environment in order. That’s where this Grunt workflow comes in (built by a developer who used to work at Mailgun). You can get up and running with it in 2 minutes, and start developing emails the same (modern) way you develop the rest of your projects.

Just npm install and you get some great tools right out of the box: responsive email starter templates, Handlebars and SCSS, and a live reload server that automatically compiles and previews your changes in the browser (including inlining your CSS, so you’re all ready to go).

The Handlebars and SCSS are particularly useful in transactional emails, as you’ll want to templatize components (like buttons and footers, for example) and reuse them across all of your emails. So a button would end up looking something like the following in your template:

This is incredibly helpful because of the terrible support for <button> and even <div> tags across email clients. So your buttons will actually compile to look something like this:

Which is, of course, a nightmare zone (can anyone spot the valign attribute? It’s actually not a typo 😱).

And finally, the icing on the cake: the browser preview layout. When you npm start your browser launches a side-by-side desktop and mobile view, with a dropdown that allows you to select from any of your email templates (see the image below).

Live-reload preview running in browser. Notice the drop down in the top right — use that to choose another email template to work on. Brilliant!

Pretty powerful stuff so far, but familiar territory for anyone who’s written front end code in the recent past. It feels strangely awesome to put it to use in email design.

On top of the amazing dev experience, you also get some pretty sophisticated testing capabilities. Email clients are incredibly finicky, so relying exclusively on the browser to test your emails is unwise. But just substitute a few values in a secrets.json file, and boom: you can send test emails of any of your templates from the command line! It’s pretty fun—you can spam all of your friends and ask them to help you QA 😉.

To set it up, just substitute a few strings in a secrets.json file:

Then send test emails right from the command line:

Though we didn’t get a chance to try them out, you can also take advantage of a variety of other features, like automatically uploading your local images to a CDN, or sending emails to Litmus to test compatibility with various clients, browsers, and devices. Pretty cool stuff.

Getting Some Inspiration

To finish up part 1, we’ll just point you to a few other resources we found that were a big help to us. One of those is Really Good Emails. It’s a catalog of a variety of types of emails from some cool companies. Search by keyword or category to find inspiration!

A Spotify welcome email on reallygoodemails.com.

And here are a few other resources to dig into:

Coming Up in Part 2

In Part 2, we’ll talk about how to wire everything up using Node and the powerful Dot.js templating engine to compile your shiny new email template with dynamic user data, and then automatically send them out through the Mailgun API. Stay tuned!

Thanks for reading! If you found it helpful be sure to click the green heart. Happy coding. 💥

--

--