email-templates

Node.js module for rendering beautiful emails with ejs, jade, swig, hbs, or handlebars templates and email-friendly inline CSS using juice.

Node Email Templates

Node.js NPM package for rendering beautiful emails with your template engine and CSS pre-processor of choice coupled with email-friendly inline CSS using juice.

Enjoy this package? Check out eskimo and express-cdn, and follow @niftylettuce!

For customizable, pre-built email templates, see Email Blueprints and Transactional Email Templates.

Developing on OS X or Ubuntu/Linux is recommended, but if you only have access to a Windows machine you can do one of the following:

Install email-templates and the engines you wish to use by adding them to your package.json dependencies.

npm install --save email-templates
npm install -S [ejs|jade|swig|handlebars|emblem|dust-linkedin]
  1. Install the module for your respective project:

    npm install --save email-templates@2
  2. Install the template engine you intend to use:

    • ejs@^2.0.0

    • jade@^1.9.0

    • swig@^1.4.2

    • handlebars@^3.0.0

    • dust-linkedin@^2.7.0

    • less@^2.5.0

    • stylus@^^0.51.0

    • styl@^0.2.9

    • node-sass@^3.0.0

    npm install --save <engine>
  3. For each of your email templates (e.g. a welcome email to send to users when they register on your site), respectively name and create a folder.

    mkdir templates/welcome-email
  4. Add the following files inside the template's folder:

    • html.{{ext}} (required)
    • text.{{ext}} (optional)
    • style.{{ext}}(optional)

    See supported template engines for possible template engine extensions (e.g. .ejs, .jade, .swig) to use for the value of {{ext}} above.

    You may prefix any file name with anything you like to help you identify the files more easily in your IDE. The only requirement is that the filename contains html., text., and style. respectively.

  5. You may use the include directive from ejs (for example, to include a common header or footer). See the /examples folder for details.

If your want to configure your template engine, just pass options.

Want to use different opening and closing tags instead of the EJS's default <% and %>?.

emailTemplates(templatesDir, { delimiter: '?' }, function (errtemplate) {

You can also pass other options from EJS's documentation.

Want to add a helper or partial to Handlebars?

// ... 
emailTemplates(templatesDir, {
  helpers: {
    uppercasefunction(context) {
      return context.toUpperCase()
    }
  }, partials: {
    // ... 
  }
})
// ... 

Render a single template (having only loaded the template once).

var EmailTemplate = require('email-templates').EmailTemplate
var path = require('path')
 
var templateDir = path.join(__dirname, 'templates', 'newsletter')
 
var newsletter = new EmailTemplate(templateDir)
var user = {name: 'Joe', pasta: 'spaghetti'}
newsletter.render(user, function (errresults) {
  // result.html 
  // result.text 
})
 
var async = require('async')
var users = [
  {name: 'John', pasta: 'Rigatoni'},
  {name: 'Luca', pasta: 'Tortellini'}
]
 
async.each(users, function (usernext) {
  newsletter.render(user, function (errresults) {
    if (err) return next(err)
    // result.html 
    // result.text 
  })
}, function (err) {
  // 
})

Render a template for a single email or render multiple (having only loaded the template once).

var path           = require('path')
var templatesDir   = path.join(__dirname, 'templates')
var emailTemplates = require('email-templates');
 
emailTemplates(templatesDir, function(errtemplate) {
 
  // Render a single email with one template 
  var locals = { pasta: 'Spaghetti' };
 
  template('pasta-dinner', locals, function(errhtmltext) {
    // ... 
  });
 
  // Render multiple emails with one template 
  var locals = [
    { pasta: 'Spaghetti' },
    { pasta: 'Rigatoni' }
  ];
 
  var Render = function(locals) {
    this.locals = locals;
    this.send = function(errhtmltext) {
      // ... 
    };
    this.batch = function(batch) {
      batch(this.locals, templatesDir, this.send);
    };
  };
 
  // An example users object 
  var users = [
    {
      email: 'pappa.pizza@spaghetti.com',
      name: {
        first: 'Pappa',
        last: 'Pizza'
      }
    },
    {
      email: 'mister.geppetto@spaghetti.com',
      name: {
        first: 'Mister',
        last: 'Geppetto'
      }
    }
  ];
 
  template('pasta-dinner', true, function(errbatch) {
    for(var user in users) {
      var render = new Render(users[user]);
      render.batch(batch);
    }
  });
 
});

Please check the examples directory

Full list of contributors can be found on the GitHub Contributor Graph

MIT