-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (43 loc) · 1.6 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
export default {
async email(message, env, ctx) {
// List of email addresses that are allowed to receive emails
// and the addresses to which the emails should be forwarded.
//
// The key is the recipient address and the value is an address
// or an array of addresses to forward the message to.
const forwardList = {
"[email protected]": [
],
};
// Extracts the email address without subaddressing,
// e.g. [email protected] → [email protected]
const extractAddress = (email) => {
const [localPart, domain] = email.split("@");
const cleanedLocalPart = localPart.split("+")[0];
return `${cleanedLocalPart}@${domain}`;
};
// Checks if the email address is allowed
const isAllowed = (email) => {
const cleanedEmail = extractAddress(email);
return Object.keys(forwardList).includes(cleanedEmail);
};
// Forwards the message to an address or multiple addresses
const forward = async (addresses) => {
const addressArray = Array.isArray(addresses) ? addresses : [addresses];
for (const address of addressArray) {
await message.forward(address);
}
};
// Reject the message sent to an unknown address
if (!isAllowed(message.to)) {
message.setReject(`Address \`${message.to}\` doesn't exist`);
return;
}
// Forward the message
const addresses = forwardList[extractAddress(message.to)];
await forward(addresses);
},
};