-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (50 loc) · 1.34 KB
/
index.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
import SES from "aws-sdk/clients/ses";
import parse from "parse-email";
const ses = new SES({ region: process.env.REGION || "us-east-1" });
const send = async ({ to, from, email }) => {
const params = {
Destination: {
ToAddresses: Array.isArray(to) ? to : [to],
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: email.html,
},
Text: {
Charset: "UTF-8",
Data: email.text,
},
},
Subject: {
Charset: "UTF-8",
Data: email.subject,
},
},
Source: from,
ReplyToAddresses: [email.from.value[0].address || email.from.text],
};
return ses.sendEmail(params).promise();
};
export const handler = async (event) => {
const { Message } = event.Records[0].Sns;
const msg = JSON.parse(Message);
console.log("Message:", JSON.stringify(msg));
let email = null;
try {
email = await parse(msg.content);
} catch (error) {
console.log("Message:", JSON.stringify(error));
return;
}
const proms = [];
const { DESTINATION, FROM } = process.env;
const destination = JSON.parse(DESTINATION);
email.to.value.forEach((e) => {
const to = destination[e.address] || destination["default"];
const prom = send({ to, from: e.address || FROM, email });
proms.push(prom);
});
await Promise.all(proms);
};