Sub topic 9
One-click unsubscribe headers and endpoint
This chapter assumes you can read raw email headers, edit DNS TXT records, and know that DKIM signs selected headers of a message. You should also have a basic idea of how a web endpoint handles an HTTP POST. The earlier chapters cover SPF, DKIM, and DMARC.
What the requirement says
Gmail and Yahoo require bulk senders to support one-click unsubscribe in marketing and subscribed messages. Transactional mail such as password resets and receipts is exempt. Both providers expect a sender to honor an unsubscribe request within two days.
The mechanism is RFC 8058. Two message headers tell the mailbox provider that it may send a POST request to your server on the user's behalf. When a user clicks the provider's own "Unsubscribe" button, the provider sends the request. The user never visits a page of yours, and nobody needs to log in or confirm.
Gmail and Yahoo also expect a visible unsubscribe link in the message body. The header does not replace that link. Microsoft's consumer-mailbox requirements focus mainly on authentication. Treat one-click unsubscribe as expected practice there as well, and check Microsoft's current documentation for exact wording.
The two headers
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-ClickEach header has a distinct job:
List-Unsubscribelists one or more URIs in angle brackets, separated by commas. For one-click, it must include an HTTPS URL. Themailto:entry is an optional fallback for clients that cannot POST.List-Unsubscribe-Posthas the fixed valueList-Unsubscribe=One-Click. This header signals that the HTTPS URL accepts a one-click POST.
Rules for the headers
- Use HTTPS, not HTTP.
- Make the URL unique per recipient and per message or list. It should contain an unguessable token, not an email address that someone else could submit.
- Include each header only once.
- Cover both headers with the DKIM signature. RFC 8058 requires this so that an intermediary cannot swap in a different unsubscribe target.
A signed message lists the headers in the h= tag of its DKIM-Signature:
DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=mail1;
c=relaxed/relaxed;
h=from:to:subject:date:message-id:list-unsubscribe:list-unsubscribe-post;
bh=...; b=...If your email service provider (ESP) signs for you, check a delivered message. A common failure is that the ESP adds the unsubscribe headers after signing, or leaves them out of h=. Providers may then ignore the headers.
What happens when the user clicks
The provider sends the POST from its own servers, with no cookies and no session. Your endpoint therefore cannot depend on a logged-in user. The token in the URL is the only identity.
Building the endpoint
The request has the content type application/x-www-form-urlencoded, and its body is List-Unsubscribe=One-Click. The endpoint should check the body, look up the token, record the opt-out, and return a 2xx status.
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 "", 200You can test it locally. Save the code as app.py, run flask --app app run, and send the same request a provider would:
curl -i -X POST \
-d "List-Unsubscribe=One-Click" \
http://127.0.0.1:5000/unsubscribe/abc123Expect 200 OK. A POST without the body, or with a bad token, should fail with 400 or 404.
Design points for a production endpoint
- Do not unsubscribe on GET. Security scanners and link previewers fetch URLs in messages automatically. If a GET opts people out, you will lose subscribers who never clicked. Use GET only to show an optional human-readable page, such as a preference center.
- Make it idempotent. Providers may retry. A second POST for an already opted-out subscriber should still return 200.
- Do not redirect. Return the 2xx directly. Redirects to a login or confirmation page can make the request look like a failure to the provider.
- No authentication, CSRF token, or captcha. These would block the provider's request. Protection comes from the unguessable per-recipient token. Generate tokens with a cryptographically secure source, or sign them with an HMAC, rather than using sequential IDs.
- Apply the opt-out everywhere it applies. Suppress the address from the list or category the message belongs to. Suppress it in the sending system as well as in the CRM, so that a separate export does not re-add the address.
- Respond fast. The endpoint should only record the request. Do slower work, such as syncing to other systems, in a background job.
- Log the request. Record the timestamp, token, and result so you can prove when each opt-out happened.
Timing and the mailto fallback
You must stop marketing mail to the address within two days of the request, but you should do it immediately where you can. A request that arrives after you have queued a large send is easy to miss. Check your suppression list at send time, not only when you build the audience.
The mailto: fallback delivers an email to an address you control. Someone or something must process that mailbox. An unmonitored unsubscribe mailbox that fills up or bounces causes the same harm as a broken endpoint.
Verifying your setup
- Send a test marketing message to a Gmail account.
- Open Show original and confirm both headers are present exactly once.
- Confirm
DKIM-Signaturelistslist-unsubscribeandlist-unsubscribe-postinh=, and that Gmail reportsdkim=pass. - Confirm Gmail shows an "Unsubscribe" link beside the sender name. It may not appear for every message, because Gmail also considers sender reputation.
- Click it and check your endpoint logs for a POST and a 200 response.
- Send again to the same address to confirm it is suppressed.
Common errors
| Symptom | Likely cause |
|---|---|
| No unsubscribe link beside the sender in Gmail | Headers missing, not DKIM-signed, or sender reputation too low for Gmail to show it |
| Endpoint returns 400 on provider requests | Body parsed as JSON, or the form field name does not match `List-Unsubscribe` |
| Subscribers vanish without clicking | Unsubscribe is triggered by GET, and scanners fetch the URL |
| Users keep receiving mail after unsubscribing | Opt-out is saved to one system but not applied to the sending list |
| Unsubscribe works in tests but not from providers | Endpoint requires cookies, login, or follows a redirect |
Exercise
A marketing message has List-Unsubscribe: <http://example.com/u/abc123> and List-Unsubscribe-Post: List-Unsubscribe=One-Click, and h= in the DKIM signature does not include either header. Name three changes needed for the message to support RFC 8058 one-click unsubscribe.
Show answer
Change the URL to HTTPS. Add both header names (list-unsubscribe and list-unsubscribe-post) to the DKIM h= list so they are signed. Make the endpoint accept an unauthenticated POST with body List-Unsubscribe=One-Click and return a 2xx status. Adding a mailto: fallback is optional.
Exercise
Your endpoint opts the subscriber out on any request to /unsubscribe/<token>, including GET. Why is this a problem, and how do you fix it?
Show answer
Security scanners and link previewers fetch URLs automatically, so GET requests would opt out people who never clicked. Restrict the opt-out to POST with the List-Unsubscribe=One-Click body. Use GET only to show an optional preference page that requires an explicit action.