-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
120 lines (107 loc) · 3.07 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
"use strict";
const AWS = require("aws-sdk");
const SES = new AWS.SES({ region: "eu-central-1" });
const s3 = new AWS.S3({ region: "eu-central-1" });
const getObject = (filename) => {
return new Promise((resolve, reject) => {
s3.getObject(
{
Bucket: process.env.AWS_S3BUCKET_NAME,
Key: filename,
},
(err, data) => {
if (err) reject(err);
else resolve(data);
}
);
});
};
const getEmailConfig = async (origin) => {
try {
const data = await getObject("config.json");
var mailConfigData = JSON.parse(data.Body.toString("utf8"));
const mailConfig = {
mailto: mailConfigData[origin].mailto,
subject: mailConfigData[origin].subject,
};
return mailConfig;
} catch (err) {
console.error(err);
}
};
const getAllowedOrigins = async () => {
try {
const data = await getObject("allowedOrigins.json");
const allowedOrigins = JSON.parse(data.Body.toString("utf8"));
//Return only the array
return allowedOrigins.allowedOrigins;
} catch (err) {
console.error(err);
}
};
const sendMail = (emailParams, origin, allowedOrigins) => {
return new Promise((resolve, reject) => {
SES.sendEmail(emailParams)
.promise()
.then((data) => {
const response = {
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": allowedOrigins.includes(origin)
? origin
: allowedOrigins[0],
},
body: JSON.stringify(data),
isBase64Encoded: false,
};
resolve(response);
})
.catch((err) => {
const response = {
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(err),
isBase64Encoded: false,
};
reject(response);
});
});
};
function createEmailParams(formData, configData) {
const emailParams = {
Source: configData.mailto, // SES SENDING EMAIL
ReplyToAddresses: [formData.email],
Destination: {
ToAddresses: [configData.mailto], // SES RECEIVING EMAIL
},
Message: {
Body: {
Text: {
Charset: "UTF-8",
Data: `${formData.text}\n\nName: ${formData.name}\nEmail: ${formData.email}\nTelefon: ${formData.phone ? formData.phone : ""}`,
},
},
Subject: {
Charset: "UTF-8",
Data: `${configData.subject} ${formData.name}`,
},
},
};
return emailParams;
}
module.exports.thefloksterMailer = async (event) => {
const headerData = event.headers;
const formData = JSON.parse(event.body);
// Get config file from s3
const emailConfig = await getEmailConfig(headerData.origin);
// Parse it into SES parameter model.
const emailParams = createEmailParams(formData, emailConfig);
// Get allowed origins.
const allowedOrigins = await getAllowedOrigins();
// Send Mail and return promise
return sendMail(emailParams, headerData.origin, allowedOrigins)
};