Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
email-templates
Advanced tools
Node.js module for rendering beautiful emails with ejs, jade, swig, hbs, or handlebars templates and email-friendly inline CSS using juice.
The email-templates npm package is a powerful tool for creating, managing, and sending email templates. It supports various templating engines, inline CSS, and internationalization, making it a versatile choice for handling email communications in Node.js applications.
Template Rendering
This feature allows you to render email templates with dynamic data. In this example, the 'mars/html' template is rendered with the name 'Elon'.
const Email = require('email-templates');
const email = new Email();
email.render('mars/html', { name: 'Elon' })
.then(console.log)
.catch(console.error);
Sending Emails
This feature allows you to send emails using a specified transporter. In this example, an email is sent using Gmail's SMTP service with the 'mars' template and dynamic data.
const Email = require('email-templates');
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your-email@gmail.com',
pass: 'your-email-password'
}
});
const email = new Email({
message: {
from: 'your-email@gmail.com'
},
transport: transporter
});
email.send({
template: 'mars',
message: {
to: 'elon@musk.com'
},
locals: {
name: 'Elon'
}
}).then(console.log).catch(console.error);
Inline CSS
This feature allows you to inline CSS styles in your email templates. In this example, CSS styles are inlined into the 'mars/html' template.
const Email = require('email-templates');
const email = new Email({
juice: true,
juiceResources: {
preserveImportant: true,
webResources: {
relativeTo: path.resolve('path/to/css')
}
}
});
email.render('mars/html', { name: 'Elon' })
.then(console.log)
.catch(console.error);
Internationalization (i18n)
This feature allows you to support multiple languages in your email templates. In this example, the 'mars/html' template is rendered in French.
const Email = require('email-templates');
const email = new Email({
i18n: {
locales: ['en', 'fr'],
directory: path.join(__dirname, 'locales')
}
});
email.render('mars/html', { name: 'Elon', locale: 'fr' })
.then(console.log)
.catch(console.error);
Nodemailer is a module for Node.js applications to allow easy email sending. While it does not provide templating capabilities out of the box, it can be used in conjunction with other templating engines to achieve similar functionality to email-templates.
MJML is a markup language designed to reduce the pain of coding responsive emails. It focuses on the structure and design of emails, and can be used alongside email-templates for rendering MJML templates.
Handlebars is a popular templating engine that can be used to create dynamic email templates. It does not provide email sending capabilities, but can be integrated with Nodemailer or other email sending libraries.
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.
node-email-templates uses consolidate.js, and therefore supports a vast array of template modules. Please see consolidate.js for the impressive full list.
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
# 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]
Install the module for your respective project:
npm install --save email-templates@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>
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
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.
, andstyle.
respectively.
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 %>
?.
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
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: '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[1].html)
console.log(results[1].text)
})
Please check the examples directory
Full list of contributors can be found on the GitHub Contributor Graph
FAQs
Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for Forward Email and Lad.
The npm package email-templates receives a total of 93,724 weekly downloads. As such, email-templates popularity was classified as popular.
We found that email-templates demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.