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
// This val creates a mailing list manager using blob storage for persistence.
// It provides functions to add and remove emails, and to send emails to all subscribers.
// The email list is stored as a JSON array in a blob.
import { blob } from "https://esm.town/v/std/blob";
import { email } from "https://esm.town/v/std/email";
const BLOB_KEY = new URL(import.meta.url).pathname.split("/").at(-1) + "_email_list";
async function getEmailList(): Promise<string[]> {
return await blob.getJSON(BLOB_KEY) || [];
}
async function saveEmailList(list: string[]): Promise<void> {
await blob.setJSON(BLOB_KEY, list);
}
async function addEmail(newEmail: string): Promise<string> {
const list = await getEmailList();
if (!list.includes(newEmail)) {
list.push(newEmail);
await saveEmailList(list);
return `Email ${newEmail} added successfully.`;
}
return `Email ${newEmail} already exists in the list.`;
}
async function removeEmail(emailToRemove: string): Promise<string> {
const list = await getEmailList();
const updatedList = list.filter(email => email !== emailToRemove);
if (list.length !== updatedList.length) {
await saveEmailList(updatedList);
return `Email ${emailToRemove} removed successfully.`;
}
return `Email ${emailToRemove} not found in the list.`;
}
async function sendEmailToAll(subject: string, content: string): Promise<string> {
const list = await getEmailList();
if (list.length === 0) {
return "No subscribers in the list.";
}
for (const recipient of list) {
await email({
// to: recipient,
subject: subject,
text: content,
html: `<p>${content}</p>`
});
}
return `Email sent to ${list.length} subscriber(s).`;
}
export default async function server(req: Request): Promise<Response> {
const url = new URL(req.url);
const path = url.pathname;
if (req.method === "POST") {
const formData = await req.formData();
const action = formData.get("action");
const emailAddress = formData.get("email") as string;
switch (action) {
case "add":
return new Response(await addEmail(emailAddress));
case "remove":
return new Response(await removeEmail(emailAddress));
case "send":
const subject = formData.get("subject") as string;
const content = formData.get("content") as string;
return new Response(await sendEmailToAll(subject, content));
default:
return new Response("Invalid action", { status: 400 });
}
}
// Default: show the management interface
const emailList = await getEmailList();
return new Response(`
<html>
<head>
<title>Mailing List Manager</title>
<style>${css}</style>
<script>
// Example of a JavaScript fetch request for addEmail
// fetch('/your-val-url', {
// method: 'POST',
// body: new FormData(document.querySelector('form[action="add"]'))
// }).then(response => response.text()).then(result => console.log(result));
// Example of a JavaScript fetch request for removeEmail
// fetch('/your-val-url', {
// method: 'POST',
// body: new FormData(document.querySelector('form[action="remove"]'))
// }).then(response => response.text()).then(result => console.log(result));
function exportCSV() {
const emails = ${JSON.stringify(emailList)};