mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
208b710677
no issue - Tagging needs to be added to be able to group/filter sent messages for various reasons. An example use case is when multiple Ghost instances use the same mailgun account - Tag value can be provided as a part of config.json file under `bulkEmail.mailgun.tag` key
78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
const {URL} = require('url');
|
|
const mailgun = require('mailgun-js');
|
|
const configService = require('../../config');
|
|
const common = require('../../lib/common');
|
|
let mailgunInstance;
|
|
|
|
function createMailgun(config) {
|
|
const baseUrl = new URL(config.baseUrl);
|
|
|
|
return mailgun({
|
|
apiKey: config.apiKey,
|
|
domain: config.domain,
|
|
protocol: baseUrl.protocol,
|
|
host: baseUrl.host,
|
|
port: baseUrl.port,
|
|
endpoint: baseUrl.pathname
|
|
});
|
|
}
|
|
|
|
const config = configService.get('bulkEmail');
|
|
|
|
if (!config || !config.mailgun) {
|
|
common.logging.warn(`Bulk email service is not configured`);
|
|
} else {
|
|
try {
|
|
mailgunInstance = createMailgun(config.mailgun);
|
|
} catch (err) {
|
|
common.logging.warn(`Bulk email service is not configured`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* An email address
|
|
* @typedef { string } EmailAddress
|
|
*/
|
|
|
|
/**
|
|
* An object representing an email to send
|
|
* @typedef { Object } Email
|
|
* @property { string } html - The html content of the email
|
|
* @property { string } subject - The subject of the email
|
|
*/
|
|
|
|
module.exports = {
|
|
/**
|
|
* @param {Email} message - The message to send
|
|
* @param {[EmailAddress]} recipients - the recipients to send the email to
|
|
* @param {[object]} recipientData - list of data keyed by email to inject into the email
|
|
* @returns {Promise<boolean>} A promise representing the success of the email sending
|
|
*/
|
|
async send(message, recipients, recipientData) {
|
|
if (!mailgunInstance) {
|
|
return;
|
|
}
|
|
let fromAddress = message.from;
|
|
if (/@localhost$/.test(message.from)) {
|
|
fromAddress = 'localhost@example.com';
|
|
common.logging.warn(`Rewriting bulk email from address ${message.from} to ${fromAddress}`);
|
|
}
|
|
try {
|
|
const messageData = Object.assign({}, message, {
|
|
to: recipients.join(', '),
|
|
from: fromAddress,
|
|
'recipient-variables': recipientData
|
|
});
|
|
|
|
if (config.mailgun.tag) {
|
|
Object.assign(messageData, {
|
|
'o:tag': config.mailgun.tag
|
|
});
|
|
}
|
|
|
|
await mailgunInstance.messages().send(messageData);
|
|
} catch (err) {
|
|
common.logging.error({err});
|
|
}
|
|
}
|
|
};
|