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
- id: C1 claim: ContentManager is the base class for content managers. source: S1 evidence: "Base class for content managers."
- id: C2 claim: set_content adds a MIME-Version header when one is missing. source: S1 evidence: "Also add a MIME-Version header if one is not present"
- id: C3 claim: The email package currently provides one concrete content manager, raw_data_manager. source: S2 evidence: "although more may be added in the future."
Sources
- [S1] email.contentmanager: Managing MIME Content. Python Software Foundation. https://docs.python.org/3/library/email.contentmanager.html . License: PSF License Version 2. Accessed 2026-11-26.
- [S2] email.contentmanager documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/email.contentmanager.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.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
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.},
}
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-contentmanager.schema.json (schema; CC-BY-4.0)
- python-email-contentmanager.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-contentmanager.md.