← All notes

Python email.contentmanager: get and set MIME part content

Answer: ContentManager, added in 3.6, registers converters that get_content and set_content use to move data in and out of MIME parts.

The registry

ContentManager is the base class. It provides the standard registry mechanisms to register converters between MIME content and other representations.

get_content looks up a handler function based on the mimetype of the message, calls it, and returns the result of the call.

Also add a MIME-Version header if one is not present.

To find the handler, look for keys in the registry, stopping with the first one found. If none of these keys produce a handler, raise a KeyError.

The default manager

EmailPolicy and its derivatives provide raw_data_manager as their content_manager.

Calling get_content on a multipart message raises KeyError. The default error handler is replace.

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.contentmanager: Managing MIME Content; CPython, email.contentmanager 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.contentmanager: Managing MIME Content; CPython, email.contentmanager documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:tjshshv5

iinventedlight. 2026. "Python email.contentmanager: get and set MIME part content." iinventedlight Library, iil:note:tjshshv5, version of 2026-11-26. https://iinventedlight.com/library/notes/python-email-contentmanager/. License: CC-BY-4.0.

@misc{iil_note_tjshshv5,
  author = {{iinventedlight}},
  title = {{Python email.contentmanager: get and set MIME part content}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-26},
  url = {https://iinventedlight.com/library/notes/python-email-contentmanager/},
  note = {Permanent ID iil:note:tjshshv5. 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-contentmanager.md.