8

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: '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);
});

My layout.handlebars has the following code

<html>
<body>
{{> _header }}
    {{{body}}}
{{> _footer }}
</body>
</html>
Michael Cole
  • 15,473
  • 7
  • 79
  • 96
Masade
  • 715
  • 1
  • 11
  • 29

1 Answers1

2

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 :

  1. Where the script is place a folder : views
  2. Inside of it place a folder called email (here store 'template.hbs')
  3. Inside of the email folder create a partials folder (here as example store 'header.hbs')
EMX
  • 6,066
  • 1
  • 25
  • 32