import { fetch } from "https://esm.town/v/std/fetch";
import { emailWebhookMap } from "https://esm.town/v/neverstew/emailWebhookMap";
export async function valtownForwarder(e: {
from: string;
to: string[];
subject: string;
text: string;
html: string;
}) {
const destinations = emailWebhookMap[e.from];
if (!destinations || destinations.length === 0)
return;
await Promise.all(destinations.map((destination) => {
return new Promise((resolve) => {
const timeout = setTimeout(() => {
console.error(`Timed out for ${destination}`);
resolve(false);
}, 3000);
fetch(destination).then((x) => {
console.log(`Delivered to ${destination}`);
}).catch((x) => {
console.log(`Delivery failed to ${destination}`);
})
.finally(() => {
clearTimeout(timeout);
resolve(true);
});
});
}));
}