2017-11-16 16:58:45 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-29 11:27:18 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2021-03-05 10:37:33 +01:00
|
|
|
const getProviderSettings = () => {
|
2021-07-29 16:39:26 +02:00
|
|
|
return strapi.config.get('plugins.email');
|
2018-05-31 14:29:00 -05:00
|
|
|
};
|
2017-11-16 16:58:45 +01:00
|
|
|
|
2020-06-29 11:27:18 +02:00
|
|
|
const send = async options => {
|
|
|
|
return strapi.plugins.email.provider.send(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fill subject, text and html using lodash template
|
|
|
|
* @param {object} emailOptions - to, from and replyto...
|
|
|
|
* @param {object} emailTemplate - object containing attributes to fill
|
|
|
|
* @param {object} data - data used to fill the template
|
|
|
|
* @returns {{ subject, text, subject }}
|
|
|
|
*/
|
|
|
|
const sendTemplatedEmail = (emailOptions = {}, emailTemplate = {}, data = {}) => {
|
|
|
|
const attributes = ['subject', 'text', 'html'];
|
|
|
|
const missingAttributes = _.difference(attributes, Object.keys(emailTemplate));
|
|
|
|
if (missingAttributes.length > 0) {
|
|
|
|
throw new Error(
|
|
|
|
`Following attributes are missing from your email template : ${missingAttributes.join(', ')}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-29 14:36:52 +02:00
|
|
|
const templatedAttributes = attributes.reduce(
|
|
|
|
(compiled, attribute) =>
|
|
|
|
emailTemplate[attribute]
|
|
|
|
? Object.assign(compiled, { [attribute]: _.template(emailTemplate[attribute])(data) })
|
|
|
|
: compiled,
|
|
|
|
{}
|
|
|
|
);
|
2020-06-29 11:27:18 +02:00
|
|
|
|
|
|
|
return strapi.plugins.email.provider.send({ ...emailOptions, ...templatedAttributes });
|
|
|
|
};
|
|
|
|
|
2021-07-29 16:39:26 +02:00
|
|
|
module.exports = () => ({
|
2021-03-05 10:37:33 +01:00
|
|
|
getProviderSettings,
|
2020-06-29 11:27:18 +02:00
|
|
|
send,
|
|
|
|
sendTemplatedEmail,
|
2021-07-29 16:39:26 +02:00
|
|
|
});
|