Shane Dowling : OfflineImap to Thunderbird

Last week I moved from Debian to Linux Mint and setup Thunderbird as my new mail client(replacing mutt). Sadly for some reason Thunderbird started tanking and wiped all the mails from my mailserver without actually downloading anything.

I could've restored the mails from a backup but instead I figured I'd take my emails offline and use Thunderbird as my primary mail client. I had a backup of my mails from OfflineImap but the problem was that OfflineImap uses the MailDir format and Thunderbird didn't support it.

Luckily Thunderbird did support maildir and I modified an existing MailDir to Mbox converter written in Python to work with Offlineimaps potentially infinitely recursive folder format. Hopefully it helps someone else.

#!/usr/bin/env python
 Shane Dowling, 8 Feb 2013
 Frédéric Grosshans, 19 January 2012
 Nathan R. Yergler, 6 June 2010

This file does not contain sufficient creative expression to invoke
 assertion of copyright. No warranty is expressed or implied; use at
 your own risk.

— -

Uses Python's included mailbox library to convert mail archives from
 maildir [] to
 mbox [] format, icluding subfolder.

See []( for
 full documentation on this library.

— -

$ python [maildir_path] [mbox_filename]

[maildir_path] should be the the path to the actual maildir (containing new,
 cur, tmp, and the subfolders, which are hidden directories with names like

[mbox_filename] will be newly created, as well as a [mbox_filename].sbd the

import mailbox
 import sys
 import email
 import os

def maildir2mailbox(maildirname, mboxfilename):
 slightly adapted from,
 Nathan R. Yergler, 6 June 2010
 and Frédéric Grosshans, 19 January 2012

 # open the existing maildir and the target mbox file
 maildir = mailbox.Maildir(maildirname, email.message_from_file)
 mbox = mailbox.mbox(mboxfilename)

# lock the mbox

# iterate over messages in the maildir and add to the mbox

# close and unlock

#Creates the main mailbox
 if not os.path.exists(mboxdirname): os.makedirs(mboxdirname)

# Iterate over all folders
 def folder_iterate(dirname, mboxname):
 listofdirs=[dn for dn in os.walk(dirname).next()[1] if dn not in ['new', 'cur', 'tmp']]
 curpath=os.path.join(*[dn+'.sbd' for dn in curlist if dn])
 if not os.path.exists(curpath): os.makedirs(curpath)
 print('no maildir folders')
 folder_iterate(os.path.join(dirname,curfold), mboxname)

folder_iterate(dirname, mboxname)