From 4e9e54464997616886103aee2f18f5a2e4eae909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Holubec?= Date: Fri, 10 Jul 2015 14:43:00 +0200 Subject: [PATCH 1/3] Modified sending email, support for nodemailer-mandrill-transport and mandrill templates --- common/models/user.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/common/models/user.js b/common/models/user.js index 2677f0d98..398a56692 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -397,8 +397,19 @@ module.exports = function(User) { options.headers = options.headers || {}; - var template = loopback.template(options.template); - options.html = template(options); + if (options.template) { + // only if template is defined + if (options.template.name) { + // if template is object with name attribute then is expected that email transport connector is converting it to html itselfs, e.g. nodemailer-mandrill-transport + // template content should have reference to all attributes already filled in the options object + options.template.content = options; + } else { + var template = loopback.template(options.template); + options.html = template(options); + // due to nodemailer-mandrill-transport, which use template different way, attribute template is removed if template was already locally converted to html + delete options.template; + } + } Email.send(options, function(err, email) { if (err) { From d679e0185ffef36796681ff14de6032e332b243d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Holubec?= Date: Fri, 10 Jul 2015 16:12:48 +0200 Subject: [PATCH 2/3] Mandrill template will get options attributes via global_merge_vars --- common/models/user.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/common/models/user.js b/common/models/user.js index 398a56692..43cec2c56 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -6,6 +6,7 @@ var loopback = require('../../lib/loopback'); var path = require('path'); var SALT_WORK_FACTOR = 10; var crypto = require('crypto'); +var extend = require('util')._extend; var bcrypt; try { @@ -342,7 +343,10 @@ module.exports = function(User) { assert(options.from, 'Must include options.from when calling user.verify() or the user must have an email property'); options.redirect = options.redirect || '/'; - options.template = path.resolve(options.template || path.join(__dirname, '..', '..', 'templates', 'verify.ejs')); + if (typeof options.template === 'string') { + // load template from file only if filename is provided, template could be also object with name to support external mandrill templates + options.template = path.resolve(options.template || path.join(__dirname, '..', '..', 'templates', 'verify.ejs')); + } options.user = this; options.protocol = options.protocol || 'http'; @@ -400,9 +404,13 @@ module.exports = function(User) { if (options.template) { // only if template is defined if (options.template.name) { - // if template is object with name attribute then is expected that email transport connector is converting it to html itselfs, e.g. nodemailer-mandrill-transport - // template content should have reference to all attributes already filled in the options object - options.template.content = options; + // if template is object with name attribute then is expected that email transport connector is converting template to html itselfs + // Following code is adapted to use options attributes (e.g. verifyHref) in mandrill templates via nodemailer-mandrill-transport + var options_clone = extend({}, options); + options.global_merge_vars = [{ + name: 'options', + content: options_clone + }]; } else { var template = loopback.template(options.template); options.html = template(options); From 15ef2cbd06804590fd4d927948225e6dc84c331f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Holubec?= Date: Fri, 10 Jul 2015 22:12:20 +0200 Subject: [PATCH 3/3] NOTE: options attribute names in camelCase format are in template visible in lowercase format --- common/models/user.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/models/user.js b/common/models/user.js index 43cec2c56..51ebc22a7 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -406,7 +406,8 @@ module.exports = function(User) { if (options.template.name) { // if template is object with name attribute then is expected that email transport connector is converting template to html itselfs // Following code is adapted to use options attributes (e.g. verifyHref) in mandrill templates via nodemailer-mandrill-transport - var options_clone = extend({}, options); + // NOTE: all atribute names in options in camelCase format will be converted to lowercase format, i.e. options.verifyHref to options.verifyhref + var options_clone = JSON.parse(JSON.stringify(options)); // to prevent error "converting circular structure to JSON" options.global_merge_vars = [{ name: 'options', content: options_clone