55 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 = {
2022-08-08 23:33:39 +02:00
apiKey: { field: 'key', fn: (value) => value },
host: { field: 'url', fn: (value) => `https://${value || 'api.mailgun.net'}` },
};
module.exports = {
convertProviderOptions(providerOptions = {}) {
const newOptions = {};
if (typeof providerOptions === 'object') {
2022-08-08 23:33:39 +02:00
Object.keys(providerOptions).forEach((key) => {
if (Object.keys(optionsMap).includes(key)) {
newOptions[optionsMap[key].field] = optionsMap[key].fn(providerOptions[key]);
} else {
newOptions[key] = providerOptions[key];
}
});
}
return newOptions;
},
init(providerOptions = {}, settings = {}) {
const defaults = {
username: 'api',
};
const mailgun = new Mailgun(formData);
const mg = mailgun.client({ ...defaults, ...this.convertProviderOptions(providerOptions) });
return {
send(options) {
2022-05-30 12:49:10 -04:00
const { from, to, cc, bcc, replyTo, subject, text, html, ...rest } = options;
2022-08-08 15:50:34 +02:00
const data = {
2022-05-30 12:49:10 -04:00
from: from || settings.defaultFrom,
to,
cc,
bcc,
'h:Reply-To': replyTo || settings.defaultReplyTo,
subject,
text,
html,
...rest,
};
2022-05-30 12:49:10 -04:00
return mg.messages.create(providerOptions.domain, removeUndefined(data));
2019-09-27 16:26:09 +02:00
},
};
2019-09-27 16:26:09 +02:00
},
};