56 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
const formData = require('form-data');
const Mailgun = require('mailgun.js');
2021-04-29 13:51:12 +02:00
const { removeUndefined } = require('@strapi/utils');
2018-06-11 16:55:04 +02:00
const optionsMap = {
apiKey: { field: 'key', fn: value => value },
domain: { field: 'domain', fn: value => value },
host: { field: 'url', fn: value => `https://${value || 'api.mailgun.net'}` },
};
const convertProviderOptions = (providerOptions = {}) => {
const newOptions = {};
if (typeof providerOptions === 'object') {
Object.keys(providerOptions).forEach(key => {
if (Object.keys(optionsMap).includes(key)) {
newOptions[optionsMap[key].field] = optionsMap[key].fn(providerOptions[key]);
}
});
}
return newOptions;
};
module.exports = {
init(providerOptions = {}, settings = {}) {
const mailgun = new Mailgun(formData);
const mg = mailgun.client({ username: 'api', ...convertProviderOptions(providerOptions) });
return {
send(options) {
return new Promise((resolve, reject) => {
const { from, to, cc, bcc, replyTo, subject, text, html, ...rest } = options;
let data = {
from: from || settings.defaultFrom,
to,
cc,
bcc,
'h:Reply-To': replyTo || settings.defaultReplyTo,
subject,
text,
html,
...rest,
2018-06-11 16:55:04 +02:00
};
mg.messages
.create(providerOptions.domain, removeUndefined(data))
.then(() => resolve())
.catch(err => reject(err));
});
2019-09-27 16:26:09 +02:00
},
};
2019-09-27 16:26:09 +02:00
},
};