Sub topic 10
Making unsubscribing simple and reliable
This chapter assumes you have the subscriber and consent event tables, the signed token helpers (make_token and verify_token), and the GET-shows-a-button, POST-changes-state pattern from earlier chapters. An unsubscribe flow has two jobs. It must be easy for the subscriber, and it must be certain to stop mail.
Why simplicity matters
A person who cannot find the unsubscribe link will click "report spam" instead. Complaints damage your sender reputation far more than an unsubscribe does. Laws such as CAN-SPAM and GDPR also expect withdrawing consent to be as easy as giving it. Treat a smooth exit as protection for the rest of your list.
These rules keep the flow simple:
- Put a clear unsubscribe link in every marketing email, not hidden in tiny grey text.
- Require no login, no password, and no reason for leaving.
- Take effect immediately. Do not tell people to "allow 10 business days".
- Confirm the result on a page, and offer a preference link as an alternative, not a barrier.
The flow
Use a separate token purpose
The unsubscribe link uses the same signed-token design as confirmation, with the purpose "unsubscribe". Because the purpose is signed into the token, a confirmation token cannot unsubscribe anyone. Unsubscribe tokens should not expire quickly, because people click old emails months later. Pass a long ttl_seconds, such as a year, or issue a fresh link in every message.
The handler
As with confirmation, the GET request must change nothing, because security scanners open links automatically. Only the POST unsubscribes.
def show_unsubscribe_page(token: str):
"""Handle GET: display a button only; change nothing."""
if verify_token(token, "unsubscribe") is None:
return render("link_invalid_or_expired.html")
return render("unsubscribe_button.html", token=token)
def unsubscribe(db, token: str, ip: str, user_agent: str):
"""Handle POST: unsubscribe the address. Safe to call repeatedly."""
email = verify_token(token, "unsubscribe")
if email is None:
return render("link_invalid_or_expired.html")
db.execute(
"WITH changed AS ("
" UPDATE subscribers SET status = 'unsubscribed' "
" WHERE email = %s AND status <> 'unsubscribed' RETURNING id"
") "
"INSERT INTO consent_events "
"(subscriber_id, event_type, source, ip_address, user_agent) "
"SELECT id, 'unsubscribe', 'unsubscribe_page', %s, %s FROM changed",
(email, ip, user_agent),
)
return render("unsubscribed.html")The single statement updates the status and writes the audit event together, so you cannot end up with one and not the other. The status <> 'unsubscribed' condition makes the call idempotent: a double click or a retry changes nothing and records no duplicate event.
Common errors
| Error | Result | Fix |
|---|---|---|
| Unsubscribing on GET | A scanner removes people who never asked | Require a POST |
| Short token expiry | Old emails show an error and people complain | Use long-lived unsubscribe tokens |
| Asking for a login or reason | Complaints go up | Make the button the only step |
| Updating status but not the send query | Mail continues after unsubscribe | Filter on status and the suppression list before every send |
Confirming and offering alternatives
The confirmation page should say plainly which address was removed. It can also link to the preference centre ("Want fewer emails instead?") and to a resubscribe action. Keep that offer secondary, so the person never has to dodge it to leave.
Exercise
A subscriber unsubscribes, then a security scanner opens the same unsubscribe link from an old forwarded copy of the email. What should happen, and which two design choices make that safe?
Show answer
Nothing changes. The GET request only displays a page with a button, so the scanner cannot change state. If something did send a POST, the status <> 'unsubscribed' condition makes the repeat a harmless no-op that writes no second consent event.