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
import { email as sendEmail } from "https://esm.town/v/std/email?v=11";
import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
export async function unsubscribe(req: Request) {
const searchParams = new URL(req.url).searchParams;
const emailAddress = searchParams.get("email");
if (!emailAddress) {
// No-op if email query param is missing
return Response.json(
"Email address missing in unsubscribe URL. If you tried to unsubscribe and are still getting emails, send me a note at pete@petemillspaugh.com",
);
}
await sqlite.execute({
sql: `
UPDATE subscribers
SET subscribed_at = NULL
WHERE email = ?
`,
args: [emailAddress],
});
sendEmail({
subject: `Someone unsubscribed from petemillspaugh.com clippings`,
text: `${emailAddress} unsubscribed 😢`,
});
const responseHtml = `
<main>
<p>You've successfully unsubscribed.</p>
<p>Take care, Pete</p>
</main>
`;
return new Response(responseHtml, { headers: { "Content-Type": "text/html" } });
}