Localized HTML emails for node
npm install node-html-emailsLocalized HTML email templates for node.
* Uses Handlebars as templating engine
* Makes localization with i18n
* You can write your own css file which will be inlined with juice
Make your own mail templates using Handlebars, run it through this package to generate the HTML-string which is ready to be sent with your choice of node-mailer.
```
npm install node-html-emails --save
`js
const app = require('express')();
// The nhe constructor
const NHE = require('node-html-emails');
// The descriptor tells nhe what layout and partial(s) to use to generate the template
// In this case the backbone of the html email will be the main layout
// And the content of the email comes from the activation partial
// By default nhe parses .hbs templates and registers them by their names. E.g
// main.layout.hbs can be refrenced as "main", and activation.partial.hbs as activation so on..
const templateDescriptors = {
activation: {
layout: 'main',
content: 'activation'
}
};
// We need to tell nhe the root of your project and it will search for .layout.hbs and .partial.hbs
// files in it by default. But it can be configured as well.
// We also need to tell nhe the directory where your translation files are held
const nheOptions = {
root: path.resolve(__dirname),
locales: {
locales: ['en', 'hu'],
directory: ${__dirname}/locales
}
}
// Instantiation
const nhe = new NHE(templateDescriptors, nheOptions);
app.post('/mail/:type', (req, res) => {
const type = req.params.type;
const lang = req.query.lang || 'en';
const templateParams = { email: 'some@email.com' };
// Generating output for your email. In a real world example "htmlString" will be used as the body
// of your email. Here, for demonstration, we send it back as the response for the request.
const htmlString = nhe.generate(type, templateParams, { locale: lang });
// Go on, make a POST request to http://localhost:3000/mail/activation?lang=en|hu in postman to
// preview your email
res.send(htmlString);
});
app.listen(3000, () => console.log('Example app listening on port 3000!'));
`main.layout.hbs
For the example to work you will need to have a and an activation.partial.hbs in your project somewhere. The package will precompile these templates for you.
For example
`html
`
`html`
{{ translate 'GREETING' }} {{ email }},
{{ translate 'ACTIVATION_MSG' }}
For the translation to work you'll need translation files located somewhere in your project
`json`
{
"GREETING": "Dear",
"ACTIVATION_MSG": "Your account has been activated."
}
`json``
{
"GREETING": "Kedves",
"ACTIVATION_MSG": "A fiókja aktiválásra került."
}