Broadcast and Unsubscribe Flow
Building a Production-Ready Newsletter System: Beyond the Welcome Email
The Problem
After implementing an automated welcome email, I realized that a truly production-ready newsletter system needs two more fundamental things:
- A way for the author to actually send emails (Broadcasts).
- A way for the user to leave gracefully (Unsubscribe).
Without a broadcast tool, the list is just a database of emails with no utility. Without an unsubscribe flow, you're one "Mark as Spam" click away from destroying your domain reputation.
The Solution
I built a secure, dual-purpose system integrated directly into Astro.
1. The Broadcast Dashboard
I created a restricted admin page at /admin/broadcast that lets me compose HTML emails and send them to all active subscribers in my Cloudflare D1 database. To handle large lists, the backend API (/api/broadcast.ts) chunks recipients into batches of 100 before sending them through Resend's Batch API.
Security is handled via an ADMIN_SECRET environment variable, which must be provided in the UI to authorize the send.
2. The Respectful Unsubscribe
Instead of a cold "You've been removed" link, I built a multi-state unsubscription flow:
- Confirmation: A landing page that asks if you're sure, giving you a chance to click "Keep me subscribed."
- Undo Button: If you do unsubscribe, you're presented with a "Welcome Back" button that instantly re-activations your record. This solves the problem of accidental clicks in email clients.
The AI Angle
This session was a masterclass in "Agentic Polish." I initially asked for unsubscription logic, and the AI proactively suggested the "Undo" feature and the choice-based UI.
When it came to deployment, the AI noticed that my local wrangler.jsonc was out of sync with my remote custom domain settings on Cloudflare. It caught a potential "site down" scenario before I even hit deploy by cross-referencing my local files with the current remote state.
Key Takeaways
- Batching matters: Don't just loop through 1,000 users and await 1,000 individual API calls. Use Batch APIs.
- Tokens are safety: Never use IDs in your unsubscribe URLs. Use the same secure
verification_tokencreated at signup. - The "Undo" UX: Users appreciate being able to fix a mistake easily. It builds trust, even at the moment they're trying to leave.