Sub topic 12
Confirming subscribers with double opt-in
This chapter assumes you can build the HTML sign-up form from earlier chapters, know what an email service provider (ESP) does, and have read the chapter on consent. Here you will learn how double opt-in works, what it costs, and how to build the pieces.
What double opt-in is
With single opt-in, an address goes on your list as soon as the visitor submits the form. With double opt-in, the address is stored as "pending". The visitor then receives a confirmation email and must click a link in it. Only after that click does the address become an active subscriber.
The extra step proves two things. The address exists and can receive mail, and the person who controls the inbox agreed to the subscription. Typos, fake addresses and addresses entered by someone else are never confirmed.
Trade-offs
| Row | Single opt-in | Double opt-in |
|---|---|---|
| List size | Larger | Smaller, often by a noticeable share |
| Address quality | Includes typos and fakes | Verified addresses only |
| Bounce and spam-complaint risk | Higher | Lower |
| Proof of consent | Weaker | Stronger, with timestamp and IP |
| Friction | One step | Two steps |
Some privacy regimes and mailbox providers expect or favour confirmed consent, and in some countries it is the norm. Check the rules for the regions you serve. Many teams pick double opt-in when list quality or provable consent matters more than raw list size.
Building it
Most ESPs offer double opt-in as a setting. If you handle it yourself, you need a token, a pending state and a confirm endpoint. The example below uses Python with Flask and an in-memory store, which you would replace with a database.
import secrets
import time
from flask import Flask, request, abort
app = Flask(__name__)
pending = {} # token -> (email, created_at)
active = {} # email -> confirmed_at
TOKEN_TTL = 48 * 60 * 60 # 48 hours
def send_email(to, subject, body):
print(f"To: {to}\nSubject: {subject}\n{body}\n")
@app.post("/subscribe")
def subscribe():
email = request.form.get("email", "").strip().lower()
if request.form.get("website"): # honeypot filled: ignore silently
return "Check your inbox to confirm.", 200
if "@" not in email:
abort(400)
token = secrets.token_urlsafe(32)
pending[token] = (email, time.time())
link = f"https://example.com/confirm?token={token}"
send_email(email, "Please confirm your subscription",
f"Click to confirm: {link}\nIf you did not ask for this, ignore this email.")
return "Check your inbox to confirm.", 200
@app.get("/confirm")
def confirm():
entry = pending.pop(request.args.get("token", ""), None)
if entry is None or time.time() - entry[1] > TOKEN_TTL:
abort(410)
email, _ = entry
active[email] = time.time()
send_email(email, "Welcome!", "Thanks for confirming.")
return "You are subscribed.", 200Notice the design choices. The token is random and unguessable. It is single-use, because pop removes it. It expires. The response to the form is identical whether or not the address is already known, so the form cannot be used to discover who is subscribed.
Common errors
- Hiding the next step. After submitting, show a message such as "Check your inbox to confirm." Without it, many visitors never open the email.
- Slow or spam-filtered confirmations. Send immediately, from a recognisable sender name, with a plain subject line.
- Confirming on page load by scanners. Some security tools open every link in an email. Keep the
GETpage harmless if you can, or show a "Confirm" button that sends aPOST. - Never expiring pending entries. Delete or expire them, and do not mail them repeatedly.
- Not storing proof. Keep the confirmation time and the IP address or user agent used for your consent record.
Exercise
A visitor says they signed up but never received your newsletter. Your system shows their address as "pending". What is the most likely cause, and give two fixes.
Show answer
They never clicked the confirmation link, perhaps because the email was filtered as spam or they did not see it. Fixes: show a clear "check your inbox" message after submission and send one reminder, and improve deliverability with a recognisable sender name and a plain subject line.