mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Wired mailgun provider API keys via config or settings
no issue
This commit is contained in:
parent
05e9f01f30
commit
d3229b6ade
3 changed files with 51 additions and 32 deletions
|
@ -143,7 +143,7 @@ const settings = (attrs) => {
|
|||
if (_.isArray(attrs)) {
|
||||
attrs.forEach((attr) => {
|
||||
if (attr.key === 'bulk_email_settings') {
|
||||
let {provider, apiKey, domain, baseUrl} = attr.value ? JSON.parse(attr.value) : {};
|
||||
const {provider, apiKey, domain, baseUrl} = attr.value ? JSON.parse(attr.value) : {};
|
||||
|
||||
const bulkEmailConfig = config.get('bulkEmail');
|
||||
const hasMailgunConfig = !!(bulkEmailConfig && bulkEmailConfig.mailgun);
|
||||
|
|
|
@ -1,34 +1,8 @@
|
|||
const {URL} = require('url');
|
||||
const _ = require('lodash');
|
||||
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`);
|
||||
}
|
||||
}
|
||||
const mailgunProvider = require('./mailgun');
|
||||
const configService = require('../../config');
|
||||
const mailgunInstance = mailgunProvider.getInstance();
|
||||
|
||||
/**
|
||||
* An email address
|
||||
|
@ -76,10 +50,11 @@ module.exports = {
|
|||
from: fromAddress,
|
||||
'recipient-variables': recipientVariables
|
||||
});
|
||||
const bulkEmailConfig = configService.get('bulkEmail');
|
||||
|
||||
if (config.mailgun.tag) {
|
||||
if (bulkEmailConfig && bulkEmailConfig.mailgun && bulkEmailConfig.mailgun.tag) {
|
||||
Object.assign(messageData, {
|
||||
'o:tag': config.mailgun.tag
|
||||
'o:tag': bulkEmailConfig.mailgun.tag
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -96,6 +71,10 @@ module.exports = {
|
|||
'message-id': messageId
|
||||
};
|
||||
|
||||
if (!mailgunInstance) {
|
||||
return;
|
||||
}
|
||||
|
||||
return await mailgunInstance.events().get(filter);
|
||||
} catch (err) {
|
||||
common.logging.error({err});
|
||||
|
|
40
core/server/services/bulk-email/mailgun.js
Normal file
40
core/server/services/bulk-email/mailgun.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
const {URL} = require('url');
|
||||
const mailgun = require('mailgun-js');
|
||||
const common = require('../../lib/common');
|
||||
const configService = require('../../config');
|
||||
const settingsCache = require('../settings/cache');
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
function getInstance() {
|
||||
const bulkEmailConfig = configService.get('bulkEmail');
|
||||
const bulkEmailSetting = settingsCache.get('bulk_email_settings');
|
||||
const hasMailgunConfig = !!(bulkEmailConfig && bulkEmailConfig.mailgun);
|
||||
const hasMailgunSetting = !!(bulkEmailSetting && bulkEmailSetting.apiKey && bulkEmailSetting.baseUrl && bulkEmailSetting.domain);
|
||||
if (!hasMailgunConfig && !hasMailgunSetting) {
|
||||
common.logging.warn(`Bulk email service is not configured`);
|
||||
} else {
|
||||
try {
|
||||
let mailgunConfig = hasMailgunConfig ? bulkEmailConfig.mailgun : bulkEmailSetting;
|
||||
return createMailgun(mailgunConfig);
|
||||
} catch (err) {
|
||||
common.logging.warn(`Bulk email service is not configured`);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getInstance: getInstance
|
||||
};
|
Loading…
Add table
Reference in a new issue