Maildir to Mbox

If you’re interested in converting maildir to mbox, you may want to check out my `blog post <>`_ with an updated, simplified version of this script.

UPDATE (6 June 2010): Thanks to everyone who pointed out the errors introduced when I imported this from my old wiki software. The curly quotes have been removed and I replaced "n" with "\n" in line 15.

UPDATE (3 Feb 2010): Corrected a bug in the code; the output file is sys.argv[-1], not sys.argv[1].

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.

# -*- coding: utf-8 -*-

import mailbox
import sys
import email

mdir = mailbox.Maildir(sys.argv [-2], email.message_from_file)
outfile = file(sys.argv[-1], 'w')

for mdir_msg in mdir:
    # parse the message:
    msg = email.message_from_string(str(mdir_msg))


This script could be used as follows:

$ python 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.

date:2008-11-13 17:37:00