Cleanup email module
This commit is contained in:
parent
e866cebc28
commit
d56439cf8c
@ -54,47 +54,6 @@ class Email {
|
|||||||
this._sender = options.sender;
|
this._sender = options.sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A generic method to send an email message via nodemailer.
|
|
||||||
* @param {Object} from sender user id object: { name:'Jon Smith', email:'j@smith.com' }
|
|
||||||
* @param {Object} to recipient user id object: { name:'Jon Smith', email:'j@smith.com' }
|
|
||||||
* @param {string} subject message subject
|
|
||||||
* @param {string} text message plaintext body template
|
|
||||||
* @param {string} html message html body template
|
|
||||||
* @param {Object} params (optional) nodermailer template parameters
|
|
||||||
* @yield {Object} reponse object containing SMTP info
|
|
||||||
*/
|
|
||||||
*send(options) {
|
|
||||||
let template = {
|
|
||||||
subject: options.subject,
|
|
||||||
text: options.text,
|
|
||||||
html: options.html
|
|
||||||
};
|
|
||||||
let sender = {
|
|
||||||
from: {
|
|
||||||
name: options.from.name,
|
|
||||||
address: options.from.email
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let recipient = {
|
|
||||||
to: {
|
|
||||||
name: options.to.name,
|
|
||||||
address: options.to.email
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let params = options.params || {};
|
|
||||||
|
|
||||||
try {
|
|
||||||
let sendFn = this._transport.templateSender(template, sender);
|
|
||||||
let info = yield sendFn(recipient, params);
|
|
||||||
log.silly('email', 'Email sent.', info);
|
|
||||||
return info;
|
|
||||||
} catch(error) {
|
|
||||||
log.error('email', 'Sending email failed.', error, options);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send the verification email to the user to verify email address
|
* Send the verification email to the user to verify email address
|
||||||
* ownership. If the primary email address is provided, only one email
|
* ownership. If the primary email address is provided, only one email
|
||||||
@ -131,20 +90,54 @@ class Email {
|
|||||||
html: message.verifyKey.html,
|
html: message.verifyKey.html,
|
||||||
params: {
|
params: {
|
||||||
name: userId.name,
|
name: userId.name,
|
||||||
protocol: origin.protocol,
|
baseUrl: origin.protocol + '://' + origin.host,
|
||||||
host: origin.host,
|
|
||||||
keyid: encodeURIComponent(userId.keyid),
|
keyid: encodeURIComponent(userId.keyid),
|
||||||
nonce: encodeURIComponent(userId.nonce)
|
nonce: encodeURIComponent(userId.nonce)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
return yield this.send(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic method to send an email message via nodemailer.
|
||||||
|
* @param {Object} from sender user id object: { name:'Jon Smith', email:'j@smith.com' }
|
||||||
|
* @param {Object} to recipient user id object: { name:'Jon Smith', email:'j@smith.com' }
|
||||||
|
* @param {string} subject message subject
|
||||||
|
* @param {string} text message plaintext body template
|
||||||
|
* @param {string} html message html body template
|
||||||
|
* @param {Object} params (optional) nodermailer template parameters
|
||||||
|
* @yield {Object} reponse object containing SMTP info
|
||||||
|
*/
|
||||||
|
*send(options) {
|
||||||
|
let template = {
|
||||||
|
subject: options.subject,
|
||||||
|
text: options.text,
|
||||||
|
html: options.html
|
||||||
|
};
|
||||||
|
let sender = {
|
||||||
|
from: {
|
||||||
|
name: options.from.name,
|
||||||
|
address: options.from.email
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let recipient = {
|
||||||
|
to: {
|
||||||
|
name: options.to.name,
|
||||||
|
address: options.to.email
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let params = options.params || {};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let info = yield this.send(msg);
|
let sendFn = this._transport.templateSender(template, sender);
|
||||||
|
let info = yield sendFn(recipient, params);
|
||||||
if (!this._checkResponse(info)) {
|
if (!this._checkResponse(info)) {
|
||||||
log.warn('email', 'Verification mail may not have been received.', info);
|
log.warn('email', 'Message may not have been received.', info);
|
||||||
}
|
}
|
||||||
return info;
|
return info;
|
||||||
} catch(e) {
|
} catch(error) {
|
||||||
util.throw(500, 'Sending verification email failed');
|
log.error('email', 'Sending message failed.', error, options);
|
||||||
|
util.throw(500, 'Sending email to user failed');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"verifyKey": {
|
"verifyKey": {
|
||||||
"subject": "Verify Your Key",
|
"subject": "Verify Your Key",
|
||||||
"text": "Hello {{name}},\n\nplease click here to verify your key: {{protocol}}://{{host}}/api/v1/verify/?keyid={{keyid}}&nonce={{nonce}}",
|
"text": "Hello {{name}},\n\nplease click here to verify your key: {{baseUrl}}/api/v1/verify/?keyid={{keyid}}&nonce={{nonce}}",
|
||||||
"html": ""
|
"html": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -17,12 +17,6 @@ describe.skip('Koa App (HTTP Server) Integration Tests', function() {
|
|||||||
app = yield init();
|
app = yield init();
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(function () {});
|
|
||||||
|
|
||||||
afterEach(function() {});
|
|
||||||
|
|
||||||
after(function () {});
|
|
||||||
|
|
||||||
describe('REST api', function() {
|
describe('REST api', function() {
|
||||||
describe('POST /api/v1/key', function() {
|
describe('POST /api/v1/key', function() {
|
||||||
it('should return 400 for an invalid body', function (done) {
|
it('should return 400 for an invalid body', function (done) {
|
||||||
|
Loading…
Reference in New Issue
Block a user