On this page
mailbox
Prev: lzma | Up: stdlib_module | Next: marshal
Rel: email, json, mimetypes, base64, binascii
Ref: docs.python.org — mailbox
Tags: #public #python3 #stdlib
Import
import mailbox
Overview
Reader and writer for local on-disk email mailbox formats (mbox, Maildir, MH, Babyl, MMDF). Use mailbox to parse exported .mbox archives (like Google Takeout mail exports), inspect local Maildir spools, or convert between local mailbox formats alongside email.
Quick Start
import mailbox
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
mbox_path = Path(tmp) / "archive.mbox"
box = mailbox.mbox(mbox_path)
try:
msg = mailbox.mboxMessage()
msg["From"] = "alice@example.com"
msg["Subject"] = "Release notes"
msg.set_payload("v1.0 is live.")
box.add(msg)
box.flush()
finally:
box.close()
with mailbox.mbox(mbox_path) as reader:
subjects = [m["Subject"] for m in reader]
assert subjects == ["Release notes"]
Implementation Walkthrough
1. Safe Locking and Flushing on Updates
Single-file formats like mbox rewrite the entire file on flush(), so always lock before mutating and release in a finally block:
import mailbox
def delete_by_subject(mbox_path: str, target: str) -> int:
removed = 0
box = mailbox.mbox(mbox_path)
box.lock()
try:
for key, msg in list(box.items()):
if msg["Subject"] == target:
box.remove(key)
removed += 1
box.flush()
finally:
box.unlock()
box.close()
return removed
Gotchas & Boundaries
- Changes aren't saved until
flush()orclose(): Mutating anmboxin memory does nothing on disk until flushed (Maildirwrites individual files immediately, but still close cleanly). - Prefer
Maildirfor concurrent access:Maildirstores one file per message and avoids whole-file locking/rewrites;mboxis best treated as read-only or single-writer. - Legacy
emailmessage policy by default: Passfactory=lambda f: email.message_from_binary_file(f, policy=email.policy.default)when opening a mailbox if you want modernEmailMessageAPIs.
Classes & Objects
- mailbox.Babyl — An Rmail-style Babyl mailbox. (Methods:
.add(),.clear(),.close(),.discard(),.flush(),.get()) - mailbox.BabylMessage — Message with Babyl-specific properties. (Methods:
.add_header(),.add_label(),.as_bytes(),.as_string(),.attach(),.del_param()) - mailbox.Error — Raised for module-specific errors. (Methods:
.add_note(),.with_traceback()) - mailbox.ExternalClashError — Another process caused an action to fail. (Methods:
.add_note(),.with_traceback()) - mailbox.FormatError — A file appears to have an invalid format. (Methods:
.add_note(),.with_traceback()) - mailbox.Mailbox — A group of messages in a particular place. (Methods:
.add(),.clear(),.close(),.discard(),.flush(),.get()) - mailbox.Maildir — A qmail-style Maildir mailbox. (Methods:
.add(),.add_flag(),.add_folder(),.clean(),.clear(),.close()) - mailbox.MaildirMessage — Message with Maildir-specific properties. (Methods:
.add_flag(),.add_header(),.as_bytes(),.as_string(),.attach(),.del_param()) - mailbox.mbox — A classic mbox mailbox. (Methods:
.add(),.clear(),.close(),.discard(),.flush(),.get()) - mailbox.mboxMessage — Message with mbox-specific properties. (Methods:
.add_flag(),.add_header(),.as_bytes(),.as_string(),.attach(),.del_param()) - mailbox.Message — Message with mailbox-format-specific properties. (Methods:
.add_header(),.as_bytes(),.as_string(),.attach(),.del_param(),.get()) - mailbox.MH — An MH mailbox. (Methods:
.add(),.add_folder(),.clear(),.close(),.discard(),.flush()) - mailbox.MHMessage — Message with MH-specific properties. (Methods:
.add_header(),.add_sequence(),.as_bytes(),.as_string(),.attach(),.del_param()) - mailbox.MMDF — An MMDF mailbox. (Methods:
.add(),.clear(),.close(),.discard(),.flush(),.get()) - mailbox.MMDFMessage — Message with MMDF-specific properties. (Methods:
.add_flag(),.add_header(),.as_bytes(),.as_string(),.attach(),.del_param()) - mailbox.NoSuchMailboxError — The specified mailbox does not exist and won't be created. (Methods:
.add_note(),.with_traceback()) - mailbox.NotEmptyError — The specified mailbox is not empty and deletion was requested. (Methods:
.add_note(),.with_traceback())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/base64
- tuts/python3/stdlib_module/binascii
- tuts/python3/stdlib_module/email
- 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/lzma
- tuts/python3/stdlib_module/marshal
- 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/email 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/lzma Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/marshal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/mimetypes Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib links to this page; shared tags: #stdlib