Sub topic 12
List-Unsubscribe headers and one-click unsubscribe
This chapter assumes you have read the chapters on the unsubscribe flow and signed tokens. It also assumes you know how HTTP GET and POST differ, and that you have a basic idea of how DKIM signing works. By the end, you can add the List-Unsubscribe headers to your marketing mail and handle the one-click request they trigger.
Why the headers exist
An unsubscribe link in the body of an email is easy to hide, easy to miss, and different in every message. Mailbox providers want a standard control that they draw themselves, such as the "Unsubscribe" link next to the sender name in Gmail. Subscribers who can't find your link often press "Report spam" instead. Complaints damage your sender reputation much more than unsubscribes do.
Two headers make that control possible:
List-Unsubscribetells the mail client where to send an unsubscribe request. It can hold an HTTPS address, amailto:address, or both.List-Unsubscribe-Posttells the client that the HTTPS address accepts a one-click POST request. This is defined in RFC 8058.
The headers do not replace the visible unsubscribe link in the body. You still need both.
The header format
List-Unsubscribe: <https://example.com/unsubscribe/TOKEN>, <mailto:[email protected]?subject=unsubscribe>
List-Unsubscribe-Post: List-Unsubscribe=One-ClickEach address sits inside angle brackets, and the entries are separated by commas. The mail client chooses which one to use. A client that supports one-click will send a POST request to the HTTPS address. An older client may send a message to the mailto: address instead.
The value List-Unsubscribe=One-Click is fixed. The client sends exactly that string as the body of its POST request.
Requirements for one-click
Gmail, Yahoo, and other large providers expect bulk senders to meet these conditions. Check their current sender guidelines, because the thresholds change.
- The
List-Unsubscribeheader contains an HTTPS address. - The
List-Unsubscribe-Postheader is present with the value above. - Both headers are covered by a valid DKIM signature. Without that, a forger could add an unsubscribe address to a message and use it to unsubscribe someone else.
- The HTTPS endpoint accepts a POST request without a login, a cookie, or a confirmation page.
- The unsubscribe takes effect quickly. Providers commonly expect it to be processed within two days, and you should aim for immediate effect.
What happens on the wire
The block above is not used. The sequence is shown in the diagram below.
The subscriber never sees your website. The mail client sends the request in the background, so your response is for the client, not for a person. A short 200 OK is enough.
Generating a per-recipient token
Each message needs its own unsubscribe address, because the address identifies the subscriber. Use the signed token approach from the earlier chapter, with a separate purpose so that a confirmation token can't be used here.
Unsubscribe links in sent mail should not expire quickly. A subscriber may open an old message months later and expect the link to work. Use a long lifetime, or sign a token with no expiry that carries only the purpose and the address. The token is limited in what it can do: it can only unsubscribe that one address.
import os
import time
from flask import Flask, abort, render_template, request
# make_token and verify_token are defined in the token chapter.
app = Flask(__name__)
TEN_YEARS = 10 * 365 * 24 * 3600
def unsubscribe_url(email: str) -> str:
token = make_token(email, "unsubscribe", ttl_seconds=TEN_YEARS)
return f"https://example.com/unsubscribe/{token}"
def unsubscribe_headers(email: str) -> dict:
url = unsubscribe_url(email)
return {
"List-Unsubscribe": f"<{url}>, <mailto:[email protected]?subject=unsubscribe>",
"List-Unsubscribe-Post": "List-Unsubscribe=One-Click",
}Handling GET and POST differently
The same address receives two kinds of request. This follows the scanner rule from the earlier chapter: a GET request must never change state.
- A GET request comes from a person who clicked the link in the email body, or from a scanner that is checking the link. Show a page with a confirmation button.
- A POST request comes from a one-click client, or from the button on that page. Process the unsubscribe.
@app.get("/unsubscribe/<token>")
def unsubscribe_page(token):
email = verify_token(token, "unsubscribe")
if email is None:
return render_template("link_invalid.html"), 400
return render_template("unsubscribe_button.html", token=token)
@app.post("/unsubscribe/<token>")
def unsubscribe_post(token):
email = verify_token(token, "unsubscribe")
if email is None:
abort(400)
apply_unsubscribe(
email=email,
source="one_click" if request.form.get("List-Unsubscribe") == "One-Click" else "web_page",
ip=request.remote_addr,
user_agent=request.headers.get("User-Agent"),
)
return "", 200The apply_unsubscribe function does the real work in one database transaction. It sets the status, adds the address to the suppression list, and writes a consent event.
def apply_unsubscribe(email, source, ip, user_agent):
with db.transaction():
row = db.query_one(
"UPDATE subscribers SET status = 'unsubscribed' "
"WHERE email = %s RETURNING id",
(email,),
)
if row is None:
return
db.execute(
"INSERT INTO suppressions (email, reason) VALUES (%s, 'unsubscribe') "
"ON CONFLICT (email) DO NOTHING",
(email,),
)
db.execute(
"INSERT INTO consent_events (subscriber_id, event_type, source, ip_address, user_agent) "
"VALUES (%s, 'unsubscribe', %s, %s, %s)",
(row["id"], source, ip, user_agent),
)The function is idempotent. A second request leaves the status as it is, and the suppression insert is ignored. This matters because some clients retry on a timeout.
The ip and user_agent values in a one-click request belong to the mail provider's servers, not to the subscriber. Store them, but do not treat them as evidence about the person.
Edge cases and common errors
| Problem | Cause | Fix |
|---|---|---|
| Provider does not show the Unsubscribe control | DKIM signature does not cover the headers | Add `List-Unsubscribe` and `List-Unsubscribe-Post` to the signed header list in your sending service |
| POST returns 403 | CSRF protection rejects a request that has no form token | Exempt this endpoint from CSRF checks, because the signed token in the address is the credential |
| POST returns 302 | The endpoint redirects to a login or a confirmation page | Return 200 directly. Clients do not follow redirects reliably |
| Subscriber is unsubscribed without clicking | The endpoint changes state on GET | Change state only on POST |
| Unsubscribe works but mail keeps arriving | The send job does not check the suppression list | Filter against suppressions at send time, as covered in the previous chapter |
| Only some messages have the headers | Transactional and marketing mail share one template path | Add the headers in the marketing send path only |
The mailto fallback
The mailto: entry is optional for providers that support one-click, but it helps with smaller clients. Incoming messages to that address need a handler. Read the From address of the message, look up the subscriber, and call the same apply_unsubscribe function with the source mailto. Do not trust the From header alone if you can avoid it. A forged sender could unsubscribe someone else. Put the token in the subject or the local part of the address instead, for example [email protected].
Forwarded mail and shared addresses
When a recipient forwards a message, the unsubscribe address still belongs to the original recipient. If a colleague presses the button, the original recipient is unsubscribed. This is a known limit of per-recipient tokens. The confirmation button on the web page reduces the risk for body links, but one-click requests have no page. Accept the risk, and make re-subscription easy through the preference centre.
Testing the endpoint
Send a POST request the way a mail client would. Use curl with the exact body.
curl -i -X POST \
-d "List-Unsubscribe=One-Click" \
"https://example.com/unsubscribe/TOKEN"Expect HTTP/1.1 200. Then check three things: the subscriber row has status unsubscribed, the suppression table has the address, and the consent events table has a new row. Repeat the request and confirm that nothing changes and the response is still 200. Send a request with a changed token and confirm that it returns 400 without touching the database.
To test the headers in a real message, send a test email to a Gmail account and open "Show original". Look for dkim=pass and check that List-Unsubscribe appears in the DKIM h= tag.
Exercise
Your server receives a POST to /unsubscribe/TOKEN, but the mail provider stops showing its Unsubscribe control. The endpoint returns 200 and works with curl. Name two header-level causes you would check first.
Show answer
First, check that both List-Unsubscribe and List-Unsubscribe-Post are covered by the DKIM signature, which you can see in the h= tag of the DKIM-Signature header. Second, check that the List-Unsubscribe header contains an HTTPS address and that List-Unsubscribe-Post has the exact value List-Unsubscribe=One-Click. A missing or altered value, or an unsigned header, makes the provider ignore the one-click control.
Exercise
Why should the GET handler for the unsubscribe address show a button instead of unsubscribing immediately, when the POST handler unsubscribes right away?
Show answer
Security scanners and link prefetchers open links with GET requests. If GET changed state, a scanner could unsubscribe people who never asked. One-click clients send POST requests only when a person has used the control, so POST can act immediately.