Sending a notification email using handlebars templates

708 views
Skip to first unread message

Kat Bella

unread,
Jan 15, 2015, 1:58:19 AM1/15/15
to keyst...@googlegroups.com
Hello :)

I am running keystonejs and using the handlebars templating engine. I would like to integrate Mandrill to send out notification emails when someone sends an enquiry but the following happens:
- The post is successful and recorded 
- I can view the enquiry in the admin ui
- The Mandrill API log does NOT show an API call failing or being successful
- No email is sent


I have in my keystone.js file the following in the init call:

// some options above
'custom engine': handlebars.create({
 layoutsDir
: 'templates/views/layouts',
 partialsDir
: 'templates/views/partials',
 defaultLayout
: 'default',
 helpers
: new require('./templates/views/helpers')(),
 extname
: '.hbs'
 
}).engine,
 
 
'auto update': true,
 
'emails': 'templates/emails',
 
'mandrill api key': 'my api key',
   
'mandrill username': 'my username',
// some options below

and in my Enquiry.js file I have the following:
Enquiry.schema.methods.sendNotificationEmail = function(callback) {
 
 
if ('function' !== typeof callback) {
 callback
= function() {};
 
}
 
 
var enquiry = this;


 
 keystone
.list('User').model.find().where('isAdmin', true).exec(function(err, admins) {
 
 
if (err) return callback(err);
 
 
new keystone.Email('enquiry-notification').send({
 to
: admins,
 
from: {
 name
: 'Kat Bella',
 email
: 'm...@email.com'
 
},
 subject
: 'New Inquiry for Kat Bella',
 enquiry
: enquiry
 
}, callback);


 
});
};

At Jed Watson's suggestion, I swapped out the above send method for the following:
 new keystone.Email('enquiry-notification').send({
    subject
: 'New Inquiry for Kat Bella',
    to
: 'me@email.com',
    fromName
: 'Kat Bella',
    fromEmail
: 'm...@email.com',
        enquiry
: enquiry
 
}, callback);


Still no luck.  I would appreciate any input on what I am doing wrong. Thanks!

atlb...@gmail.com

unread,
Jan 15, 2015, 12:17:08 PM1/15/15
to keyst...@googlegroups.com
What version of Keystone are you using? Have you tried using the callback to print any errors to the console?

Kat Bella

unread,
Jan 15, 2015, 4:13:43 PM1/15/15
to keyst...@googlegroups.com
I used the version that the yeoman generator had about a week ago, so possibly 0.2.16 or 0.2.15.

I have not tried using the callback to print errors yet - I will try that next and report my findings. 

(currently not looking at code so will get back to this tonight)

Kat Bella

unread,
Jan 16, 2015, 2:29:47 AM1/16/15
to keyst...@googlegroups.com
alright so the error I'm getting is - 


{ [Error: ENOENT, open '/......../katbella.com/templates/emails/enquiry-notification/email.jade']
  errno
: 34,
  code
: 'ENOENT',
  path
: '/............./katbella.com/templates/emails/enquiry-notification/email.jade' }


At a high level glance it appears to be looking for the jade extension still. But shouldn't the handlebars handling code take care of this? Or is this not helpful?

Kat Bella

unread,
Jan 16, 2015, 2:30:18 AM1/16/15
to keyst...@googlegroups.com
Apologies in advance - still new to keystone. :)

Kat Bella

unread,
Jan 16, 2015, 2:44:07 AM1/16/15
to keyst...@googlegroups.com
Another update (sorry, posting as I go along)

Went into the email.js class and required in handlebars and changed the extension and template type from jade (default) to handlebars. Seems that the yeoman generator didn't do this automatically when I selected the templating engine?

Got it to send notification emails - sending two though - need to verify this is because it's set to send to admins and I think I have two with the same contact email.

Kat Bella

unread,
Jan 16, 2015, 2:49:11 AM1/16/15
to keyst...@googlegroups.com
That was exactly it - had two admin users with the same email.

Seems good now! Would appreciate clarification regarding the keystone yeoman generator not changing the Email class to take the templating engine option into account.

Tom Hartnell

unread,
Jan 20, 2015, 12:39:26 PM1/20/15
to keyst...@googlegroups.com
Hi Bella,
I've been trying to do the same so glad to hear you got it working. 
From a cursory glance at the Email class it seems you can pass the template engine config in the options when creating an email
which should avoid the need to modify the Keystone internals. 

Kat Bella

unread,
Jan 20, 2015, 12:58:54 PM1/20/15
to keyst...@googlegroups.com
Hi Tom,

Interesting. Do you have a snippet of code you can show that shows this working? It would be great to not have to alter the internals. :)

James Lukensow

unread,
Jan 20, 2015, 2:00:17 PM1/20/15
to keyst...@googlegroups.com
This is what I ended up doing, which is using swig for the email template:

    keystone.Email.defaults.templateExt = 'swig';
    keystone.Email.defaults.templateEngine = require('swig');

    new keystone.Email('forgotten-password').send({
      user: user,
      link: '/reset-password/' + user.resetPasswordKey,
      subject: 'Reset your account',
      to: user.email,
      from: {
        name: 'From Name',
        email: 'nor...@something.com'
      }
    }, callback);

Kat Bella

unread,
Jan 20, 2015, 2:01:56 PM1/20/15
to keyst...@googlegroups.com
Great! Thanks for that - I will remove my internal changes and do that instead. Thanks for the help. :)

Tom Hartnell

unread,
Jan 21, 2015, 6:50:13 AM1/21/15
to keyst...@googlegroups.com
That's great thanks James.
I've been passing it in the options when creating the email (below) but 
changing the defaults globally as you have is probably neater.

  var enquiry = this,
      options
= {
        templateExt
: 'hbs',
        templateEngine
: require('handlebars'),
        templateName
: 'enquiry-notification'
     
},
      email
= new keystone.Email(options);


Reply all
Reply to author
Forward
0 new messages