How to use rudders with nodemailer to send emails?
I am using nodemailer to send emails using the following nodemailer-express-handlebars plugin. I have used this Blog post as a link
The code compiles the template welcome
but doesn't uselayout
My code looks like this:
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: 'from@mydomain.com',
to: 'to@gmail.com',
subject: 'Welcome to the XXX',
template: 'welcome'
}, function (err, results) {
if (err) console.log('Error: ' + err);
else console.log('Response: ' + results);
});
Mine layout.handlebars
has the following code
<html>
<body>
{{> _header }}
{{{body}}}
{{> _footer }}
</body>
</html>
+3
source to share
1 answer
You are missing an option partialsDir
.
I've tested with the following parameters and it works great:
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:
- Where script the folder is placed:
views
- Inside this place a folder with a name is placed
email
("template.hbs" is stored here) - Inside your email folder create a folder
partials
(here as example store 'header.hbs')
0
source to share