forked from jameskirkby/serverless-contact-form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
180 lines (132 loc) · 3.65 KB
/
Copy pathhandler.js
File metadata and controls
180 lines (132 loc) · 3.65 KB
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
'use strict';
// load the AWS SDK
const AWS = require('aws-sdk');
// I was getting an error saying that the region wasn't set, so this fixes it
if (!AWS.config.region) {
AWS.config.update({
region: 'eu-west-1'
});
}
// load ses, for sending emails
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 {
// 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;
}
// 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;
}
// 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;
}
// 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();
// Send the email via SES.
ses.sendEmail({
Destination: {
ToAddresses: [
'YOUR NAME <[email protected]>'
]
},
Message: {
Body: {
Text: {
Data: message,
Charset: 'UTF-8'
}
},
Subject: {
Data: subject,
Charset: 'UTF-8'
}
},
Source: "Contact Form <[email protected]>",
ReplyToAddresses: [
replyTo
]
}, (err, data) => {
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 {
if(event.body.redirectUrl) {
// if a redirect URL has been passed, redirect to that URL
callback(null, {
statusCode: 302,
headers: {
'Location': event.body.redirectUrl
}
});
} else {
callback(null, {
statusCode: 200,
body: 'success'
});
}
}
});
};