mailbox provides interfaces for local formats including mbox, Maildir, MH, Babyl, and MMDF. It manages the message collection while the email package parses headers and MIME content.
Iterate over an mbox file
import mailbox
box = mailbox.mbox("archive.mbox", create=False)
try:
for message in box:
print(message.get("Subject", "(no subject)"))
finally:
box.close()
Separate storage from parsing
A mailbox format determines storage, not whether content is trustworthy. Headers may be absent or malformed, and attachments need size limits and safe filenames. Never render received HTML without sanitization.
Protect modifications
When writing, use locking appropriate to the format and release it with unlock() and close() in a finally block. Back up before conversions and test large copies. For remote access, keep protocol synchronization separate from parsing.
Keep learning
Continue with email messages and SMTP in Python. The official Python documentation, accessed July 22, 2026, describes formats, locking, and message representations.