2017-11-16 16:58:45 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Email.js service
|
|
|
|
*
|
|
|
|
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2018-05-23 00:18:39 -05:00
|
|
|
|
2018-05-31 14:29:00 -05:00
|
|
|
const createDefaultEnvConfig = async (env) => {
|
|
|
|
const pluginStore = strapi.store({
|
2018-08-23 18:28:13 +02:00
|
|
|
environment: env,
|
|
|
|
type: 'plugin',
|
2018-05-31 14:29:00 -05:00
|
|
|
name: 'email'
|
2018-05-23 00:18:39 -05:00
|
|
|
});
|
|
|
|
|
2018-08-23 18:28:13 +02:00
|
|
|
const provider = _.find(strapi.plugins.email.config.providers, {provider: 'sendmail'});
|
|
|
|
const value = _.assign({}, provider, {});
|
|
|
|
|
|
|
|
await pluginStore.set({key: 'provider', value});
|
2018-05-31 14:29:00 -05:00
|
|
|
return await strapi.store({
|
2018-08-23 18:28:13 +02:00
|
|
|
environment: env,
|
|
|
|
type: 'plugin',
|
2018-05-31 14:29:00 -05:00
|
|
|
name: 'email'
|
2018-08-23 18:28:13 +02:00
|
|
|
}).get({key: 'provider'});
|
2018-05-31 14:29:00 -05:00
|
|
|
};
|
2018-05-23 00:18:39 -05:00
|
|
|
|
2018-05-31 14:29:00 -05:00
|
|
|
const getProviderConfig = async (env) => {
|
|
|
|
let config = await strapi.store({
|
|
|
|
environment: env,
|
|
|
|
type: 'plugin',
|
|
|
|
name: 'email'
|
|
|
|
}).get({key: 'provider'});
|
2018-05-23 00:18:39 -05:00
|
|
|
|
2018-05-31 14:29:00 -05:00
|
|
|
if(!config) {
|
2018-08-23 18:28:13 +02:00
|
|
|
config = await createDefaultEnvConfig(env);
|
2018-05-31 14:29:00 -05:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:28:13 +02:00
|
|
|
return config;
|
2018-05-31 14:29:00 -05:00
|
|
|
};
|
2017-11-16 16:58:45 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2018-08-23 18:28:13 +02:00
|
|
|
getProviderConfig,
|
2018-05-31 14:29:00 -05:00
|
|
|
send: async (options, config, cb) => {
|
|
|
|
// Get email provider settings to configure the provider to use.
|
|
|
|
if(!config) {
|
2018-08-23 18:28:13 +02:00
|
|
|
config = await getProviderConfig(strapi.config.environment);
|
2018-05-31 14:29:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const provider = _.find(strapi.plugins.email.config.providers, { provider: config.provider });
|
|
|
|
|
|
|
|
if (!provider) {
|
2018-10-19 09:26:09 +02:00
|
|
|
throw new Error(`The provider package isn't installed. Please run \`npm install strapi-provider-email-${config.provider}\``);
|
2018-05-31 14:29:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const actions = provider.init(config);
|
2017-11-16 16:58:45 +01:00
|
|
|
|
2018-05-31 14:29:00 -05:00
|
|
|
// Execute email function of the provider for all files.
|
2018-08-23 18:28:13 +02:00
|
|
|
return actions.send(options, cb);
|
2017-11-16 16:58:45 +01:00
|
|
|
}
|
|
|
|
};
|