Skip to content

Commit

Permalink
Change fallback from address to webmaster@[blog.url]
Browse files Browse the repository at this point in the history
This change is needed because the previous default of the user's email
address is too often mismatched against the site domain, triggering spam filters.

Fixes #2145
- added `fromAddress()` to GhostMailer to handle this logic
- added unit tests to `mail_spec.js`
  • Loading branch information
jondavidjohn authored and ErisDS committed Mar 14, 2014
1 parent fd0838f commit a90d759
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
21 changes: 18 additions & 3 deletions core/server/mail.js
Expand Up @@ -82,6 +82,22 @@ GhostMailer.prototype.emailDisabled = function () {
this.transport = null;
};

GhostMailer.prototype.fromAddress = function () {
var from = config().mail && config().mail.fromaddress,
domain;

if (!from) {
// Extract the domain name from url set in config.js
domain = config().url.match(new RegExp("^https?://([^/:?#]+)(?:[/:?#]|$)", "i"));
domain = domain && domain[1];

// Default to webmaster@[blog.url]
from = 'webmaster@' + domain;
}

return from;
};

// Sends an e-mail message enforcing `to` (blog owner) and `from` fields
GhostMailer.prototype.send = function (message) {
var self = this;
Expand All @@ -94,11 +110,10 @@ GhostMailer.prototype.send = function (message) {
}

return api.settings.read('email').then(function (email) {
var from = (config().mail && config().mail.fromaddress) || email.value,
to = message.to || email.value;
var to = message.to || email.value;

message = _.extend(message, {
from: from,
from: self.fromAddress(),
to: to,
generateTextFromHTML: true
});
Expand Down
22 changes: 22 additions & 0 deletions core/test/unit/mail_spec.js
Expand Up @@ -167,4 +167,26 @@ describe("Mail", function () {
done();
});
});

it('should use from address as configured in config.js', function (done) {
overrideConfig({mail:{fromaddress: 'static@example.com'}});
mailer.fromAddress().should.equal('static@example.com');
done();
});

it('should fall back to webmaster@[blog.url] as from address', function (done) {
// Standard domain
overrideConfig({url: 'http://default.com', mail:{fromaddress: null}});
mailer.fromAddress().should.equal('webmaster@default.com');

// Trailing slash
overrideConfig({url: 'http://default.com/', mail:{}});
mailer.fromAddress().should.equal('webmaster@default.com');

// Strip Port
overrideConfig({url: 'http://default.com:2368/', mail:{}});
mailer.fromAddress().should.equal('webmaster@default.com');

done();
});
});

0 comments on commit a90d759

Please sign in to comment.