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.
This commit is contained in:
David Glasser
2015-01-26 15:19:19 -08:00
parent 0c7b6dd460
commit a355dd213d
4 changed files with 43 additions and 3 deletions

View File

@@ -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

View File

@@ -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');
}));
};

View File

@@ -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) {

View File

@@ -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);
};