From 06d5e0d9bfd11c219bce5df82e54780b9fbd9039 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Fri, 8 Apr 2016 15:00:54 +0300 Subject: [PATCH] Fixed an issue with broken archive link. Added plaintext versions for transactional messages --- lib/mailer.js | 29 ++++++++++---- lib/models/users.js | 4 +- package.json | 14 +++---- routes/archive.js | 57 ++++++++++++++++++++-------- routes/subscription.js | 3 +- views/emails/confirm-html.hbs | 26 +++++++++++++ views/emails/confirm-mail.hbs | 18 --------- views/emails/confirm-text.hbs | 10 +++++ views/emails/password-reset-html.hbs | 23 +++++++++++ views/emails/password-reset-text.hbs | 9 +++++ views/emails/password-reset.hbs | 14 ------- 11 files changed, 144 insertions(+), 63 deletions(-) create mode 100644 views/emails/confirm-html.hbs delete mode 100644 views/emails/confirm-mail.hbs create mode 100644 views/emails/confirm-text.hbs create mode 100644 views/emails/password-reset-html.hbs create mode 100644 views/emails/password-reset-text.hbs delete mode 100644 views/emails/password-reset.hbs diff --git a/lib/mailer.js b/lib/mailer.js index de9c1274..b5eb38af 100644 --- a/lib/mailer.js +++ b/lib/mailer.js @@ -38,24 +38,39 @@ module.exports.sendMail = (mail, template, callback) => { }); } - if (!template || !template.template) { - return module.exports.transport.sendMail(mail, callback); - } - - getTemplate(template.template, (err, renderer) => { + getTemplate(template.html, (err, htmlRenderer) => { if (err) { return callback(err); } - mail.html = renderer(template.data || {}); - module.exports.transport.sendMail(mail, callback); + + if (htmlRenderer) { + mail.html = htmlRenderer(template.data || {}); + } + + getTemplate(template.text, (err, textRenderer) => { + if (err) { + return callback(err); + } + + if (textRenderer) { + mail.text = textRenderer(template.data || {}); + } + + module.exports.transport.sendMail(mail, callback); + }); }); }; function getTemplate(template, callback) { + if (!template) { + return callback(null, false); + } + if (templates.has(template)) { return callback(null, templates.get(template)); } + fs.readFile(path.join(__dirname, '..', 'views', template), 'utf-8', (err, source) => { if (err) { return callback(err); diff --git a/lib/models/users.js b/lib/models/users.js index eb82abee..6631d292 100644 --- a/lib/models/users.js +++ b/lib/models/users.js @@ -218,8 +218,10 @@ module.exports.sendReset = (username, callback) => { }, subject: 'Mailer password change request' }, { - template: 'emails/password-reset.hbs', + html: 'emails/password-reset-html.hbs', + text: 'emails/password-reset-text.hbs', data: { + title: 'Mailtrain', username: rows[0].username, confirmUrl: urllib.resolve(configItems.serviceUrl, '/users/reset') + '?token=' + encodeURIComponent(resetToken) + '&username=' + encodeURIComponent(rows[0].username) } diff --git a/package.json b/package.json index 03195e8a..36b48a6d 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "node" : ">=5.0.0" }, "devDependencies": { - "grunt": "^0.4.5", - "grunt-cli": "^1.1.0", + "grunt": "^1.0.1", + "grunt-cli": "^1.2.0", "grunt-contrib-nodeunit": "^1.0.0", "grunt-eslint": "^18.0.0" }, @@ -28,13 +28,13 @@ "bcrypt-nodejs": "0.0.3", "body-parser": "^1.15.0", "compression": "^1.6.1", - "config": "^1.19.0", + "config": "^1.20.0", "connect-flash": "^0.1.1", "connect-redis": "^3.0.2", "content-security-policy": "^0.2.0", "cookie-parser": "^1.4.1", "csurf": "^1.8.3", - "csv-parse": "^1.0.2", + "csv-parse": "^1.0.4", "escape-html": "^1.0.3", "express": "^4.13.4", "express-session": "^1.13.0", @@ -46,13 +46,13 @@ "morgan": "^1.7.0", "multer": "^1.1.0", "mysql": "^2.10.2", - "nodemailer": "^2.3.0", + "nodemailer": "^2.3.1", "npmlog": "^2.0.3", "passport": "^0.3.2", "passport-local": "^1.0.0", - "request": "^2.69.0", + "request": "^2.70.0", "serve-favicon": "^2.3.0", - "shortid": "^2.2.4", + "shortid": "^2.2.6", "slugify": "^0.1.1", "smtp-server": "^1.9.0", "striptags": "^2.1.1", diff --git a/routes/archive.js b/routes/archive.js index 5a897d8b..170feb94 100644 --- a/routes/archive.js +++ b/routes/archive.js @@ -4,6 +4,7 @@ let settings = require('../lib/models/settings'); let campaigns = require('../lib/models/campaigns'); let lists = require('../lib/models/lists'); let subscriptions = require('../lib/models/subscriptions'); +let fields = require('../lib/models/fields'); let tools = require('../lib/tools'); let express = require('express'); let router = new express.Router(); @@ -51,24 +52,50 @@ router.get('/:campaign/:list/:subscription', (req, res, next) => { return next(err); } - campaigns.getMail(campaign.id, list.id, subscription.id, (err, mail) => { - if (err) { - req.flash('danger', err.message || err); - return res.redirect('/'); + fields.list(list.id, (err, fieldList) => { + if (err || !fieldList) { + return fieldList = []; } - if (!mail) { - err = new Error('Not Found'); - err.status = 404; - return next(err); - } + subscription.mergeTags = { + EMAIL: subscription.email, + FIRST_NAME: subscription.firstName, + LAST_NAME: subscription.lastName, + FULL_NAME: [].concat(subscription.firstName || []).concat(subscription.lastName || []).join(' ') + }; - res.render('archive/view', { - layout: 'archive/layout', - message: tools.formatMessage(serviceUrl, campaign, list, subscription, campaign.html), - campaign, - list, - subscription + fields.getRow(fieldList, subscription, true, true).forEach(field => { + if (field.mergeTag) { + subscription.mergeTags[field.mergeTag] = field.mergeValue || ''; + } + if (field.options) { + field.options.forEach(subField => { + if (subField.mergeTag) { + subscription.mergeTags[subField.mergeTag] = subField.mergeValue || ''; + } + }); + } + }); + + campaigns.getMail(campaign.id, list.id, subscription.id, (err, mail) => { + if (err) { + req.flash('danger', err.message || err); + return res.redirect('/'); + } + + if (!mail) { + err = new Error('Not Found'); + err.status = 404; + return next(err); + } + + res.render('archive/view', { + layout: 'archive/layout', + message: tools.formatMessage(serviceUrl, campaign, list, subscription, campaign.html), + campaign, + list, + subscription + }); }); }); }); diff --git a/routes/subscription.js b/routes/subscription.js index 36da66fa..cd0d689b 100644 --- a/routes/subscription.js +++ b/routes/subscription.js @@ -209,7 +209,8 @@ router.post('/:cid/subscribe', passport.parseForm, passport.csrfProtection, (req }, subject: list.name + ': Please Confirm Subscription' }, { - template: 'emails/confirm-mail.hbs', + html: 'emails/confirm-html.hbs', + text: 'emails/confirm-text.hbs', data: { title: list.name, contactAddress: configItems.defaultAddress, diff --git a/views/emails/confirm-html.hbs b/views/emails/confirm-html.hbs new file mode 100644 index 00000000..54b5a573 --- /dev/null +++ b/views/emails/confirm-html.hbs @@ -0,0 +1,26 @@ + + + + + + {{title}}: Please Confirm Subscription + + + + +

{{title}}

+ +

Please Confirm Subscription

+ +
+ +

+ +

If you received this email by mistake, simply delete it. You won't be subscribed if you don't click the confirmation link above.

+ +

For questions about this list, please contact: +
{{contactAddress}}

+ + + + diff --git a/views/emails/confirm-mail.hbs b/views/emails/confirm-mail.hbs deleted file mode 100644 index 858d3a0b..00000000 --- a/views/emails/confirm-mail.hbs +++ /dev/null @@ -1,18 +0,0 @@ -

{{title}}

- -

Please Confirm Subscription

- -
- -

- -

-

- - If you received this email by mistake, simply delete it. You won't be subscribed if you don't click the confirmation link above. -

-

- - For questions about this list, please contact: -
{{contactAddress}} -

diff --git a/views/emails/confirm-text.hbs b/views/emails/confirm-text.hbs new file mode 100644 index 00000000..3440f1e0 --- /dev/null +++ b/views/emails/confirm-text.hbs @@ -0,0 +1,10 @@ +{{{title}}} +Please Confirm Subscription +=========================== + +Yes, subscribe me to this list: {{{confirmUrl}}} + +If you received this email by mistake, simply delete it. You won't be subscribed if you don't click the confirmation link above. + +For questions about this list, please contact: +{{{contactAddress}}} diff --git a/views/emails/password-reset-html.hbs b/views/emails/password-reset-html.hbs new file mode 100644 index 00000000..adcded21 --- /dev/null +++ b/views/emails/password-reset-html.hbs @@ -0,0 +1,23 @@ + + + + + + Change your password + + + + +

Change your password

+ +
+ +

We have received a password change request for your Mailtrain account ({{username}}).

+ +

+ +

If you did not ask to change your password, then you can ignore this email and your password will not be changed.

+ + + + diff --git a/views/emails/password-reset-text.hbs b/views/emails/password-reset-text.hbs new file mode 100644 index 00000000..40b49e9c --- /dev/null +++ b/views/emails/password-reset-text.hbs @@ -0,0 +1,9 @@ +{{{title}}} +Change your password +==================== + +We have received a password change request for your Mailtrain account ({{{username}}}). + +Reset password: {{{confirmUrl}}} + +If you did not ask to change your password, then you can ignore this email and your password will not be changed. diff --git a/views/emails/password-reset.hbs b/views/emails/password-reset.hbs deleted file mode 100644 index bd61b0c9..00000000 --- a/views/emails/password-reset.hbs +++ /dev/null @@ -1,14 +0,0 @@ -

Change your password

- -
- -

- We have received a password change request for your Mailtrain account ({{username}}). -

- -

- -

-

- If you did not ask to change your password, then you can ignore this email and your password will not be changed. -