1
1
// File /api/email/controllers/Email.js
2
- 'use strict'
2
+ 'use strict' ;
3
3
4
4
/**
5
5
* Read the documentation () to implement custom controller functions
@@ -10,22 +10,48 @@ module.exports = {
10
10
* Sends an email to the recipient in the body of the request
11
11
*/
12
12
send : async ( ctx ) => {
13
- const body = ctx . request . body
14
- const sendTo = body . email
15
- strapi . log . debug ( `Trying to send an email to ${ sendTo } ` )
13
+ // ensure request was not sent as formdata
14
+ if ( ctx . is ( 'multipart' ) )
15
+ return ctx . badRequest ( 'Multipart requests are not accepted!' , {
16
+ error : 'ValidationError' ,
17
+ } ) ;
18
+
19
+ // ensure the request has the right number of params
20
+ const params = Object . keys ( ctx . request . body ) . length ;
21
+ if ( params !== 5 )
22
+ return ctx . badRequest ( 'Invalid number of params!' , {
23
+ error : 'ValidationError' ,
24
+ } ) ;
25
+
26
+ // validate the request
27
+ const { description, steps, name, email, systemInfo } = ctx . request . body ;
28
+ if ( ! description || ! steps || ! name || ! email || ! systemInfo )
29
+ return ctx . badRequest (
30
+ 'A description, steps, name, email and systemInfo must be provided!' ,
31
+ { error : 'ValidationError' }
32
+ ) ;
16
33
17
34
try {
18
35
const emailOptions = {
19
- to : sendTo ,
20
- subject : 'This is a test' ,
21
- html : `<h1>Welcome!</h1><p>This is a test HTML email.</p>` ,
22
- }
23
- await strapi . plugins [ 'email' ] . services . email . send ( emailOptions )
24
- strapi . log . debug ( `Email sent to ${ sendTo } ` )
25
- ctx . send ( { message : 'Email sent' } )
36
+
37
+ subject : 'Bug Report' ,
38
+ html : `
39
+ <h3>Description of the bug: </h3>
40
+ <p>${ description } <p>
41
+ <h3>Steps to reproduce: </h3>
42
+ <p>${ steps } <p>
43
+ <h3>Reported by: </h3>
44
+ <p>${ name } <p> <a href='mailto: ${ email } '>(${ email } )</a>
45
+ <h3>Captured in: </h3>
46
+ <p>${ systemInfo } </p>
47
+ ` ,
48
+ } ;
49
+ await strapi . plugins [ 'email' ] . services . email . send ( emailOptions ) ;
50
+ strapi . log . debug ( `Email sent to [email protected] ` ) ;
51
+ ctx . send ( { message : 'Email sent' } ) ;
26
52
} catch ( err ) {
27
- strapi . log . error ( `Error sending email to ${ sendTo } ` , err )
28
- ctx . send ( { error : 'Error sending email' } )
53
+ strapi . log . error ( `Error sending email to [email protected] , ` , err ) ;
54
+ ctx . send ( { error : 'Error sending email' } ) ;
29
55
}
30
56
} ,
31
- }
57
+ } ;
0 commit comments