Templated transactional emails in MJML

A microservice for developing, testing and sending templated transactional emails in your next app

Adam Quaile
2 min readNov 13, 2017

Remove boilerplate SMTP code, take advantage of templates in any language, and fix email client issues with MJML.

The Status Quo

As a developer, having worked on many applications I think I’ve written a “forgotten your password” email a thousand times. The same goes for “Welcome to product X” or “Purchase confirmation”.

These transactional emails are a simple necessity in almost all apps and thankfully they’re not that complex. You just have to write some HTML. And it’s dynamic, so setup a template language, like Twig, Handlebars, or Jinja2.

For good measure, add a plain-text version for old email clients and spam filters.

Now send it, just integrate a library like SwiftMailer, or Nodemailer, configure your SMTP or API details within your framework of choice.

Oh, but this email needs a PDF attachment, and of course that’s dynamically generated too, and you probably need to see quite a few versions of the email and PDF before you’re done, so maybe build a way to test what they look like in development.

Speaking of development, you’re not actually sending these email are you? No, you’re probably using Mailhog, or Mailcatcher.

Reinventing the wheel

So it’s simple. No really, most of us developers have done this many times and not had much trouble. But it’s a lot of work. All this time spent configuring libraries, tools and services, when there’s so many more interesting things to build.

All of these libraries and tools have improved this considerably, but in the new era of microservices and 12factor apps, I think we can do better.

Enveloper is a simple API-driven service which you can run through a public docker image, mount your templates, configure credentials for your favourite mail delivery service, and you’re ready.

Now a simple API request will:

  • Parse and resolve placeholders using a template language like Twig
  • Convert MJML to HTML (optional, but a massive time-saver, and fixes a load of rendering issues)
  • Send your message through your provider

Additionally, it:

  • Handles plain text versions
  • Supports multiple attachments
  • Is capable of many (and mixed) template languages and preprocessors

Head over to Github to get started, or to Twitter to follow along for updates on new features; there’s plenty on the way! Have an idea or a question? Missing something? I’m happy to help.

--

--