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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Python email.generator: serialize a message object to text or bytes." iinventedlight Library, iil:note:2xn2yjj1, version of 2026-11-26. https://iinventedlight.com/library/notes/python-email-generator/. License: CC-BY-4.0.
@misc{iil_note_2xn2yjj1,
author = {{iinventedlight}},
title = {{Python email.generator: serialize a message object to text or bytes}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-email-generator/},
note = {Permanent ID iil:note:2xn2yjj1. 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-generator.schema.json (schema; CC-BY-4.0)
- python-email-generator.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-generator.md.