Email and MIME Modules

email.message

Provides EmailMessage, the central message-tree interface. Headers are distinct from body content; attachments and alternatives are represented as nested parts. Use get_content for decoded content and iter_attachments for attachment traversal. Treat filenames and received payloads as untrusted.

Formal Syntax & Structure

python syntaxpython
import email.message

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from email.message import EmailMessagemessage = EmailMessage()message["Subject"] = "Report"message.set_content("Ready")message.add_attachment(b"abc", maintype="application", subtype="octet-stream", filename="data.bin")print(next(message.iter_attachments()).get_filename())
Python Reference