2018-05-31 14:29:00 -05:00
|
|
|
'use strict';
|
|
|
|
|
2022-11-24 18:03:31 +01:00
|
|
|
const { pick } = require('lodash/fp');
|
2021-10-20 17:30:05 +02:00
|
|
|
const { ApplicationError } = require('@strapi/utils').errors;
|
2021-03-05 10:37:33 +01:00
|
|
|
|
2018-05-31 14:29:00 -05:00
|
|
|
/**
|
|
|
|
* Email.js controller
|
|
|
|
*
|
|
|
|
* @description: A set of functions called "actions" of the `email` plugin.
|
|
|
|
*/
|
|
|
|
module.exports = {
|
2021-03-05 10:37:33 +01:00
|
|
|
async send(ctx) {
|
2022-08-08 15:50:34 +02:00
|
|
|
const options = ctx.request.body;
|
2021-09-03 23:50:47 +02:00
|
|
|
|
2020-06-09 12:11:25 +02:00
|
|
|
try {
|
2022-08-08 23:33:39 +02:00
|
|
|
await strapi.plugin('email').service('email').send(options);
|
2020-06-09 12:11:25 +02:00
|
|
|
} catch (e) {
|
|
|
|
if (e.statusCode === 400) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ApplicationError(e.message);
|
2020-06-09 12:11:25 +02:00
|
|
|
} else {
|
|
|
|
throw new Error(`Couldn't send email: ${e.message}.`);
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 14:29:00 -05:00
|
|
|
|
|
|
|
// Send 200 `ok`
|
|
|
|
ctx.send({});
|
|
|
|
},
|
2021-03-05 10:37:33 +01:00
|
|
|
|
|
|
|
async test(ctx) {
|
|
|
|
const { to } = ctx.request.body;
|
|
|
|
|
2022-11-24 17:52:25 +01:00
|
|
|
if (!to) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ApplicationError('No recipient(s) are given');
|
2021-03-05 10:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const email = {
|
2021-09-13 12:03:12 +02:00
|
|
|
to,
|
2021-03-05 10:37:33 +01:00
|
|
|
subject: `Strapi test mail to: ${to}`,
|
2021-08-19 22:27:00 +02:00
|
|
|
text: `Great! You have correctly configured the Strapi email plugin with the ${strapi.config.get(
|
|
|
|
'plugin.email.provider'
|
2021-11-29 07:01:25 -08:00
|
|
|
)} provider. \r\nFor documentation on how to use the email plugin checkout: https://docs.strapi.io/developer-docs/latest/plugins/email.html`,
|
2021-03-05 10:37:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2022-08-08 23:33:39 +02:00
|
|
|
await strapi.plugin('email').service('email').send(email);
|
2021-03-05 10:37:33 +01:00
|
|
|
} catch (e) {
|
|
|
|
if (e.statusCode === 400) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ApplicationError(e.message);
|
2021-03-05 10:37:33 +01:00
|
|
|
} else {
|
|
|
|
throw new Error(`Couldn't send test email: ${e.message}.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send 200 `ok`
|
|
|
|
ctx.send({});
|
|
|
|
},
|
|
|
|
|
|
|
|
async getSettings(ctx) {
|
2022-08-08 23:33:39 +02:00
|
|
|
const config = strapi.plugin('email').service('email').getProviderSettings();
|
2021-03-05 10:37:33 +01:00
|
|
|
|
|
|
|
ctx.send({
|
|
|
|
config: pick(
|
|
|
|
['provider', 'settings.defaultFrom', 'settings.defaultReplyTo', 'settings.testAddress'],
|
|
|
|
config
|
|
|
|
),
|
|
|
|
});
|
|
|
|
},
|
2018-05-31 14:29:00 -05:00
|
|
|
};
|