Sub topic 9
One-click unsubscribe headers and endpoint
This chapter assumes you can read raw email headers, publish DNS records, and write a small web handler. It also assumes your messages are already DKIM-signed, as covered in the DKIM chapter.
What the rule asks for
Gmail and Yahoo require bulk senders to support one-click unsubscribe on marketing and subscribed mail. The standard behind it is RFC 8058. A recipient clicks "Unsubscribe" in the mailbox interface, and the provider sends an HTTPS POST to your server. The recipient never sees a landing page, a login, or a confirmation form.
Providers also expect you to process the opt-out promptly. Gmail and Yahoo publish a window of two days. Keep the visible unsubscribe link in the message body as well.
The two headers
One-click needs two headers together. Neither one works alone.
From: Example News <[email protected]>
To: [email protected]
Subject: Our November update
List-Unsubscribe: <https://example.com/unsubscribe/abc123>, <mailto:[email protected]?subject=unsubscribe>
List-Unsubscribe-Post: List-Unsubscribe=One-ClickList-Unsubscribeholds one or more URIs in angle brackets. The HTTPS URL is what one-click uses. Themailto:address is an optional fallback for clients that do not support POST.List-Unsubscribe-Posttells the receiver that the HTTPS URL accepts a one-click POST.
The URL should be unique per recipient and message. Use an opaque token in the path, not an email address. The token lets you identify the subscriber without a login.
Both headers must be covered by the DKIM signature. Add list-unsubscribe and list-unsubscribe-post to the signed header list (the h= tag). If a header is unsigned, a receiver cannot trust that it came from you.
How the flow works
The POST comes from the provider's servers, not from the recipient's browser. Your endpoint cannot depend on cookies or a session.
The endpoint
This Flask handler accepts the POST, checks the body, and opts the subscriber out.
from flask import Flask, request, abort
app = Flask(__name__)
def unsubscribe_token(token):
# Replace with a lookup that marks the subscriber as opted out.
return token == "abc123"
@app.post("/unsubscribe/<token>")
def one_click_unsubscribe(token):
if request.form.get("List-Unsubscribe") != "One-Click":
abort(400)
if not unsubscribe_token(token):
abort(404)
return "", 200Test it with curl before you send any mail:
curl -i -X POST -d "List-Unsubscribe=One-Click" https://example.com/unsubscribe/abc123Common errors
| Problem | Effect |
|---|---|
| Only `List-Unsubscribe` is present | The provider does not show the one-click control |
| Endpoint redirects to a page that asks for confirmation | The request fails the one-click requirement |
| Endpoint answers only GET | The provider's POST gets a 405 error |
| Headers not covered by DKIM | The receiver may ignore them |
| URL is plain HTTP | The request is not secure and may be rejected |
| Opt-out processed after several days | Complaints rise and you miss the processing window |
Scope
Gmail and Yahoo apply the requirement to marketing and subscribed messages, not to purely transactional mail such as password resets. Microsoft's consumer rules focus on authentication, so check the provider comparison chapter for what it asks for. Adding the headers to all promotional mail is the safest policy.
Exercise
A sender adds List-Unsubscribe: <https://example.com/u/abc123> to every message and signs it with DKIM. Gmail still does not show the one-click unsubscribe control. What is missing, and what must the endpoint do?
Show answer
The List-Unsubscribe-Post: List-Unsubscribe=One-Click header is missing, and it should also be in the DKIM-signed header list. The HTTPS endpoint must accept a POST with the body List-Unsubscribe=One-Click, opt the subscriber out immediately with no further interaction (no login, confirmation page, or CSRF check), and return a 2xx status.
Exercise
Why should the unsubscribe URL contain a unique token instead of the subscriber's email address?
Show answer
The POST comes from the mailbox provider with no session or login, so the token is the only thing that identifies and authorizes the request. A random token is hard to guess, so nobody can unsubscribe other people by trying addresses. It also keeps the recipient's address out of URLs and server logs.