← All notes

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

Sources

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

Permanent ID: iil:note:2xn2yjj1

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.},
}

Markdown

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.

All artifact files (JSON index)

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.