Merge pull request #2252 from jondavidjohn/from-address-default

Change fallback from address to webmaster@[blog.url]
This commit is contained in:
Hannah Wolfe 2014-02-27 15:56:46 +00:00
commit 895180fbf0
2 changed files with 40 additions and 3 deletions

View File

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

View File

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