Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/mandrill template #1517

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -397,8 +401,24 @@ 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 template to html itselfs
// Following code is adapted to use options attributes (e.g. verifyHref) in mandrill templates via nodemailer-mandrill-transport
// 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
}];
} 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) {
Expand Down