Giter VIP home page Giter VIP logo

node-email-templates's Introduction

Node Email Templates

NPM version Build Status NPM downloads Test Coverage Static Analysis MIT License Gitter 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.

Enjoy this package? Check out eskimo and express-cdn, 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:

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

Template Engine Options

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 %>?.

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

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

var async = require('async')
var users = [
  {name: 'John', pasta: 'Rigatoni'},
  {name: 'Luca', pasta: 'Tortellini'}
]

async.each(users, function (user, next) {
  newsletter.render(user, function (err, results) {
    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) 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 = [
  {
    email: '[email protected]',
    name: {
      first: 'Pappa',
      last: 'Pizza'
    }
  },
  {
    email: '[email protected]',
    name: {
      first: 'Mister',
      last: 'Geppetto'
    }
  }
]

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[1].html)
    console.log(results[1].text)
  })

More

Please check the examples directory

Contributors

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

License

MIT

node-email-templates's People

Contributors

a-b-r-o-w-n avatar alexandrusavin avatar antoinepairet avatar bratchenko avatar connor-knabe avatar dchymko avatar designbyonyx avatar faceleg avatar gierschv avatar jasminetsai avatar jasonsims avatar jeduan avatar jksdua avatar jonkemp avatar kewisch avatar kingcody avatar mike-spainhower avatar nicjansma avatar niftylettuce avatar ninowalker avatar rblu avatar remicastaing avatar roymax avatar skimmmer avatar vekexasia avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.