Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Convert maildirs (including subfolders) to mbox format
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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.
To run, save as and run:
$ 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
# 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
for msg in maildir:
# close and unlock
print(dirname +' -> ' +mboxname)
if not os.path.exists(mboxdirname): os.makedirs(mboxdirname)
listofdirs=[dn for dn in os.walk(dirname).next()[1] if dn not in ['new', 'cur', 'tmp']]
for curfold in listofdirs:
curpath=os.path.join(*[dn+'.sbd' for dn in curlist if dn])
if not os.path.exists(curpath): os.makedirs(curpath)
print('| ' +curfold +' -> '+curpath[:-4])

scrinzi commented Aug 6, 2012

Thanks, inspired me to find salvation after KMail did not work for me in Suse 12.1.
Had to modify, though, find a version that properly descends into KMail generated subdirectories here

Works great ! Thx !

works very well !

Hi! New to Python, new to Maildir, fighting to find a way of moving my old mailing list arquives to mbox. This script seems to be created to transform email accounts Maildir folders into mbox files, but it doesn't work to transform arquived lists into this format. Please, could you point me in the right direction? Do you know some script to do that job? Thanks for your answer! Thanks for your job!

Here is an updated version that seems to work better:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment