From a355dd213ddd7aa7278ed68c16b8fb01ca01fda7 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Mon, 26 Jan 2015 15:19:19 -0800 Subject: [PATCH] Allow individual email templates to override From Also allow accounts-password email templates to set mail headers, and for the From override to be a function rather than a constant. Fixes #2858. Fixes #2854. --- docs/client/full-api/api/passwords.md | 4 ++++ packages/accounts-password/email_tests.js | 9 ++++++++ .../accounts-password/email_tests_setup.js | 12 +++++++++++ packages/accounts-password/password_server.js | 21 ++++++++++++++++--- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/docs/client/full-api/api/passwords.md b/docs/client/full-api/api/passwords.md index fe0b6c1a56..0d839df07c 100644 --- a/docs/client/full-api/api/passwords.md +++ b/docs/client/full-api/api/passwords.md @@ -119,7 +119,11 @@ Override fields of the object by assigning to them: to set this to an email address that you can receive email at. - `siteName`: The public name of your application. Defaults to the DNS name of the application (eg: `awesome.meteor.com`). +- `headers`: An `Object` for custom email headers as described in + [`Email.send`](#email_send). - `resetPassword`: An `Object` with two fields: + - `resetPassword.from`: A `Function` used to override the `from` address defined + by the `emailTemplates.from` field. - `resetPassword.subject`: A `Function` that takes a user object and returns a `String` for the subject line of a reset password email. - `resetPassword.text`: A `Function` that takes a user object and a url, and diff --git a/packages/accounts-password/email_tests.js b/packages/accounts-password/email_tests.js index bb4a5df990..ba37c19a40 100644 --- a/packages/accounts-password/email_tests.js +++ b/packages/accounts-password/email_tests.js @@ -38,6 +38,9 @@ testAsyncMulti("accounts emails - reset password flow", [ test.isTrue(match); resetPasswordToken = match[1]; test.isTrue(options.html.match(re)); + + test.equal(options.from, 'test@meteor.com'); + test.equal(options.headers['My-Custom-Header'], 'Cool'); })); }, function (test, expect) { @@ -79,6 +82,9 @@ var getVerifyEmailToken = function (email, test, expect) { test.isTrue(match); verifyEmailToken = match[1]; test.isTrue(options.html.match(re)); + + test.equal(options.from, 'test@meteor.com'); + test.equal(options.headers['My-Custom-Header'], 'Cool'); })); }; @@ -173,6 +179,9 @@ var getEnrollAccountToken = function (email, test, expect) { test.isTrue(match); enrollAccountToken = match[1]; test.isTrue(options.html.match(re)); + + test.equal(options.from, 'test@meteor.com'); + test.equal(options.headers['My-Custom-Header'], 'Cool'); })); }; diff --git a/packages/accounts-password/email_tests_setup.js b/packages/accounts-password/email_tests_setup.js index 6c21b4d452..da5e007fdf 100644 --- a/packages/accounts-password/email_tests_setup.js +++ b/packages/accounts-password/email_tests_setup.js @@ -12,6 +12,18 @@ Accounts.emailTemplates.resetPassword.html = return url; }; +// override the from address +Accounts.emailTemplates.resetPassword.from = + Accounts.emailTemplates.enrollAccount.from = + Accounts.emailTemplates.verifyEmail.from = function (user) { + return 'test@meteor.com'; + }; + +// add a custom header to check against +Accounts.emailTemplates.headers = { + 'My-Custom-Header' : 'Cool' +}; + EmailTest.hookSend(function (options) { var to = options.to; if (to.indexOf('intercept') === -1) { diff --git a/packages/accounts-password/password_server.js b/packages/accounts-password/password_server.js index 4245207f77..268b9715d3 100644 --- a/packages/accounts-password/password_server.js +++ b/packages/accounts-password/password_server.js @@ -396,7 +396,8 @@ Accounts.sendResetPasswordEmail = function (userId, email) { var options = { to: email, - from: Accounts.emailTemplates.from, + from: Accounts.emailTemplates.resetPassword.from ? + Accounts.emailTemplates.resetPassword.from(user) : Accounts.emailTemplates.from, subject: Accounts.emailTemplates.resetPassword.subject(user), text: Accounts.emailTemplates.resetPassword.text(user, resetPasswordUrl) }; @@ -405,6 +406,10 @@ Accounts.sendResetPasswordEmail = function (userId, email) { options.html = Accounts.emailTemplates.resetPassword.html(user, resetPasswordUrl); + if (typeof Accounts.emailTemplates.headers === 'object') { + options.headers = Accounts.emailTemplates.headers; + } + Email.send(options); }; @@ -454,7 +459,8 @@ Accounts.sendEnrollmentEmail = function (userId, email) { var options = { to: email, - from: Accounts.emailTemplates.from, + from: Accounts.emailTemplates.enrollAccount.from ? + Accounts.emailTemplates.enrollAccount.from(user) : Accounts.emailTemplates.from, subject: Accounts.emailTemplates.enrollAccount.subject(user), text: Accounts.emailTemplates.enrollAccount.text(user, enrollAccountUrl) }; @@ -463,6 +469,10 @@ Accounts.sendEnrollmentEmail = function (userId, email) { options.html = Accounts.emailTemplates.enrollAccount.html(user, enrollAccountUrl); + if (typeof Accounts.emailTemplates.headers === 'object') { + options.headers = Accounts.emailTemplates.headers; + } + Email.send(options); }; @@ -590,7 +600,8 @@ Accounts.sendVerificationEmail = function (userId, address) { var options = { to: address, - from: Accounts.emailTemplates.from, + from: Accounts.emailTemplates.verifyEmail.from ? + Accounts.emailTemplates.verifyEmail.from(user) : Accounts.emailTemplates.from, subject: Accounts.emailTemplates.verifyEmail.subject(user), text: Accounts.emailTemplates.verifyEmail.text(user, verifyEmailUrl) }; @@ -599,6 +610,10 @@ Accounts.sendVerificationEmail = function (userId, address) { options.html = Accounts.emailTemplates.verifyEmail.html(user, verifyEmailUrl); + if (typeof Accounts.emailTemplates.headers === 'object') { + options.headers = Accounts.emailTemplates.headers; + } + Email.send(options); };