Sub topic 12
List-Unsubscribe headers and one-click unsubscribe
This chapter adds machine-readable unsubscribe headers to your marketing emails. It assumes you have read the chapters on the unsubscribe flow and signed tokens. You should also be comfortable with HTTP GET and POST requests and with how email headers work.
Why headers matter
The unsubscribe link in the body of an email is for people. The List-Unsubscribe header is for mail clients. When it is present, Gmail, Apple Mail and others can show their own "Unsubscribe" control next to the sender name. A subscriber who wants to leave then uses that control instead of reporting your message as spam. A spam complaint hurts your sender reputation. An unsubscribe does not.
Major mailbox providers expect bulk senders to support one-click unsubscribe, so these headers are a requirement for reaching the inbox as well as a courtesy.
The two headers
List-Unsubscribe: <https://example.com/unsubscribe/TOKEN>, <mailto:[email protected]?subject=unsubscribe>
List-Unsubscribe-Post: List-Unsubscribe=One-ClickList-Unsubscribelists one or more addresses, each in angle brackets. The HTTPS address is the main one. Themailto:address is a fallback for older clients.List-Unsubscribe-Posttells the client it may send a one-click request, as defined in RFC 8058.
Mailbox providers also expect the message to carry a valid DKIM signature that covers both headers. Without it, clients may ignore them. Add list-unsubscribe and list-unsubscribe-post to the list of signed headers in your DKIM configuration.
What the client sends
With one-click, the mail client does not open a page. It sends an HTTP POST to your HTTPS address with this body:
List-Unsubscribe=One-ClickThe client sends no cookies and shows the user no confirmation page from you. Your endpoint must therefore identify the subscriber from the URL alone. This is why the URL carries a signed token, built as in the earlier chapter on tokens, with the purpose unsubscribe.
Handling the request
The endpoint must accept both methods, and they must behave differently. This follows the same rule as confirmation links: a GET may be a scanner prefetching the link, so it must not change state. A POST is an explicit action.
from datetime import datetime, timezone
def unsubscribe_page(token: str):
"""GET: show a confirmation button. Change nothing."""
email = verify_token(token, "unsubscribe")
if email is None:
return render("link_invalid_or_expired.html")
return render("unsubscribe_button.html", token=token)
def unsubscribe(db, token: str):
"""POST: used by the page button and by one-click clients."""
email = verify_token(token, "unsubscribe")
if email is None:
return render("link_invalid_or_expired.html"), 400
now = datetime.now(timezone.utc)
db.execute(
"UPDATE subscribers SET status = 'unsubscribed' "
"WHERE email = %s AND status <> 'unsubscribed'",
(email,),
)
db.execute(
"INSERT INTO consent_events (subscriber_id, event_type, source, occurred_at) "
"SELECT id, 'unsubscribe', 'list_unsubscribe_post', %s "
"FROM subscribers WHERE email = %s",
(now, email),
)
return render("unsubscribed.html")The status change is idempotent, so a client that retries the POST does no harm. Unsubscribe tokens should not expire quickly, because people unsubscribe from old emails.
Common errors
- Changing state on GET. A scanner then unsubscribes people who never asked.
- Requiring a login or a CAPTCHA. One-click requests have no session, and this defeats the purpose.
- Redirecting the POST. Some clients do not follow redirects, so answer directly with a 2xx status.
- Forgetting DKIM coverage. The headers are present but ignored.
- One shared token for everyone. Each message needs a token tied to one subscriber.
- Not updating the suppression list. The unsubscribe must take effect before the next send.
Exercise
A scanner opens every link in your emails with a GET request. Why is your List-Unsubscribe URL still safe, and what must the one-click handler do on POST?
Show answer
The GET handler only shows a page with a button and changes nothing, so a scanner cannot unsubscribe anyone. The POST handler verifies the signed token for the purpose "unsubscribe", marks the subscriber unsubscribed in a way that is safe to repeat, records a consent event, and replies with a 2xx status without a redirect, login or CAPTCHA.