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.
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))
outfile.write(str(msg))
outfile.write(ānā)
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.
Doesn’t work for me:
[fons@betsy ~]$ python ./mailvonv.py Maildir output.mbox
Traceback (most recent call last):
File “./mailvonv.py”, line 6, in ?
outfile = file(sys.argv[1], “w”)
IOError: [Errno 21] Is a directory: ‘Maildir’
Sorry about that; the code had a bug in the
sys.argvindexing. I’ve updated the code to correct that issue.still doesn’t work bro :(
File “/home/baumi/bin/maildir2mbox.py”, line 6
SyntaxError: Non-ASCII character ‘\xe2′ in file /home/baumi/bin/maildir2mbox.py on line 6, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details