2019-11-04 12:36:10 +07:00
|
|
|
// @ts-check
|
|
|
|
const mailService = require('../mail');
|
|
|
|
const ghostMailer = new mailService.GhostMailer();
|
2019-11-04 12:36:10 +07:00
|
|
|
const common = require('../../lib/common');
|
2019-11-04 12:36:10 +07:00
|
|
|
|
2019-11-04 17:24:02 +07:00
|
|
|
/**
|
2019-11-04 12:36:10 +07:00
|
|
|
* An email address
|
|
|
|
* @typedef { string } EmailAddress
|
|
|
|
*/
|
|
|
|
|
2019-11-04 17:24:02 +07:00
|
|
|
/**
|
2019-11-04 12:36:10 +07:00
|
|
|
* An object representing an email to send
|
|
|
|
* @typedef { Object } Email
|
|
|
|
* @property { string } html - The html content of the email
|
2019-11-04 12:36:10 +07:00
|
|
|
* @property { string } subject - The subject of the email
|
2019-11-04 12:36:10 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* @param {Email} message - The message to send
|
|
|
|
* @param {[EmailAddress]} recipients - the recipients to send the email to
|
2019-11-06 17:50:41 +07:00
|
|
|
* @param {[object]} recipientData - list of data keyed by email to inject into the email
|
2019-11-04 12:36:10 +07:00
|
|
|
* @returns {Promise<boolean>} A promise representing the success of the email sending
|
|
|
|
*/
|
2019-11-06 17:50:41 +07:00
|
|
|
async send(message, recipients, recipientData) {
|
2019-11-04 13:23:24 +07:00
|
|
|
for (const recipient of recipients) {
|
2019-11-04 12:36:10 +07:00
|
|
|
const messageToSend = Object.assign({}, message, {
|
|
|
|
to: recipient
|
|
|
|
});
|
2019-11-06 18:36:15 +07:00
|
|
|
let unsubscribeUrl = '';
|
|
|
|
if (recipientData && recipientData[recipient]) {
|
|
|
|
unsubscribeUrl = recipientData[recipient].unsubscribe_url;
|
|
|
|
}
|
2019-11-06 17:50:41 +07:00
|
|
|
messageToSend.html = messageToSend.html.replace('%recipient.unsubscribe_url%', unsubscribeUrl);
|
2019-11-04 12:36:10 +07:00
|
|
|
try {
|
|
|
|
await ghostMailer.send(messageToSend);
|
|
|
|
} catch (err) {
|
|
|
|
// @TODO log this somewhere with state?
|
2019-11-04 12:36:10 +07:00
|
|
|
common.logging.warn(`Oh no! an email failed to send :( ${recipient}`);
|
2019-11-04 12:36:10 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|