npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

collabos-email-templates

v2.5.6

Published

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

Downloads

11

Readme

Node Email Templates

Slack Status NPM version Build Status NPM downloads Test Coverage Static Analysis MIT License js-standard-style

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.

NEW IN VERSION 2.4.0: Localization support has been added! You can now translate your emails for users! See Localized Template below for more info!

Enjoy this package? Build rapid startups using it with Glazed and follow and follow @niftylettuce!

Index

Email Templates

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

Supported Template Engines

node-email-templates uses consolidate.js, and therefore supports a vast array of template modules. Please see consolidate.js for the impressive full list.

Supported CSS Pre-processors

Prerequisites

Important Note for Windows Users

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:

Installation

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

npm install --save email-templates
# See https://www.npmjs.com/package/consolidate for a full list of available template engines
npm install -S [ejs|jade|nunjucks|handlebars|emblem|dust-linkedin]

Quick Start

  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.0.0

    • nunjucks@^1.0.0

    • handlebars@^3.0.0

    • dust-linkedin@^2.0.0

    • less@^2.0.0

    • stylus@^0.51.0

    • styl@^0.2.0

    • node-sass@^3.0.0

    • See https://www.npmjs.com/package/consolidate for a full list

    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) - for html format of email
    • text.{{ext}} (optional) - for text format of email
    • style.{{ext}}(optional) - styles for html format
    • subject.{{ext}}(optional) - for subject of email

    See supported template engines for possible template engine extensions (e.g. .ejs, .jade, .nunjucks) 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., style., and subject. 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.

Template Engine Options

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

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

new EmailTemplate(templateDir, { delimiter: '?' })

You can also directly modify the template engine

// ...
Handlebars.registerPartial('name', '{{name.first}} {{name.last}}')
Handlebars.registerHelper('capitalize', function (context) {
  return context.toUpperCase()
})
new EmailTemplate(templateDir)
// ...

You can also pass a juiceOptions object to configure the output from juice

new EmailTemplate(templateDir, {juiceOptions: {
  preserveMediaQueries: false,
  removeStyleTags: false
}})

You can check all the options in juice's documentation

If you wish to disable juice, you can pass disableJuice as an option:

new EmailTemplate(templateDir, { disableJuice: true })

You can add includePaths for sass using sassOptions.

new EmailTemplate(templateDir, {sassOptions: {
  includePaths: ['~/someproject/sass']
}})

Examples

Basic

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 (err, result) {
  // result.html
  // result.text
})

var async = require('async')
var users = [
  {
    email: '[email protected]',
    name: {
      first: 'Pappa',
      last: 'Pizza'
    }
  },
  {
    email: '[email protected]',
    name: {
      first: 'Mister',
      last: 'Geppetto'
    }
  }
]

async.each(users, function (user, next) {
  newsletter.render(user, function (err, result) {
    if (err) return next(err)
    // result.html
    // result.text
    // result.subject
  })
}, function (err) {
  //
})

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

var path           = require('path')
var templateDir   = path.join(__dirname, 'templates', 'pasta-dinner')
var EmailTemplate = require('email-templates').EmailTemplate

var template = new EmailTemplate(templateDir)
var users = [
  {name: 'John', pasta: 'Rigatoni'},
  {name: 'Luca', pasta: 'Tortellini'}
]

var templates = users.map(function (user) {
  return template.render(user)
})

Promise.all(templates)
  .then(function (results) {
    console.log(results[0].html)
    console.log(results[0].text)
    console.log(results[0].subject)
    console.log(results[1].html)
    console.log(results[1].text)
    console.log(results[1].subject)
  })

Localized Template

Localized template folder:

templates/
templates/newsletter/
// defalt locale templates are stored in root folder and by default is en-us:
templates/newsletter/html.{{ext}}
templates/newsletter/style.{{ext}}
// for add pt-br locale:
templates/newsletter/pt-br/html.{{ext}}
templates/newsletter/pt-br/style.{{ext}}

To render the pt-br localized version for folder structure above, pass locale name in .render method

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 (err, result) {
  // result.html
  // result.text
})

var async = require('async')
var users = [
  {
    email: '[email protected]',
    name: {
      first: 'Pappa',
      last: 'Pizza'
    }
  },
  {
    email: '[email protected]',
    name: {
      first: 'Mister',
      last: 'Geppetto'
    }
  }
]

async.each(users, function (user, next) {
  // render the pt-br localized template:
  newsletter.render(user, 'pt-br', function (err, result) {
    if (err) return next(err)
    // result.html
    // result.text
    // result.subject
  })
}, function (err) {
  //
})

More

Please check the examples directory

Contributors

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

License

MIT