Difference between revisions of "Mbox"
From Christoph's Personal Wiki
(Created page with "'''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 a...") |
|||
| Line 2: | Line 2: | ||
==Working with mbox files== | ==Working with mbox files== | ||
| + | |||
| + | * Create an example mbox file using [[Python]]: | ||
| + | <pre> | ||
| + | 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() | ||
| + | </pre> | ||
| + | |||
| + | Run the above Python script: | ||
| + | $ python mbox_create.py | ||
| + | <pre> | ||
| + | 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. | ||
| + | </pre> | ||
| + | |||
| + | * Print out all of the Subject fields: | ||
| + | <pre> | ||
| + | import mailbox | ||
| + | |||
| + | mbox = mailbox.mbox('example.mbox') | ||
| + | for message in mbox: | ||
| + | print message['subject'] | ||
| + | </pre> | ||
[[Category:Linux Command Line Tools]] | [[Category:Linux Command Line Tools]] | ||
Revision as of 18:30, 16 February 2017
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']