I am using nodemailer to send emails using the following nodemailer-express-handlebars plugin. I used this {dead blog post} as reference
The code is compiling the welcome
template but is not using the layout
My code is as below:
var nodemailer = require('nodemailer');
var mg = require('nodemailer-mailgun-transport');
var hbs = require('nodemailer-express-handlebars');
var config = {auth: {api_key: "key-xxx",domain: "mydomain.com}}
var nodemailerTransport = nodemailer.createTransport(mg(config));
var options = {
viewEngine: {
extname: '.handlebars',
layoutsDir: 'views/email/',
defaultLayout : 'layout',
},
viewPath: 'views/email/'
}
nodemailerTransport.use('compile', hbs(options));
nodemailerTransport.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'Welcome to the XXX',
template: 'welcome'
}, function (err, results) {
if (err) console.log('Error: ' + err);
else console.log('Response: ' + results);
});
My layout.handlebars
has the following code
<html>
<body>
{{> _header }}
{{{body}}}
{{> _footer }}
</body>
</html>
Nodemailer-express-handlebars is a useful plugin for Nodemailer that allows you to use Handlebars templates in your HTML emails | Link. The documentation on that page does not provide a full usage example and is really short on details.
HandleBars can be used to render web pages to the client side from data on the server-side. To use handlebars in express, we need to store HTML code into a . hbs extension in the 'views' folder in the source directory as hbs looks for the pages in the views folder. Now, we need to change the default view engine.
You are missing a partialsDir
option.
I have tested with the following options and it works fine :
var options = {
extName:'.hbs', /* or '.handlebars' */
viewPath:__dirname+'/views/email/',
layoutsDir:__dirname+'/view/email',
defaultLayout:'template',
partialsDir:__dirname+'/views/email/partials/'
}
To use my directory structure :
views
email
(here store 'template.hbs')partials
folder (here as example store 'header.hbs')If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With