node package manager


Preprocessing and Sending HTML Emails in Node.js


Preprocessing and Sending HTML Emails in Node.js

  1. Send HTML Emails
  2. Use preprocessors like Jade and Sass to write them
  3. Only care about the template and its data at time of sending

This plugin requires Grunt ~0.4.2

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install jamoose --save

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:


If you can figure out how I can use loadNpmTasks, please open a pull request

You can require it in your application like so:

var jamoose = require('jamoose'),
    Mailer = new jamoose({});

In your project's Gruntfile, add a section named jamoose to the data object passed into grunt.initConfig().

  jamoose: {
    options: {
      // Task-specific options go here. 
    your_target: {
      // Target-specific file lists and/or options go here. 

Type: Object Default value: {}

Same as Jade API options

_NB: options.jade.filename defaults to the current file's path

Type: Object Default value: { url: 'file://' + process.cwd() + '/' }

Same as Juice options

Important: End your url with a trailing slash

  jamoose: {
    default_options: {
      files: [
          expand: true,
          flatten: false,
          cwd: 'test/fixtures',
          src: '**/*.jade',
          dest: 'tmp',
          ext: '.html'

See the examples for detailed, working code.

  1. Create your email template using Jade. Use Jade variables for anything you want replaced at build time (think cross-app vars like domains and dates). Use Mustache variables for anything you want inserted at send time (think user-specific details like name or address)
//- welcome.jade
    link(rel="stylesheet", href="path/to/email.css")
          h1 Welcome {{name}}
          a(href=domain + '/privacy') Privacy Policy
// email.sass
  font-size: 30px
  color: #333
  1. Build your templates with Grunt
$ grunt sass jamoose

You now will have an HTML file with inlined CSS* and Mustache variables still in tact.

          <h1 style="font-size:30px;color:#333">Welcome {{name}}</h1>
          <a href="http://your.domain.from.grunt/privacy">Privacy Policy</a>
  1. Send an email in your app
var jamoose = require('jamoose'),
    mailer = new jamoose({
      tplPath: 'path/to/templates/from/grunt/',
      fromEmail: ''
  '', // to 
  'Welcome!', // subject 
  'welcome', // template 
  { // data 
    name: 'John Smith'
  function(err) { // callback 
    if (err) { console.log(err); }

"Welcome John Smith" will be sent to "" from "" with the subject "Welcome!"

Currently supporting:

Please submit a pull request to add more service providers.

Set the environment variables for the provider of your choice and it will be used automatically.

SendGrid's node library is used under the hood. Required environment variables:

  • SENDGRID_USER - API user (usually email address)
  • SENDGRID_KEY - API key (usually your password)

Mandrill's node library is used under the hood. Required environment variable:

If process.env.NODE_ENV is set to development, HTML files will be written to a temporary file when you send. The path is console.log for you to inspect. If you'd like to actually send an email during development or locally, set process.env.NODE_ENV to something besides development.

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Tests are written with nodeunit and can be run with npm test

v2.0.0 - Drop support for Node v0.8 v1.0.0 - Add Mandrill support

Copyright (c) 2014 Max Beatty Licensed under the MIT license.