1
1
const axios = require ( 'axios' ) ;
2
2
const validator = require ( 'validator' ) ;
3
- const nodemailer = require ( 'nodemailer' ) ;
3
+ const nodemailerConfig = require ( '../config/nodemailer' ) ;
4
+
5
+ async function validateReCAPTCHA ( token ) {
6
+ const response = await axios . post ( `https://www.google.com/recaptcha/api/siteverify?secret=${ process . env . RECAPTCHA_SECRET_KEY } &response=${ token } ` ,
7
+ { } ,
8
+ {
9
+ headers : { 'Content-Type' : 'application/x-www-form-urlencoded; charset=utf-8' } ,
10
+ } ) ;
11
+ return response ;
12
+ }
4
13
5
14
/**
6
15
* GET /contact
@@ -20,7 +29,7 @@ exports.getContact = (req, res) => {
20
29
* POST /contact
21
30
* Send a contact form via Nodemailer.
22
31
*/
23
- exports . postContact = async ( req , res ) => {
32
+ exports . postContact = async ( req , res , next ) => {
24
33
const validationErrors = [ ] ;
25
34
let fromName ;
26
35
let fromEmail ;
@@ -30,81 +39,54 @@ exports.postContact = async (req, res) => {
30
39
}
31
40
if ( validator . isEmpty ( req . body . message ) ) validationErrors . push ( { msg : 'Please enter your message.' } ) ;
32
41
33
- function getValidateReCAPTCHA ( token ) {
34
- return axios . post ( `https://www.google.com/recaptcha/api/siteverify?secret=${ process . env . RECAPTCHA_SECRET_KEY } &response=${ token } ` ,
35
- { } ,
36
- {
37
- headers : { 'Content-Type' : 'application/x-www-form-urlencoded; charset=utf-8' } ,
38
- } ) ;
39
- }
40
-
41
42
try {
42
- const validateReCAPTCHA = await getValidateReCAPTCHA ( req . body [ 'g-recaptcha-response' ] ) ;
43
- if ( ! validateReCAPTCHA . data . success ) {
43
+ const reCAPTCHAResponse = await validateReCAPTCHA ( req . body [ 'g-recaptcha-response' ] ) ;
44
+ if ( ! reCAPTCHAResponse . data . success ) {
44
45
validationErrors . push ( { msg : 'reCAPTCHA validation failed.' } ) ;
45
46
}
47
+ } catch ( error ) {
48
+ console . error ( 'Error validating reCAPTCHA:' , error ) ;
49
+ validationErrors . push ( { msg : 'Error validating reCAPTCHA. Please try again.' } ) ;
50
+ }
46
51
47
- if ( validationErrors . length ) {
48
- req . flash ( 'errors' , validationErrors ) ;
49
- return res . redirect ( '/contact' ) ;
50
- }
51
-
52
- if ( ! req . user ) {
53
- fromName = req . body . name ;
54
- fromEmail = req . body . email ;
55
- } else {
56
- fromName = req . user . profile . name || '' ;
57
- fromEmail = req . user . email ;
58
- }
59
-
60
- const transportConfig = {
61
- host : process . env . SMTP_HOST ,
62
- port : 465 ,
63
- secure : true ,
64
- auth : {
65
- user : process . env . SMTP_USER ,
66
- pass : process . env . SMTP_PASSWORD
67
- }
68
- } ;
52
+ if ( validationErrors . length ) {
53
+ req . flash ( 'errors' , validationErrors ) ;
54
+ return res . redirect ( '/contact' ) ;
55
+ }
69
56
70
- let transporter = nodemailer . createTransport ( transportConfig ) ;
57
+ if ( ! req . user ) {
58
+ fromName = req . body . name ;
59
+ fromEmail = req . body . email ;
60
+ } else {
61
+ fromName = req . user . profile . name || '' ;
62
+ fromEmail = req . user . email ;
63
+ }
71
64
65
+ const sendContactEmail = async ( ) => {
72
66
const mailOptions = {
73
67
to : process . env . SITE_CONTACT_EMAIL ,
74
68
from : `${ fromName } <${ fromEmail } >` ,
75
69
subject : 'Contact Form | Hackathon Starter' ,
76
70
text : req . body . message
77
71
} ;
78
72
79
- return transporter . sendMail ( mailOptions )
80
- . then ( ( ) => {
81
- req . flash ( 'success' , { msg : 'Email has been sent successfully!' } ) ;
82
- res . redirect ( '/contact' ) ;
83
- } )
84
- . catch ( ( err ) => {
85
- if ( err . message === 'self signed certificate in certificate chain' ) {
86
- console . log ( 'WARNING: Self signed certificate in certificate chain. Retrying with the self signed certificate. Use a valid certificate if in production.' ) ;
87
- transportConfig . tls = transportConfig . tls || { } ;
88
- transportConfig . tls . rejectUnauthorized = false ;
89
- transporter = nodemailer . createTransport ( transportConfig ) ;
90
- return transporter . sendMail ( mailOptions ) ;
91
- }
92
- console . log ( 'ERROR: Could not send contact email after security downgrade.\n' , err ) ;
93
- req . flash ( 'errors' , { msg : 'Error sending the message. Please try again shortly.' } ) ;
94
- return false ;
95
- } )
96
- . then ( ( result ) => {
97
- if ( result ) {
98
- req . flash ( 'success' , { msg : 'Email has been sent successfully!' } ) ;
99
- return res . redirect ( '/contact' ) ;
100
- }
101
- } )
102
- . catch ( ( err ) => {
103
- console . log ( 'ERROR: Could not send contact email.\n' , err ) ;
104
- req . flash ( 'errors' , { msg : 'Error sending the message. Please try again shortly.' } ) ;
105
- return res . redirect ( '/contact' ) ;
106
- } ) ;
107
- } catch ( err ) {
108
- console . log ( err ) ;
73
+ const mailSettings = {
74
+ successfulType : 'info' ,
75
+ successfulMsg : 'Email has been sent successfully!' ,
76
+ loggingError : 'ERROR: Could not send contact email after security downgrade.\n' ,
77
+ errorType : 'errors' ,
78
+ errorMsg : 'Error sending the message. Please try again shortly.' ,
79
+ mailOptions,
80
+ req
81
+ } ;
82
+
83
+ return nodemailerConfig . sendMail ( mailSettings ) ;
84
+ } ;
85
+
86
+ try {
87
+ await sendContactEmail ( ) ;
88
+ res . redirect ( '/contact' ) ;
89
+ } catch ( error ) {
90
+ next ( error ) ;
109
91
}
110
92
} ;
0 commit comments