Mbox

From Christoph's Personal Wiki
Jump to: navigation, search

Mbox is a generic term for a family of related file formats used for holding collections of email messages. All messages in an mbox mailbox are concatenated and stored as plain text in a single file. Each message starts with the four characters "From" followed by a space (the so named "From_ line") and the sender's email address. RFC 4155 defines that a UTC timestamp follows after another separating space character.

Working with mbox files

  • Create an example mbox file using Python:
import mailbox
import email.utils

from_addr = email.utils.formataddr(('Author', 'author@example.com'))
to_addr = email.utils.formataddr(('Recipient', 'recipient@example.com'))

mbox = mailbox.mbox('example.mbox')
mbox.lock()
try:
    msg = mailbox.mboxMessage()
    msg.set_unixfrom('author Thus Feb  16 17:10:02 2017')
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = 'Sample message 1'
    body = "This is the body.\n"
    body += "From (should be escaped).\n"
    body += "There are 3 lines.\n"
    msg.set_payload(body)
    mbox.add(msg)
    mbox.flush()

    msg = mailbox.mboxMessage()
    msg.set_unixfrom('author')
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = 'Sample message 2'
    msg.set_payload('This is the second body.\n')
    mbox.add(msg)
    mbox.flush()
finally:
    mbox.unlock()

print open('example.mbox', 'r').read()

Run the above Python script:

$ python mbox_create.py
From MAILER-DAEMON Thu Feb 16 17:15:22 2017
From: Author <author@example.com>
To: Recipient <recipient@example.com>
Subject: Sample message 1

This is the body.
>From (should be escaped).
There are 3 lines.

From MAILER-DAEMON Thu Feb 16 17:15:22 2017
From: Author <author@example.com>
To: Recipient <recipient@example.com>
Subject: Sample message 2

This is the second body.
  • Print out all of the Subject fields:
import mailbox

mbox = mailbox.mbox('example.mbox')
for message in mbox:
    print message['subject']
$ python mbox_read.py
Sample message 1
Sample message 2
  • Remove an existing message from the mbox file:
#!/usr/bin/env python
# Removes an existing message from a mbox file
import mailbox

mbox = mailbox.mbox('example.mbox')
to_remove = []
for key, msg in mbox.iteritems():
    if '2' in msg['subject']:
        print 'Removing:', key
        to_remove.append(key)
mbox.lock()
try:
    for key in to_remove:
        mbox.remove(key)
finally:
    mbox.flush()
    mbox.close()

print open('example.mbox', 'r').read()
$ python mbox_remove.py
Removing: 1
From MAILER-DAEMON Thu Feb 16 18:30:42 2017
From: Author <author@example.com>
To: Recipient <recipient@example.com>
Subject: Sample message 1

This is the body.
>From (should be escaped).
There are 3 lines.