Skip to main content

listunsub

Typed, zero-dependency parser and builder for the email List-Unsubscribe and List-Unsubscribe-Post headers: RFC 2369 parsing, RFC 8058 one-click, RFC 6068 mailto: URIs, and a check against the rules Gmail and Yahoo apply to bulk senders.

Install

pip install listunsub

Reading the header (mail receivers, clients, tools)

from listunsub import choose, one_click_request, parse_header

field = """(Unsubscribe)
 <mailto:list-request@example.com?subject=unsubscribe>,
 <https://example.com/unsubscribe/opaque123>"""
header = parse_header(field)
print(header.uris)
# ('mailto:list-request@example.com?subject=unsubscribe', 'https://example.com/unsubscribe/opaque123')
print(header.comments)
# ('Unsubscribe',)

# One-click only when the post value is right and you verified the DKIM signature.
action = choose(header, "List-Unsubscribe=One-Click", dkim_verified=True)
print(action)
# Action(kind='one-click', uri='https://example.com/unsubscribe/opaque123')

request = one_click_request(action.uri, form="urlencoded")
print(request.method, request.url)
# POST https://example.com/unsubscribe/opaque123
print(request.headers, request.body)
# {'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': '26'} b'List-Unsubscribe=One-Click'

Parsing follows the client rules of RFC 2369 section 2: folding and comments are understood, a field that does not start with < is ignored, text after a URI ends the list unless a comma follows, and white space inside the brackets is dropped. Pass strict=True to get a ListUnsubscribeError (a ValueError with reason and offset) instead of silently ignoring anything:

from listunsub import ListUnsubscribeError, parse

print(parse("<https://example.com/u> trailing text, <mailto:u@example.com>"))
# ['https://example.com/u']
try:
    parse("<https://example.com/u> trailing text", strict=True)
except ListUnsubscribeError as error:
    print(error.reason, error.offset)
# expected ',' after '>' 24

Comments are decoded for display but remain untrusted text: control characters, line separators and bidi overrides in them are replaced by U+FFFD.

