Node Email Templates
!NPM versionnpm-imagenpm-url
!Build Statustravis-imagetravis-url
!NPM downloadsnpm-downloadsnpm-url
!Test Coveragecoveralls-imagecoveralls-url
!Static Analysiscodeclimate-imagecodeclimate-url
!MIT Licenselicense-imagelicense-url
!Gittergitter-imagegitter-url
!js-standard-stylestandard-imagestandard-urlNode.js NPM package for rendering beautiful emails with your template engine and CSS pre-processor of choice coupled with email-friendly inline CSS using juicejuice.
Enjoy this package? Follow @yeliahs!
Note
This package (v2) is extended version of email-templates. v2 adds support for templating subjects.Index
* [Basic](#basic)
Email Templates
For customizable, pre-built email templates, see Email Blueprintsemail-blueprints and Transactional Email Templatestransactional-email-templates.Supported Template Engines
email-templates-v2 uses consolidate.jsconsolidate, and therefore supports a vast array of template modules. Please see consolidate.jsconsolidate 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:- Use vagrant to create a linux dev environment (recommended)
- Follow the Windows installation guide for contextify
Installation
Installemail-templates-v2
and the engines you wish to use by adding them to your package.json
dependencies.npm install --save email-templates-v2
# 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
- Install the module for your respective project:
```bash
npm install --save email-templates-v2@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
```bash
npm install --save <engine>
```
- 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.
```bash
mkdir templates/welcome-email
```
- 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](#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.
- You may use the
include
directive from ejsejs (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 juicejuicenew 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
// 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 = [
{
email: 'pappa.pizza@spaghetti.com',
name: {
first: 'Pappa',
last: 'Pizza'
}
},
{
email: 'mister.geppetto@spaghetti.com',
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[0].subject)
console.log(results[1].html)
console.log(results[1].text)
console.log(results[1].subject)
})
More
Please check the examples directoryContributors
- Nick Baugh
- Andrea Baccega
- Nic Jansma
- Jason Sims
- Miguel Mota
- Jeduan Cornejo
- Shailendra Sharma
Full list of contributors can be found on the GitHub Contributor Graphgh-graph