Networking and Internet Modules

mailbox

Reads and modifies local mailbox formats such as Maildir and mbox. These containers provide message keys and format-specific storage operations; concurrent access may require locking. The example creates an isolated temporary mailbox, not a real user mailbox.

Formal Syntax & Structure

python syntaxpython
import mailbox

Return Value

module

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

Working Example

Example Snippetpython
import mailboxfrom tempfile import TemporaryDirectoryfrom pathlib import Pathwith TemporaryDirectory() as folder:    box = mailbox.Maildir(Path(folder)/"mail")    try:        box.add(mailbox.MaildirMessage("Subject: Note\n\nhello"))        print(len(box))    finally:        box.close()
Python Reference