Without a verified DKIM signature choose never returns one-click: RFC 8058 section 4 says not to offer it. actions lists every usable way, best first, so you can fall back when one fails. one_click_request refuses anything but an HTTPS URI with a host and no user information, drops the fragment, and builds multipart/form-data by default (the RFC's SHOULD). Send it without cookies or credentials and do not follow redirects.

Writing the header (mail senders)

from listunsub import dkim_covers, header_line, sender_headers

fields = sender_headers(
    "https://example.com/unsubscribe/7f3a9c",
    "mailto:unsubscribe@example.com?subject=unsubscribe",
)
for name, value in fields.items():
    print(header_line(name, value, width=60))
# List-Unsubscribe: <https://example.com/unsubscribe/7f3a9c>,
#  <mailto:unsubscribe@example.com?subject=unsubscribe>
# List-Unsubscribe-Post: List-Unsubscribe=One-Click

# Both fields must be in the h= tag of your DKIM signature.
print(dkim_covers("v=1; d=example.com; h=from:to:list-unsubscribe:list-unsubscribe-post"))
# True

build refuses a URI that would break the header or inject another one: no scheme, white space, angle brackets, control characters or raw non-ASCII text. header_line refuses a bad field name, control characters and Unicode line separators, and a line over the 998-octet limit.

mailto URIs

from listunsub import build_mailto, parse_mailto

uri = build_mailto("list-request@example.com", subject="unsubscribe café", body="remove me")
print(uri)
# mailto:list-request@example.com?subject=unsubscribe%20caf%C3%A9&body=remove%20me
mailto = parse_mailto(uri, strict=True)
print(mailto.to, mailto.subject)
# ('list-request@example.com',) unsubscribe café
message = mailto.to_message("reader@example.org")
print(message["To"], "|", message["Subject"])
# list-request@example.com | unsubscribe café

to_message keeps only To, Subject, Keywords and the body, as RFC 6068 sections 3 and 4 advise, writes an internationalized domain in its IDNA form, and refuses a header value holding a line break or a control character. build_mailto also prefers the IDNA form.

Checking a message

from listunsub import audit

for problem in audit("<http://example.com/u>", None, dkim_signatures=[]):
    print(problem.code)
# no-https
# missing-post
# missing-dkim

The codes are missing-list-unsubscribe, duplicate-list-unsubscribe, duplicate-post, invalid-list-unsubscribe, invalid-mailto, unusable-https, no-https, missing-post, invalid-post, missing-dkim and dkim-not-covering. Since February 2024 Gmail and Yahoo expect one-click on marketing and subscribed mail from bulk senders, so an empty list is what such a sender wants.

Command line

listunsub message.eml [--dkim-verified]
python -m listunsub < message.eml

It reads a message (or just its header block), prints the URIs, comments, one-click status, usable actions and audit problems as JSON, and exits 0 when the audit is clean, 1 when it is not and 2 when the file cannot be read.

API

Name What it does
parse(value, *, strict=False) URIs of a List-Unsubscribe field body, as a list
parse_header(value, *, strict=False) ListUnsubscribe: uris, comments, ignored, https, mailto, by_scheme()
build(uris) the field body for a list of URIs, validated
header_line(name, value, *, width=78) Name: value folded with CRLF and a space
sender_headers(https, mailto=None) both one-click fields for a sender
ONE_CLICK_POST, one_click_post() List-Unsubscribe=One-Click
is_one_click(value) whether a List-Unsubscribe-Post body asks for one-click
one_click_request(uri, *, form, boundary) the OneClickRequest (method, url, content type, body, headers)
actions(...), choose(...) every usable Action, best first, or the best one
signed_headers(signature), dkim_covers(signature, fields) the DKIM h= list and whether it signs the fields
parse_mailto(uri, *, strict=False), build_mailto(to, ...) Mailto (to, fields, subject, body, get(), to_message()) and its inverse
audit(list_unsubscribe, post, *, dkim_signatures) a list of Problem (code, message)
mailto_links(uris), http_links(uris) filter URIs by scheme
ListUnsubscribeError the ValueError raised for anything invalid

Why

Every bulk sender has to produce these headers and every mailbox provider, client and deliverability tool has to read them, yet the parsing is usually a regular expression that takes any <...> anywhere, including inside comments, and the one-click POST is assembled by hand. This library does the standard parts exactly and nothing else: it sends no mail and no HTTP request, and it reads the DKIM h= tag without verifying the signature (that needs DNS and cryptography).

Standards

RFC 2369 (list header fields), RFC 8058 (one-click, with its verified errata), RFC 6068 (mailto:), RFC 5322 (folding, comments), RFC 2047 (encoded words in comments) and RFC 6376 (the DKIM tag list). The tests check the library against the examples and grammar rules of those texts; python scripts/fetch_upstream.py downloads them into tests/upstream, verified against the sizes and hashes in MANIFEST.json.

License

MIT. See LICENSE.

Metadata

Release files for listunsub 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for listunsub 0.2.0
File Size Uploaded Provenance Actions
listunsub-0.2.0.tar.gz 41.0 kB Without provenance Details for listunsub-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for listunsub 0.2.0
File Interpreter ABI Platform Actions
listunsub-0.2.0-py3-none-any.whl Python 3 none any Details for listunsub-0.2.0-py3-none-any.whl

Total release size: 66.7 kB

Release files / listunsub-0.2.0.tar.gz

Download URL listunsub-0.2.0.tar.gz
Size 41.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0d08234e02c392b17fb68edfbf933c8f55f9cf8dac43417547923063fc5e77df
BLAKE2b-256 checksum
How to use checksums
8b03554e20d7b03b74a6e39e7e5940a0f4f1f278cbf101160914b2c348ed474d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / listunsub-0.2.0-py3-none-any.whl

Download URL listunsub-0.2.0-py3-none-any.whl
Size 25.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30091d5eefa943680cd7de9ddd6738c92fadc4716a69d767bde265d19c9e1927
BLAKE2b-256 checksum
How to use checksums
b3d932e61f2b1eba18960b050bd827554e36ed01cd2d271950540cfd90b680af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for listunsub 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page