Austin Bratcher ddc01eb27b Update to add dynamic settings to email plugin
Email plugin now functions more like the upload plugin. Users can go to the plugins page, click on the settings cog for the email, and switch between providers or change settings. The default provider is strapi-email-sendmail. Extra providers can be added by installing strapi-email-mailgun or strapi-email-sendgrid.
2018-05-31 14:29:00 -05:00

63 lines
1.5 KiB
JavaScript

'use strict';
/**
* Email.js service
*
* @description: A set of functions similar to controller's actions to avoid code duplication.
*/
const _ = require('lodash');
const createDefaultEnvConfig = async (env) => {
const pluginStore = strapi.store({
environment: env,
type: 'plugin',
name: 'email'
});
const provider = _.find(strapi.plugins.email.config.providers, {provider: 'sendmail'});
const value = _.assign({}, provider, {});
await pluginStore.set({key: 'provider', value});
return await strapi.store({
environment: env,
type: 'plugin',
name: 'email'
}).get({key: 'provider'});
};
const getProviderConfig = async (env) => {
let config = await strapi.store({
environment: env,
type: 'plugin',
name: 'email'
}).get({key: 'provider'});
if(!config) {
config = await createDefaultEnvConfig(env);
}
return config;
};
module.exports = {
getProviderConfig,
send: async (options, config, cb) => {
// Get email provider settings to configure the provider to use.
if(!config) {
config = await getProviderConfig(strapi.config.environment);
}
const provider = _.find(strapi.plugins.email.config.providers, { provider: config.provider });
if (!provider) {
throw new Error(`The provider package isn't installed. Please run \`npm install strapi-email-${config.provider}\``);
}
const actions = provider.init(config);
// Execute email function of the provider for all files.
return actions.send(options, cb);
}
};