This script demonstrates reading and writing an mbox style mailbox. This script is an mbox filter. It scans through an entire mbox and writes the messages to a new file. Each message is passed through a filter function which may modify the document or ignore it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#!/usr/bin/env python """This is an mbox filter. It scans through an entire mbox style mailbox and writes the messages to a new file. Each message is passed through a filter function which may modify the document or ignore it. The passthrough_filter() example below simply prints the 'from' email address and returns the document unchanged. After running this script the input mailbox and output mailbox should be identical. """ import mailbox, rfc822 import sys, os, string, re LF = '\x0a' def main (): mailboxname_in = sys.argv mailboxname_out = mailboxname_in + '.out' process_mailbox (mailboxname_in, mailboxname_out, passthrough_filter) def passthrough_filter (msg, document): """This prints the 'from' address of the message and returns the document unchanged. """ from_addr = msg.getaddr('From') print from_addr return document def process_mailbox (mailboxname_in, mailboxname_out, filter_function): """This processes a each message in the 'in' mailbox and optionally writes the message to the 'out' mailbox. Each message is passed to the filter_function. The filter function may return None to ignore the message or may return the document to be saved in the 'out' mailbox. See passthrough_filter(). """ # Open the mailbox. mb = mailbox.UnixMailbox (file(mailboxname_in,'r')) fout = file(mailboxname_out, 'w') msg = mb.next() while msg is not None: # Properties of msg cannot be modified, so we pull out the # document to handle is separately. We keep msg around to # keep track of headers and stuff. document = msg.fp.read() document = filter_function (msg, document) if document is not None: write_message (fout, msg, document) msg = mb.next() fout.close() def write_message (fout, msg, document): """This writes an 'rfc822' message to a given file in mbox format. This assumes that the arguments 'msg' and 'document' were generate by the 'mailbox' module. The important thing to remember is that the document MUST end with two linefeeds ('\n'). It comes this way from the mailbox module, so you don't need to do anything if you want to write it unchanged. If you modified the document then be sure that it still ends with '\n\n'. """ fout.write (msg.unixfrom) for l in msg.headers: fout.write (l) fout.write (LF) fout.write (document) if __name__ == '__main__': main ()
I find myself writing lots of little scripts to filter my mbox. I use this script as the basis of my mbox filters. The docs for the 'mailbox' and 'rfc822' modules shows how to read an mbox, but the docs don't show how to write to an mbox format. This is pretty easy to do, but it is not obvious, so here it is.
The 'process_messages()' function loops over each message in the input mailbox. Each message is passed to a filter function. In this example the filter is called 'passthrough_filter()' which does nothing but print the 'from' address of the message and then return the document unchanged. After running this script the input mailbox and output mailbox should be identical. The 'write_message()' function shows how to take an 'rfc822' message and write it to a file in mbox format.
I used this script as the basis of a tiny spam filter. I defined a filter function that accepts any message with a 'from' address in a list of addresses known to me. It rejects any message over 30KB from addresses that are not in the list. This does not get rid of all spam messages, but it does get rid of most of the ones that take a lot of space (mostly viruses and huge HTML advertisements).
Python 2.2 and above should use the 'email' module for creating new messages by hand. I used the 'rfc822' module because that is what is returned by the 'mailbox' module.