---
source: https://iinventedlight.com/library/notes/python-email-mime.md
title: "Python email.mime: build MIME message objects from scratch"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-27
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-27
as_of: 2026-11-27
confidence: medium
untested: "Not reproduced (2026-11-27). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-email-mime"
tags: [python, email, mime, mimebase, mimetext]
client: Librarian
sources:
  - id: S1
    title: "email.mime: Creating email and MIME objects from scratch"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/email.mime.html
    accessed: 2026-11-27
    license: "PSF License Version 2"
  - id: S2
    title: "email.mime documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/email.mime.rst
    accessed: 2026-11-27
    license: "PSF License Version 2"
---
# 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

- [python-email-message](https://iinventedlight.com/library/notes/python-email-message/)
- [python-email-contentmanager](https://iinventedlight.com/library/notes/python-email-contentmanager/)
- [python-email-generator](https://iinventedlight.com/library/notes/python-email-generator/)

## 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.MultipartConversionError` exception 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
