2018-05-31 14:29:00 -05:00
'use strict' ;
2021-03-05 10:37:33 +01:00
const { isNil , pick } = require ( 'lodash/fp' ) ;
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 ) {
2018-09-18 13:20:55 +02:00
let options = ctx . request . body ;
2020-06-09 12:11:25 +02:00
try {
await strapi . plugins . email . services . email . send ( options ) ;
} catch ( e ) {
if ( e . statusCode === 400 ) {
return ctx . badRequest ( e . message ) ;
} 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 ;
if ( isNil ( to ) ) {
throw strapi . errors . badRequest ( null , {
errors : [ { id : 'Email.to.empty' , message : 'No recipient(s) are given' } ] ,
} ) ;
}
const email = {
to : to ,
subject : ` Strapi test mail to: ${ to } ` ,
2021-03-09 01:13:15 -07:00
text : ` Great! You have correctly configured the Strapi email plugin with the ${ strapi . plugins . email . config . provider } provider. \r \n For documentation on how to use the email plugin checkout: https://strapi.io/documentation/developer-docs/latest/development/plugins/email.html ` ,
2021-03-05 10:37:33 +01:00
} ;
try {
await strapi . plugins . email . services . email . send ( email ) ;
} catch ( e ) {
if ( e . statusCode === 400 ) {
return ctx . badRequest ( e . message ) ;
} else {
throw new Error ( ` Couldn't send test email: ${ e . message } . ` ) ;
}
}
// Send 200 `ok`
ctx . send ( { } ) ;
} ,
async getSettings ( ctx ) {
const config = strapi . plugins . email . services . email . getProviderSettings ( ) ;
ctx . send ( {
config : pick (
[ 'provider' , 'settings.defaultFrom' , 'settings.defaultReplyTo' , 'settings.testAddress' ] ,
config
) ,
} ) ;
} ,
2018-05-31 14:29:00 -05:00
} ;