2022-08-10 13:16:55 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const debug = require('@tryghost/debug');
|
|
|
|
const logging = require('@tryghost/logging');
|
2022-10-11 17:11:46 +03:00
|
|
|
const metrics = require('@tryghost/metrics');
|
2022-08-10 13:16:55 +03:00
|
|
|
|
|
|
|
module.exports = class MailgunClient {
|
|
|
|
#config;
|
|
|
|
#settings;
|
|
|
|
|
2022-08-18 23:14:54 +03:00
|
|
|
static BATCH_SIZE = 1000;
|
|
|
|
|
2022-08-10 13:16:55 +03:00
|
|
|
constructor({config, settings}) {
|
|
|
|
this.#config = config;
|
|
|
|
this.#settings = settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the data payload and sends to Mailgun
|
|
|
|
*
|
|
|
|
* @param {Object} message
|
|
|
|
* @param {Object} recipientData
|
|
|
|
* @param {Array<Object>} replacements
|
|
|
|
*
|
|
|
|
* recipientData format:
|
|
|
|
* {
|
|
|
|
* 'test@example.com': {
|
|
|
|
* name: 'Test User',
|
|
|
|
* unique_id: '12345abcde',
|
|
|
|
* unsubscribe_url: 'https://example.com/unsub/me'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2022-08-15 13:52:29 +03:00
|
|
|
async send(message, recipientData, replacements) {
|
2022-08-10 13:16:55 +03:00
|
|
|
const mailgunInstance = this.getInstance();
|
|
|
|
if (!mailgunInstance) {
|
|
|
|
logging.warn(`Mailgun is not configured`);
|
2022-08-11 09:55:53 +03:00
|
|
|
return null;
|
2022-08-10 13:16:55 +03:00
|
|
|
}
|
|
|
|
|
2022-08-18 23:28:10 +03:00
|
|
|
if (Object.keys(recipientData).length > MailgunClient.BATCH_SIZE) {
|
2022-08-11 09:49:10 +03:00
|
|
|
// TODO: what to do here?
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:16:55 +03:00
|
|
|
let messageData = {};
|
|
|
|
|
2022-10-11 17:11:46 +03:00
|
|
|
let startTime;
|
2022-08-10 13:16:55 +03:00
|
|
|
try {
|
|
|
|
const bulkEmailConfig = this.#config.get('bulkEmail');
|
|
|
|
const messageContent = _.pick(message, 'subject', 'html', 'plaintext');
|
|
|
|
|
|
|
|
// update content to use Mailgun variable syntax for replacements
|
|
|
|
replacements.forEach((replacement) => {
|
|
|
|
messageContent[replacement.format] = messageContent[replacement.format].replace(
|
2022-10-18 11:32:50 +03:00
|
|
|
replacement.regexp,
|
2022-08-10 13:16:55 +03:00
|
|
|
`%recipient.${replacement.id}%`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
messageData = {
|
|
|
|
to: Object.keys(recipientData),
|
|
|
|
from: message.from,
|
|
|
|
'h:Reply-To': message.replyTo || message.reply_to,
|
|
|
|
subject: messageContent.subject,
|
|
|
|
html: messageContent.html,
|
|
|
|
text: messageContent.plaintext,
|
2022-08-15 13:52:29 +03:00
|
|
|
'recipient-variables': JSON.stringify(recipientData)
|
2022-08-10 13:16:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// add a reference to the original email record for easier mapping of mailgun event -> email
|
|
|
|
if (message.id) {
|
|
|
|
messageData['v:email-id'] = message.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tags = ['bulk-email'];
|
2022-08-11 09:40:44 +03:00
|
|
|
if (bulkEmailConfig?.mailgun?.tag) {
|
2022-08-10 13:16:55 +03:00
|
|
|
tags.push(bulkEmailConfig.mailgun.tag);
|
|
|
|
}
|
|
|
|
messageData['o:tag'] = tags;
|
|
|
|
|
2022-08-11 09:40:44 +03:00
|
|
|
if (bulkEmailConfig?.mailgun?.testmode) {
|
2022-08-10 13:16:55 +03:00
|
|
|
messageData['o:testmode'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable tracking if turned on for this email
|
|
|
|
if (message.track_opens) {
|
|
|
|
messageData['o:tracking-opens'] = true;
|
|
|
|
}
|
|
|
|
|
2022-08-15 13:52:29 +03:00
|
|
|
const mailgunConfig = this.#getConfig();
|
2022-10-11 17:11:46 +03:00
|
|
|
startTime = Date.now();
|
2022-08-15 13:52:29 +03:00
|
|
|
const response = await mailgunInstance.messages.create(mailgunConfig.domain, messageData);
|
2022-10-11 17:11:46 +03:00
|
|
|
metrics.metric('mailgun-send-mail', {
|
|
|
|
value: Date.now() - startTime,
|
|
|
|
statusCode: 200
|
|
|
|
});
|
2022-08-10 13:16:55 +03:00
|
|
|
|
2022-08-15 13:52:29 +03:00
|
|
|
return {
|
|
|
|
id: response.id
|
|
|
|
};
|
2022-08-10 13:16:55 +03:00
|
|
|
} catch (error) {
|
2022-10-11 17:11:46 +03:00
|
|
|
logging.error(error);
|
|
|
|
metrics.metric('mailgun-send-mail', {
|
|
|
|
value: Date.now() - startTime,
|
|
|
|
statusCode: error.status
|
|
|
|
});
|
2022-08-10 13:16:55 +03:00
|
|
|
return Promise.reject({error, messageData});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchEvents(mailgunOptions, batchHandler, {maxEvents = Infinity} = {}) {
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
const mailgunInstance = this.getInstance();
|
|
|
|
if (!mailgunInstance) {
|
|
|
|
logging.warn(`Mailgun is not configured`);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(`fetchEvents: starting fetching first events page`);
|
2022-08-15 13:52:29 +03:00
|
|
|
const mailgunConfig = this.#getConfig();
|
2022-10-11 17:11:46 +03:00
|
|
|
let startTime = Date.now();
|
|
|
|
try {
|
|
|
|
let page = await mailgunInstance.events.get(mailgunConfig.domain, mailgunOptions);
|
|
|
|
metrics.metric('mailgun-get-events', {
|
|
|
|
value: Date.now() - startTime,
|
|
|
|
statusCode: 200
|
|
|
|
});
|
|
|
|
let events = page?.items?.map(this.normalizeEvent) || [];
|
|
|
|
debug(`fetchEvents: finished fetching first page with ${events.length} events`);
|
|
|
|
|
|
|
|
let eventCount = 0;
|
|
|
|
|
|
|
|
pagesLoop:
|
|
|
|
while (events.length !== 0) {
|
|
|
|
const batchResult = await batchHandler(events);
|
|
|
|
|
|
|
|
result = result.concat(batchResult);
|
|
|
|
eventCount += events.length;
|
|
|
|
|
|
|
|
if (eventCount >= maxEvents) {
|
|
|
|
break pagesLoop;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextPageId = page.pages.next.page;
|
|
|
|
debug(`fetchEvents: starting fetching next page ${nextPageId}`);
|
|
|
|
startTime = Date.now();
|
|
|
|
page = await mailgunInstance.events.get(mailgunConfig.domain, {
|
|
|
|
page: nextPageId,
|
|
|
|
...mailgunOptions
|
|
|
|
});
|
|
|
|
metrics.metric('mailgun-get-events', {
|
|
|
|
value: Date.now() - startTime,
|
|
|
|
statusCode: 200
|
|
|
|
});
|
|
|
|
events = page?.items?.map(this.normalizeEvent) || [];
|
|
|
|
debug(`fetchEvents: finished fetching next page with ${events.length} events`);
|
2022-08-10 13:16:55 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 17:11:46 +03:00
|
|
|
return result;
|
|
|
|
} catch (error) {
|
|
|
|
// Log and re-throw Mailgun errors
|
|
|
|
logging.error(error);
|
|
|
|
metrics.metric('mailgun-get-events', {
|
|
|
|
value: Date.now() - startTime,
|
|
|
|
statusCode: error.status
|
2022-08-15 13:52:29 +03:00
|
|
|
});
|
2022-10-11 17:11:46 +03:00
|
|
|
throw error;
|
2022-08-10 13:16:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
normalizeEvent(event) {
|
|
|
|
const providerId = event?.message?.headers['message-id'];
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: event.event,
|
|
|
|
severity: event.severity,
|
|
|
|
recipientEmail: event.recipient,
|
|
|
|
emailId: event['user-variables'] && event['user-variables']['email-id'],
|
|
|
|
providerId: providerId,
|
|
|
|
timestamp: new Date(event.timestamp * 1000)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:43:19 +03:00
|
|
|
#getConfig() {
|
2022-08-10 13:16:55 +03:00
|
|
|
const bulkEmailConfig = this.#config.get('bulkEmail');
|
|
|
|
const bulkEmailSetting = {
|
|
|
|
apiKey: this.#settings.get('mailgun_api_key'),
|
|
|
|
domain: this.#settings.get('mailgun_domain'),
|
|
|
|
baseUrl: this.#settings.get('mailgun_base_url')
|
|
|
|
};
|
|
|
|
|
2022-08-11 09:40:44 +03:00
|
|
|
const hasMailgunConfig = !!(bulkEmailConfig?.mailgun);
|
2022-08-10 13:16:55 +03:00
|
|
|
const hasMailgunSetting = !!(bulkEmailSetting && bulkEmailSetting.apiKey && bulkEmailSetting.baseUrl && bulkEmailSetting.domain);
|
|
|
|
|
|
|
|
if (!hasMailgunConfig && !hasMailgunSetting) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mailgunConfig = hasMailgunConfig ? bulkEmailConfig.mailgun : bulkEmailSetting;
|
2022-08-10 18:43:19 +03:00
|
|
|
return mailgunConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an instance of the Mailgun client based upon the config or settings values
|
|
|
|
*
|
|
|
|
* We don't cache the instance so we can always get a fresh one based upon changed settings
|
|
|
|
* or config values over time
|
|
|
|
*
|
|
|
|
* Note: if the credentials are not configure, this method returns `null` and it is down to the
|
|
|
|
* consumer to act upon this/log this out
|
|
|
|
*
|
2022-08-15 13:52:29 +03:00
|
|
|
* @returns {import('mailgun.js')} the Mailgun client instance
|
2022-08-10 18:43:19 +03:00
|
|
|
*/
|
|
|
|
getInstance() {
|
|
|
|
const mailgunConfig = this.#getConfig();
|
|
|
|
if (!mailgunConfig) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-15 13:52:29 +03:00
|
|
|
const formData = require('form-data');
|
|
|
|
const Mailgun = require('mailgun.js');
|
|
|
|
|
2022-08-10 13:16:55 +03:00
|
|
|
const baseUrl = new URL(mailgunConfig.baseUrl);
|
2022-08-15 13:52:29 +03:00
|
|
|
const mailgun = new Mailgun(formData);
|
2022-08-10 13:16:55 +03:00
|
|
|
|
2022-08-15 13:52:29 +03:00
|
|
|
return mailgun.client({
|
|
|
|
username: 'api',
|
|
|
|
key: mailgunConfig.apiKey,
|
2022-08-24 10:13:13 +03:00
|
|
|
url: baseUrl.origin,
|
|
|
|
timeout: 60000
|
2022-08-10 13:16:55 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the Mailgun instance is configured via config/settings
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isConfigured() {
|
|
|
|
const instance = this.getInstance();
|
|
|
|
return !!instance;
|
|
|
|
}
|
|
|
|
};
|