-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
204 lines (176 loc) · 5.1 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
const request = require('request');
const config = require('./config.json');
const secretKey = config['captchaSecret'];
const myEmail = config['sendMailTo'];
const AWS = require('aws-sdk');
if (!AWS.config.region) {
AWS.config.update({
region: 'us-east-1'
});
}
const ses = new AWS.SES();
// function to convert query string to object
function QueryStringToObj(str) {
let obj = {};
str.replace(/([^=&]+)=([^&]*)/g, (m, key, value) => {
obj[decodeURIComponent(key)] = decodeURIComponent(value).replace(/\+/g, ' ');
});
return obj;
}
module.exports.processFormData = (event, context, callback) => {
// log the incoming data
console.log('Received event:', JSON.stringify(event, null, 2));
// check if form data has actually been sent
if(! event.body || event.body.trim() === '') {
callback(null, {
statusCode: 500,
body: 'Form data not sent'
});
return;
} else {
console.log(event.body);
// convert form fields to an object
event.body = QueryStringToObj(event.body);
}
// log form data object
console.log('Form Data:', JSON.stringify(event.body, null, 2));
// Check that the name has been sent, and that the name isn't empty
if (! event.body.name || event.body.name.trim() === '') {
callback(null, {
statusCode: 500,
body: 'Name is required.'
});
return;
}
function checkEmail(){
// check that the email has been sent
if (! event.body.email) {
callback(null, {
statusCode: 500,
body: 'Email address is required.'
});
return;
}
// setup an email regex
const email_regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// check the submitted email is valid
if (! email_regex.test(event.body.email)) {
callback(null, {
statusCode: 500,
body: 'The email not valid'
});
return;
}
}
function checkMessage(){
// check the message has been sent, and that it's not empty
if (! event.body.message || event.body.message.trim() === '') {
callback(null, {
statusCode: 500,
body: 'Message is required.'
});
return;
}
// basic spam check
if (event.body.message.indexOf('<a') !== -1) {
callback(null, {
statusCode: 500,
body: 'Spam detected.'
});
return;
}
if(event.body['g-recaptcha-response'] === undefined || event.body['g-recaptcha-response'] === '' || event.body['g-recaptcha-response'] === null) {
callback(null, {
statusCode: 500,
body: 'Please select captcha.'
});
return
}
}
function checkCaptcha(url) {
return new Promise(function (resolve, reject) {
request(url,function(error,response,body) {
body = JSON.parse(body);
// Success will be true or false depending upon captcha validation.
if (!error && response.statusCode == 200 && body.success == true){
console.log('good captcha');
resolve(true);
}else{
callback(null, {
statusCode: 500,
body: 'bad captcha'
});
return
}
});
});
}
// Hitting GET request to the URL, Google will respond with success or error scenario.
//var verify_captcha = await checkCaptcha(verificationUrl);
function sendMail(){
// Put together all info needed to send the email
const name = event.body.name.trim(),
email = unescape(event.body.email.trim()),
replyTo = event.body.name + " <" + email + ">",
subject = "Website message from " + name,
message = "Website message from " + name + " <" + email + ">\n\n" + event.body.message.trim();
console.log(name);
// Send the email via SES.
ses.sendEmail({
Destination: {
ToAddresses: [
'Alden Jenkins <' + myEmail + '>'
]
},
Message: {
Body: {
Text: {
Data: message,
Charset: 'UTF-8'
}
},
Subject: {
Data: subject,
Charset: 'UTF-8'
}
},
Source: "Contact Form <" + myEmail + ">",
ReplyToAddresses: [
replyTo
]
}, (err, data) => {
console.log('sending email');
if (err) {
// email was not sent
console.log('Error Sending Email:', JSON.stringify(err, null, 2));
callback(null, {
statusCode: 500,
body: 'Message could not be sent'
});
} else {
console.log('email sent')
if(event.body.redirectUrl) {
// if a redirect URL has been passed, redirect to that URL
console.log('redirecting to', event.body.redirectUrl)
callback(null, {
statusCode: 302,
headers: {
'Location': event.body.redirectUrl,
}
});
} else {
callback(null, {
statusCode: 200,
body: 'success'
});
}
}
});
}
(async () => {
var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + event.body['g-recaptcha-response']
await Promise.all([checkEmail(), checkMessage(), checkCaptcha(verificationUrl)]);
sendMail();
})()
}