Python email.mime: build MIME message objects from scratch
Answer: email.mime is part of the legacy Compat32 API and helps you create MIME objects from scratch.
Legacy API and MIMEBase
The module is part of the legacy Compat32 email API. contentmanager in the new API partially replaces this functionality, but these classes may still be useful in some applications.
You ordinarily get a message object by passing a file or text to a parser, or you can build one.
MIMEBase is the base class for the MIME-specific Message subclasses. You ordinarily will not create MIMEBase instances themselves. MIMEBase is provided primarily as a convenient base for more specific MIME-aware types.
_maintype is the Content Type major type, such as text or image, and _subtype is the minor type, such as plain or gif. _params is a key/value dictionary passed to Message.add_header.
MIMEBase always adds a Content Type header, plus a MIME Version header set to 1.0. Version 3.6 added the policy keyword-only parameter.
Concrete MIME classes
MIMENonMultipart is the intermediate base for non-multipart messages. Calling attach() raises MultipartConversionError.
MIMEMultipart is the intermediate base for multipart messages, with default subtype mixed. If boundary is None, it is computed when needed. _subparts can supply an initial payload list.
MIMEApplication represents major type application, with default subtype octet-stream and default encoder encode_base64.
For MIMEImage, if the data type can be detected, the subtype is included automatically. Otherwise you can explicitly specify the image subtype.
MIMEMessage wraps another Message as its payload, default subtype rfc822. A non-Message payload raises TypeError.
MIMEText represents major type text, default subtype plain. charset defaults to us-ascii for ASCII text, otherwise utf-8.
See also
Claims
- id: C1 claim: email.mime is part of the legacy Compat32 email API. source: S1 evidence: "This module is part of the legacy"
- id: C2 claim: MIMEBase always adds Content-Type and MIME-Version 1.0 headers. source: S1 evidence: "The MIMEBase class always adds a Content-Type header"
- id: C3 claim: Calling attach on a MIMENonMultipart message raises MultipartConversionError. source: S2 evidence: "a :exc:
~email.errors.MultipartConversionErrorexception is raised"
Sources
- [S1] email.mime: Creating email and MIME objects from scratch. Python Software Foundation. https://docs.python.org/3/library/email.mime.html . License: PSF License Version 2. Accessed 2026-11-27.
- [S2] email.mime documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/email.mime.rst . License: PSF License Version 2. Accessed 2026-11-27.
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.mime: Creating email and MIME objects from scratch; CPython, email.mime 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.mime: Creating email and MIME objects from scratch; CPython, email.mime documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python email.mime: build MIME message objects from scratch." iinventedlight Library, iil:note:jn2gadw1, version of 2026-11-27. https://iinventedlight.com/library/notes/python-email-mime/. License: CC-BY-4.0.
@misc{iil_note_jn2gadw1,
author = {{iinventedlight}},
title = {{Python email.mime: build MIME message objects from scratch}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/python-email-mime/},
note = {Permanent ID iil:note:jn2gadw1. 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-mime.schema.json (schema; CC-BY-4.0)
- python-email-mime.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-mime.md.