
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
email-templates
Advanced tools
Create, preview, and send custom email templates for Node.js. Highly configurable and supports automatic inline CSS, stylesheets, embedded images and fonts, and much more! Made for sending beautiful emails with Lad.
Create, preview, and send custom email templates for Node.js. Highly configurable and supports automatic inline CSS, stylesheets, embedded images and fonts, and much more! Made for sending beautiful emails with Lad.
NEW: v3.0.0 is released! See the 2.x branch documentation if you're using an older version
By default we recommend pug for your template engine, but you can use any template engine.
npm:
npm install email-templates pug
yarn:
yarn add email-templates pug
We've added preview-email by default to this package!
This means that (by default) in the development environment (e.g. NODE_ENV=development
) your emails will be rendered to the tmp directory for you and automatically opened in the browser.
You can swap the
transport
option with a Nodemailer transport configuration object or transport instance. We highly recommend using Postmark for your transport (it's the default in Lad).
const Email = require('email-templates');
const email = new Email({
message: {
from: 'niftylettuce@gmail.com'
},
transport: {
jsonTransport: true
}
});
email.send({
template: 'mars',
message: {
to: 'elon@spacex.com'
},
locals: {
name: 'Elon'
}
}).then(console.log).catch(console.error);
The example above assumes you have the following directory structure:
.
├── app.js
└── emails
└── mars
├── html.pug
└── subject.pug
And the contents of the pug
files are:
html.pug
:
p Hi #{name},
p Welcome to Mars, the red planet.
subject.pug
:
= `Hi ${name}, welcome to Mars`
We strongly suggest to follow this example and pre-cache your templates with cache-pug-templates (if you're using the default Pug template engine).
If you do not do this, then your Pug templates will re-compile and re-cache every time you deploy new code and restart your app.
Ensure you have Redis (v4.x+) installed:
Mac: brew install redis && brew services start redis
Ubuntu:
sudo add-apt-repository -y ppa:chris-lea/redis-server
sudo apt-get update
sudo apt-get -y install redis-server
Install the packages:
npm:
npm install cache-pug-templates redis
yarn:
yarn add cache-pug-templates redis
Configure it to read and cache your entire email templates directory:
const path = require('path');
const cachePugTemplates = require('cache-pug-templates');
const redis = require('redis');
const Email = require('email-templates');
const redisClient = redis.createClient();
const email = new Email({
message: {
from: 'niftylettuce@gmail.com'
},
transport: {
jsonTransport: true
}
});
cachePugTemplates(redisClient, email.config.views.root);
// ...
For more configuration options see cache-pug-templates.
All you need to do is simply pass an i18n configuration object as config.i18n
(or an empty one as this example shows to use defaults).
const Email = require('email-templates');
const email = new Email({
message: {
from: 'niftylettuce@gmail.com'
},
transport: {
jsonTransport: true
},
i18n: {} // <------ HERE
});
email.send({
template: 'mars',
message: {
to: 'elon@spacex.com'
},
locals: {
name: 'Elon'
}
}).then(console.log).catch(console.error);
Then slightly modify your templates to use localization functions.
html.pug
:
p= t(`Hi ${name},`)
p= t('Welcome to Mars, the red planet.')
subject.pug
:
= t(`Hi ${name}, welcome to Mars`)
Note that if you use Lad, you have a built-in filter called translate
:
p: :translate(locale) Hi #{name}
p: :translate(locale) Welcome to Mars, the red planet.
By default we use
html-to-text
to generate a plaintext version and attach it asmessage.text
.
If you'd like to customize the text body, you can pass message.text
or set config.htmlToText: false
(doing so will automatically lookup a text
template file just like it normally would for html
and subject
).
const Email = require('email-templates');
const email = new Email({
message: {
from: 'niftylettuce@gmail.com'
},
transport: {
jsonTransport: true
},
htmlToText: false // <----- HERE
});
email.send({
template: 'mars',
message: {
to: 'elon@spacex.com'
},
locals: {
name: 'Elon'
}
}).then(console.log).catch(console.error);
text.pug
:
| Hi #{name},
| Welcome to Mars, the red planet.
Install your desired template engine (e.g. EJS)
npm:
npm install ejs
yarn:
yarn add ejs
Set the extension in options and send an email
const Email = require('email-templates');
const email = new Email({
message: {
from: 'niftylettuce@gmail.com'
},
transport: {
jsonTransport: true
},
views: {
options: {
extension: 'ejs' // <---- HERE
}
}
});
You can configure your Email instance to have default message options, such as a default "From", an unsubscribe header, etc.
For a list of all available message options and fields see the Nodemailer mesage reference.
Here's an example showing how to set a default custom header and a list unsubscribe header:
const Email = require('email-templates');
const email = new Email({
message: {
from: 'niftylettuce@gmail.com',
headers: {
'X-Some-Custom-Thing': 'Some-Value'
},
list: {
unsubscribe: 'https://niftylettuce.com/unsubscribe'
}
},
transport: {
jsonTransport: true
}
});
You can pass a custom config.render
function which accepts two arguments view
and locals
and must return a Promise
.
If you wanted to read a stored EJS template from MongoDB, you could do something like:
const ejs = require('ejs');
const email = new Email({
// ...
render: (view, locals) => {
return new Promise((resolve, reject) => {
// this example assumes that `template` returned
// is an ejs-based template string
db.templates.findOne({ view }, (err, template) => {
if (err) return reject(err);
if (!template) return reject(new Error('Template not found'));
resolve(ejs.render(template, locals));
});
});
}
});
For a list of all available options and defaults view the configuration object.
You can use any nodemailer plugin. Simply pass an existing transport instance as config.transport
.
You should add the nodemailer-base64-to-s3 plugin to convert base64 inline images to actual images stored on Amazon S3 and Cloudfront.
We also highly recommend to add to your default config.locals
the following:
Instead of having to configure this for yourself, you could just use Lad instead.
Name | Website |
---|---|
Nick Baugh | http://niftylettuce.com |
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 130,661 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.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.