-
Notifications
You must be signed in to change notification settings - Fork 114
/
contact.js
65 lines (58 loc) · 1.26 KB
/
contact.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
const sendMail = require('sendmail')()
const { validateEmail, validateLength } = require('../utils/validations')
exports.handler = (event, context, callback) => {
if (!process.env.CONTACT_EMAIL) {
return callback(null, {
statusCode: 500,
body: 'process.env.CONTACT_EMAIL must be defined',
})
}
const body = JSON.parse(event.body)
try {
validateLength('body.name', body.name, 3, 50)
}
catch (e) {
return callback(null, {
statusCode: 403,
body: e.message
})
}
try {
validateEmail('body.email', body.email)
}
catch (e) {
return callback(null, {
statusCode: 403,
body: e.message
})
}
try {
validateLength('body.details', body.details, 10, 1000)
}
catch (e) {
return callback(null, {
statusCode: 403,
body: e.message
})
}
const descriptor = {
from: `"${body.email}" <[email protected]>`,
to: process.env.CONTACT_EMAIL,
subject: `${body.name} sent you a message from gql-modules.com`,
text: body.details,
}
sendMail(descriptor, (e) => {
if (e) {
callback(null, {
statusCode: 500,
body: e.message
})
}
else {
callback(null, {
statusCode: 200,
body: '',
})
}
})
}