Skip to Content
Course content

235: Building a Newsletter Subscription Service

Click on the "Edit" button in the top corner of the screen to edit your slide content.

By now, you're comfortable with databases and form handling, so building a newsletter subscription service feels like a weekend project. It seems simple: you need an input field for an email address, a table to store it, and a way to send a "Welcome!" email. If you're in a rush, you might be tempted to just glue these three things together in a single PHP script.

The temptation of the "Just Save It" script

In the naive version of this service, you'd likely write a script that accepts a POST request, runs an INSERT statement, and then calls the mail() function right there in the middle of the execution. It looks clean on paper. You hit submit, the database updates, the email sends, and you redirect the user to a "Thank You" page. I've seen this in countless junior projects, and it works perfectly... until you actually have users.

// The naive way: Don't do this in production
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = $_POST['email'];
    $db->query("INSERT INTO subscribers (email) VALUES ('$email')"); 
    mail($email, "Welcome!", "Thanks for joining our newsletter!");
    header("Location: /thanks.php");
}

The first thing that breaks here isn't even the code; it's the user experience. The mail() function is synchronous. Your PHP script pauses everything while it talks to the mail server. If the SMTP server is sluggish or the network hiccups, your user is staring at a white screen for five or ten seconds. In the modern web, a five-second delay feels like an eternity. You're essentially holding the user's browser hostage while your server does chores.

Decoupling the subscription from the notification

To fix the latency issue, we have to stop treating the email as part of the request. Instead of sending the mail immediately, I want you to think of the database as a "to-do list." When a user signs up, you save their email and a status flag—something like is_processed = 0. Then, you immediately redirect the user to the thank-you page. The user is happy because the page loaded instantly.

Now, you have a separate PHP script—a worker—that runs in the background (perhaps via a cron job every minute). This script looks for any rows where is_processed is 0, sends the emails, and flips the flag to 1. This way, if your mail server goes down for an hour, your website doesn't crash; the emails just queue up and send once the service is back online. It's a much more resilient architecture.

The risk of "Single Opt-In"

There's another problem with the naive approach: trust. If you just take an email and add it to your list, you're inviting trouble. Someone could write a simple script to submit a thousand fake email addresses to your form. Suddenly, your mail server is sending spam to people who never signed up, and your IP address gets blacklisted by Gmail and Outlook. Once you're blacklisted, even your legitimate business emails stop arriving.

The professional way to handle this is "Double Opt-In." Instead of adding them to the active subscriber list immediately, you create a pending_subscriptions table. You generate a long, random token—something like bin2hex(random_bytes(32))—and save it alongside the email. You send a confirmation email with a link: subscribe.php?token=abc123xyz.

Only when the user clicks that link do you actually move them into your main subscribers table. It's an extra step for the user, sure, but it guarantees that the email address is valid and that the owner actually wants your content. It protects your sender reputation and keeps your list clean of bots.




📋 Practical Task

Implementing the Double Opt-In Token System

Build a subscription flow that prevents immediate activation. You will need to create two tables: pending_subscriptions (storing email, token, and created_at) and subscribers (storing email and subscribed_at).

  • Create a signup.php script that generates a secure random token, stores the email in the pending table, and simulates sending an email (you can just echo the confirmation link to the screen for testing).
  • Create a verify.php script that accepts a token via GET. It must check if the token exists in the pending table, move that email address to the subscribers table, and then delete the record from the pending table to prevent the token from being used twice.
  • Ensure you use prepared statements for all database interactions to prevent SQL injection.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.