← All notes

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

Sources

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

Permanent ID: iil:note:vh4bjxf6

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.},
}

Markdown

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.

All artifact files (JSON index)

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.