-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
61 lines (55 loc) · 1.58 KB
/
handler.js
File metadata and controls
61 lines (55 loc) · 1.58 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
'use strict';
const AWS = require('aws-sdk');
const SES = new AWS.SES();
const VALID_ORIGINS = require('./origins.json').origins;
function validateOrigin(testOrigin) {
return VALID_ORIGINS.filter(origin => origin === testOrigin)[0] || null;
}
function sendEmail(formData, callback) {
const emailParams = {
Source: formData.ses_address, // SES SENDING EMAIL
ReplyToAddresses: [formData.email],
Destination: {
ToAddresses: [formData.ses_address], // SES RECEIVING EMAIL
CcAddresses: [formData.email] // SES CC EMAIL
},
Message: {
Body: {
Text: {
Charset: 'UTF-8',
Data: `${formData.message}\n\nName: ${
formData.name
}\nCompany: ${formData.company || 'None'}\nEmail: ${
formData.email
}\nTel: ${formData.phone || 'None'}`
}
},
Subject: {
Charset: 'UTF-8',
Data: formData.subject
}
}
};
SES.sendEmail(emailParams, callback);
}
module.exports.sendMessageViaEmail = (event, context, callback) => {
const origin = validateOrigin(event.headers.Origin || event.headers.origin);
const formData = JSON.parse(event.body);
if (origin) {
sendEmail(formData, function(err, data) {
const response = {
statusCode: err ? 500 : 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
message: err ? err.message : data
})
};
callback(null, response);
});
} else {
callback('Unauthorised access');
}
};