---
source: https://iinventedlight.com/library/notes/python-email-generator.md
title: "Python email.generator: serialize a message object to text or bytes"
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.generator: Generating MIME documents; CPython, email.generator 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-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). 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-generator"
tags: [python, email, generator, BytesGenerator, flatten]
client: Librarian
sources:
  - id: S1
    title: "email.generator: Generating MIME documents"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/email.generator.html
    accessed: 2026-11-26
    license: "PSF License Version 2"
  - id: S2
    title: "email.generator documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/email.generator.rst
    accessed: 2026-11-26
    license: "PSF License Version 2"
---
# Python email.generator: serialize a message object to text or bytes
**Answer:** Generator classes take a message object and write a serialized form of it, as text or as bytes.

## BytesGenerator and Generator

Most programs should use BytesGenerator rather than the text Generator.

outfp must support a write method that accepts binary data.

The text Generator's output stream must provide a write method that accepts string data.

If cte_type is 7bit, bytes that have the high bit set are converted with an ASCII-compatible Content-Transfer-Encoding.

## Flatten options

If the root object has no envelope header, the generator crafts a standard one.

Subparts never get an envelope header printed.

A None policy means flatten uses the policy attached to the Message or EmailMessage.

If linesep is None, use the value specified in the policy.

## See also

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

## Claims

- id: C1
  claim: Generator classes serialize a message object structure.
  source: S1
  evidence: "producing a serialized representation is the job of the generator classes."

- id: C2
  claim: If the root has no envelope header, one is crafted.
  source: S1
  evidence: "If the root object has no envelope header, craft a standard one."

- id: C3
  claim: Most applications should use BytesGenerator, not Generator.
  source: S2
  evidence: "most applications will want to be using"

- id: C4
  claim: Subparts never print an envelope header.
  source: S2
  evidence: "Note that for subparts, no envelope header is ever printed."

## Sources

- [S1] email.generator: Generating MIME documents. Python Software Foundation. https://docs.python.org/3/library/email.generator.html . License: PSF License Version 2. Accessed 2026-11-26.

- [S2] email.generator documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/email.generator.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.generator: Generating MIME documents; CPython, email.generator 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).
