node-email-templates 0.0.9
Node.js module for rendering beautiful emails with ejs templates and email-friendly inline CSS using juice.
UPDATE: Teelaunch plans to rewrite this module, subscribe to their email newsletter for special updates.
v0.0.9:
- Fixed
juice
dependency issue
v0.0.7:
- Added support for ejs's
include
directive thanks to @nicjansma
v0.0.6:
- Fixed batch problem (
...has no method slice
) thanks to @vekexasia
v0.0.5:
- Added support for an optional zlib compression type (e.g. you can return compressed html/text buffer for db storage)
...
template('newsletter', locals, 'deflateRaw', function(err, html, text) {
// The `html` and `text` are buffers compressed using zlib.deflateRaw
// <http://nodejs.org/docs/latest/api/zlib.html
// **NOTE**: You could also pass 'deflate' or 'gzip' if necessary, and it works with batch rendering as well
})
...
v0.0.4 (with bug fix for 0.0.3):
- Removed requirement for
style.css
and text.ejs
files with compatibility in node
v0.6.x to v0.8.x (utilizes path.exists
vs. fs.exists
respectively).
Email Templates
For professional and customizable email templates, please visit https://github.com/mailchimp/Email-Blueprints.
Installation
Unix/OS X
npm install email-templates
Windows
npm install email-templates-windows
Quick start
- Install the module for your respective project
npm install email-templates
. - Create a folder called
templates
inside your root directory (or elsewhere). - For each of your templates, respectively name and create a folder inside the
templates
folder. - Add the following files inside the template's folder:
html.ejs
- html + ejs version of your email template (required)text.ejs
- text + ejs version of your email template (optional)style.css
- stylesheet for the template, which will render html.ejs
with inline CSS (optional)
- You may use the
include
directive from ejs (for example, to include a common header or footer). See the /examples
folder for details. - Utilize one of the examples below for your respective email module and start sending beautiful emails!
EJS Custom Tags
Want to use different opening and closing tags instead of the EJS's default <%
and %>
?.
...
emailTemplates(templatesDir, { open: '{{', close: '}}' }, function(err, template) {
...
NOTE: You can also pass other options from EJS's documentation.
Usage
Render a template for a single email or render multiple (having only loaded the template once).
var path = require('path')
, templatesDir = path.join(__dirname, 'templates')
, emailTemplates = require('email-templates');
emailTemplates(templatesDir, function(err, template) {
var locals = { pasta: 'Spaghetti' };
template('pasta-dinner', locals, function(err, html, text) {
});
var locals = [
{ pasta: 'Spaghetti' },
{ pasta: 'Rigatoni' }
];
var Render = function(locals) {
this.locals = locals;
this.send = function(err, html, text) {
};
this.batch = function(batch) {
batch(this.locals, this.send);
};
};
template('pasta-dinner', true, function(err, batch) {
for(var user in users) {
var render = new Render(users[user]);
render.batch(batch);
}
});
});
var path = require('path')
, templatesDir = path.resolve(__dirname, '..', 'templates')
, emailTemplates = require('email-templates')
, nodemailer = require('nodemailer');
emailTemplates(templatesDir, function(err, template) {
if (err) {
console.log(err);
} else {
var transport = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
user: "some-user@gmail.com",
pass: "some-password"
}
});
var locals = {
email: 'mamma.mia@spaghetti.com',
name: {
first: 'Mamma',
last: 'Mia'
}
};
template('newsletter', locals, function(err, html, text) {
if (err) {
console.log(err);
} else {
transport.sendMail({
from: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
to: locals.email,
subject: 'Mangia gli spaghetti con polpette!',
html: html,
text: text
}, function(err, responseStatus) {
if (err) {
console.log(err);
} else {
console.log(responseStatus.message);
}
});
}
});
var transportBatch = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
user: "some-user@gmail.com",
pass: "some-password"
}
});
var users = [
{
email: 'pappa.pizza@spaghetti.com',
name: {
first: 'Pappa',
last: 'Pizza'
}
},
{
email: 'mister.geppetto@spaghetti.com',
name: {
first: 'Mister',
last: 'Geppetto'
}
}
];
var Render = function(locals) {
this.locals = locals;
this.send = function(err, html, text) {
if (err) {
console.log(err);
} else {
transportBatch.sendMail({
from: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
to: locals.email,
subject: 'Mangia gli spaghetti con polpette!',
html: html,
text: text
}, function(err, responseStatus) {
if (err) {
console.log(err);
} else {
console.log(responseStatus.message);
}
});
}
};
this.batch = function(batch) {
batch(this.locals, this.send);
};
};
template('newsletter', true, function(err, batch) {
for(var user in users) {
var render = new Render(users[user]);
render.batch(batch);
}
});
}
});
NOTE: Did you know nodemailer
can also be used to send SMTP email through Postmark? See this section of their Readme for more info.
For more message format options, see this section of Postmark's developer documentation section.
var path = require('path')
, templatesDir = path.resolve(__dirname, '..', 'templates')
, emailTemplates = require('email-templates')
, postmark = require('postmark')('your-api-key');
emailTemplates(templatesDir, function(err, template) {
if (err) {
console.log(err);
} else {
var locals = {
email: 'mamma.mia@spaghetti.com',
name: {
first: 'Mamma',
last: 'Mia'
}
};
template('newsletter', locals, function(err, html, text) {
if (err) {
console.log(err);
} else {
postmark.send({
From: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
To: locals.email,
Subject: 'Mangia gli spaghetti con polpette!',
HtmlBody: html,
TextBody: text
}, function(err, response) {
if (err) {
console.log(err.status);
console.log(err.message);
} else {
console.log(response);
}
});
}
});
var users = [
{
email: 'pappa.pizza@spaghetti.com',
name: {
first: 'Pappa',
last: 'Pizza'
}
},
{
email: 'mister.geppetto@spaghetti.com',
name: {
first: 'Mister',
last: 'Geppetto'
}
}
];
var Render = function(locals) {
this.locals = locals;
this.send = function(err, html, text) {
if (err) {
console.log(err);
} else {
postmark.send({
From: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
To: locals.email,
Subject: 'Mangia gli spaghetti con polpette!',
HtmlBody: html,
TextBody: text
}, function(err, response) {
if (err) {
console.log(err.status);
console.log(err.message);
} else {
console.log(response);
}
});
}
};
this.batch = function(batch) {
batch(this.locals, this.send);
};
};
template('newsletter', true, function(err, batch) {
for(user in users) {
var render = new Render(users[user]);
render.batch(batch);
}
});
}
});
Contributors
License
MIT Licensed