Maildir to Mbox

I recently moved a large (20,000 messages) Archive mail folder from my IMAP server to my local workstation. The goal was to reduce load on the server, and improve search performance. I use Thunderbird as my mail client, and so needed to convert from Maildir (used by the IMAP server) to mbox (used by Thunderbird). Python includes some libraries for manipulating mailboxes and messages, and so I was able to put together a short script for doing the conversion.

   1 import mailbox
   2 import sys
   3 import email
   4 
   5 mdir = mailbox.Maildir(sys.argv[-2], email.message_from_file)
   6 outfile = file(sys.argv[-1], 'w')
   7 
   8 for mdir_msg in mdir:
   9 
  10     # parse the message:
  11     msg = email.message_from_string(str(mdir_msg))
  12     outfile.write(str(msg))
  13     outfile.write('\n')
  14 
  15 outfile.close()

This script could be used as follows:

  $ python mailconv.py Maildir output.mbox

To get the newly created Mbox file into Thunderbird, it's usually easiest to create a new local folder in Thunderbird, shut down the application and replace the file for the Folder in your profile directory with the new Mbox file.


CategoryProjects CategoryOneOff

Maildir to Mbox (last edited 2007-03-26 14:04:26 by NathanYergler)