On this page
Prev: doctest | Up: stdlib_module | Next: encodings
Rel: json, mailbox, mimetypes, base64, binascii
Ref: docs.python.org — email
Tags: #public #python3 #stdlib
Import
import email
Overview
Full MIME and RFC 5322 email message parser and builder (email.message.EmailMessage, email.parser, email.policy.default). Always use the modern EmailMessage class with policy=email.policy.default (rather than legacy MIMEText/MIMEMultipart) for clean UTF-8 headers, HTML alternatives, and attachments.
Quick Start
from email.message import EmailMessage
msg = EmailMessage()
msg["Subject"] = "Release v0.1.0"
msg["From"] = "releases@example.org"
msg["To"] = "team@example.org"
msg.set_content("Plain-text summary of the release.")
assert msg["Subject"] == "Release v0.1.0"
assert "Plain-text summary" in msg.get_content()
Implementation Walkthrough
1. Multipart Plain-Text + HTML Message with Attachment
from email.message import EmailMessage
msg = EmailMessage()
msg["Subject"] = "Weekly Report"
msg["From"] = "bot@example.org"
msg["To"] = "ada@example.org"
msg.set_content("See the attached CSV report.")
msg.add_alternative("<p>See the attached <b>CSV</b> report.</p>", subtype="html")
msg.add_attachment(b"name,score\nAda,98\n", maintype="text", subtype="csv", filename="report.csv")
assert msg.is_multipart()
2. Parsing Raw RFC 5322 Bytes with policy.default
import email
from email import policy
raw = b"From: ada@example.org\r\nSubject: Hello\r\n\r\nBody text.\r\n"
parsed = email.message_from_bytes(raw, policy=policy.default)
assert parsed["Subject"] == "Hello"
assert parsed.get_body(preferencelist=("plain",)).get_content().strip() == "Body text."
Gotchas & Boundaries
- Always pass
policy=email.policy.defaultwhen parsing: The defaultcompat32policy returns legacyMessageobjects without.get_body()or automatic header decoding. emailbuilds and parses messages—it does NOT send them: Hand theEmailMessagetosmtplib.SMTP.send_message(msg)to transmit.- Sanitize untrusted header values against CRLF (
\r/\n):policy.defaultrejects header-injection newlines automatically.
Common Functions
- email.message_from_binary_file() —
message_from_binary_file(fp, *args, **kws)— Read a binary file and parse its contents into a Message object model. - email.message_from_bytes() —
message_from_bytes(s, *args, **kws)— Parse a bytes string into a Message object model. - email.message_from_file() —
message_from_file(fp, *args, **kws)— Read a file and parse its contents into a Message object model. - email.message_from_string() —
message_from_string(s, *args, **kws)— Parse a string into a Message object model.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/base64
- tuts/python3/stdlib_module/binascii
- tuts/python3/stdlib_module/doctest
- tuts/python3/stdlib_module/email/message_from_binary_file
- tuts/python3/stdlib_module/email/message_from_bytes
- tuts/python3/stdlib_module/email/message_from_file
- tuts/python3/stdlib_module/email/message_from_string
- tuts/python3/stdlib_module/encodings
- tuts/python3/stdlib_module/ftplib
- tuts/python3/stdlib_module/html
- tuts/python3/stdlib_module/http
- tuts/python3/stdlib_module/imaplib
- tuts/python3/stdlib_module/ipaddress
- tuts/python3/stdlib_module/json
- tuts/python3/stdlib_module/mailbox
- tuts/python3/stdlib_module/mimetypes
- tuts/python3/stdlib_module/nturl2path
- tuts/python3/stdlib_module/poplib
- tuts/python3/stdlib_module/quopri
- tuts/python3/stdlib_module/smtplib
- tuts/python3/stdlib_module/socketserver
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/urllib
- tuts/python3/stdlib_module/uuid
- tuts/python3/stdlib_module/webbrowser
- tuts/python3/stdlib_module/wsgiref
- tuts/python3/stdlib_module/xml
- tuts/python3/stdlib_module/xmlrpc
Related notes
- tuts/python3/stdlib_module/base64 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/doctest Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/email/message_from_binary_file Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/email/message_from_bytes Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/email/message_from_file Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/email/message_from_string Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/encodings Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/json Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/mailbox Linked here; links to this page; shared tags: #stdlib