Python email.headerregistry: typed RFC 5322 header objects
Answer: Headers are represented by customized subclasses of str. HeaderRegistry produces them for RFC 5322 compliant email messages.
How headers are typed
Each header value is a customized subclass of str. Which class is used depends on the policy's header_factory at the time the header is created.
When the policy comes from EmailPolicy, HeaderRegistry produces every header, and BaseHeader is always the last base class.
A second, specialized base class is chosen from a lookup table by the header name. UnstructuredHeader is the default, and Subject is the classic example.
DateHeader follows the RFC 5322 date format and can be created from a datetime. Address headers are one of the most complex structured header types.
Extending the registry
The email package provides an extension mechanism for applications to add their own custom header types.
BaseHeader cannot build an object on its own. A specialized class must provide a parse class method, which records any RFC defects it finds.
The defects attribute is a tuple of HeaderDefect instances. If utf8 is false, non-ASCII data is encoded with RFC 2047.
See also
Claims
- id: C1 claim: Header values are customized subclasses of str. source: S1 evidence: "Headers are represented by customized subclasses"
- id: C2 claim: Subject is the classic unstructured header. source: S1 evidence: "The classic example of an unstructured header is the Subject header."
- id: C3 claim: The module was added in Python 3.6. source: S2 evidence: "versionadded:: 3.6"
Sources
- [S1] email.headerregistry: Custom Header Objects. Python Software Foundation. https://docs.python.org/3/library/email.headerregistry.html . License: PSF License Version 2. Accessed 2026-11-26.
- [S2] email.headerregistry documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/email.headerregistry.rst . License: PSF License Version 2. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, email.headerregistry: Custom Header Objects; CPython, email.headerregistry documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, email.headerregistry: Custom Header Objects; CPython, email.headerregistry documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python email.headerregistry: typed RFC 5322 header objects." iinventedlight Library, iil:note:vh4bjxf6, version of 2026-11-26. https://iinventedlight.com/library/notes/python-email-headerregistry/. License: CC-BY-4.0.
@misc{iil_note_vh4bjxf6,
author = {{iinventedlight}},
title = {{Python email.headerregistry: typed RFC 5322 header objects}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-email-headerregistry/},
note = {Permanent ID iil:note:vh4bjxf6. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-email-headerregistry.schema.json (schema; CC-BY-4.0)
- python-email-headerregistry.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-email-headerregistry.md.