| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2021-09-03 23:50:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-09 12:11:25 +02:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2021-08-19 22:27:00 +02:00
										 |  |  |       await strapi | 
					
						
							|  |  |  |         .plugin('email') | 
					
						
							|  |  |  |         .service('email') | 
					
						
							|  |  |  |         .send(options); | 
					
						
							| 
									
										
										
										
											2020-06-09 12:11:25 +02:00
										 |  |  |     } 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 = { | 
					
						
							| 
									
										
										
										
											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' | 
					
						
							|  |  |  |       )} provider. \r\nFor 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 { | 
					
						
							| 
									
										
										
										
											2021-08-19 22:27:00 +02:00
										 |  |  |       await strapi | 
					
						
							|  |  |  |         .plugin('email') | 
					
						
							|  |  |  |         .service('email') | 
					
						
							|  |  |  |         .send(email); | 
					
						
							| 
									
										
										
										
											2021-03-05 10:37:33 +01:00
										 |  |  |     } 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) { | 
					
						
							| 
									
										
										
										
											2021-08-19 22:27:00 +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
										 |  |  | }; |