MBOX to Maildir conversion scripts

Frustrated with UW-IMAP, I recently decided to start migrating servers over to Courier IMAP. In examining the scripts for MBOX->Maildir migration available at the Qmail website, I found that none of the available scripts would preserve existing information about which messages had been read and which had been replied to. Further research suggested that the method used in these scripts might not reliably detect the beginnings of messages in mbox format files.

The following scripts deal with these issues. They are a little bit cobbed, largely because I am not a skilled perl hacker (but then, I have never claimed to be a skilled perl hacker.) They do get the job done, and for each server, they only need to be used once.

The scripts


convert-and-create is a Perl script derived from Russ Nelson's original. It differs in that it uses safecat and Procmail's formail to do the actual conversion. Grab a copy and put it whereever you want. Note that it assumes that the user mbox files are in /var/spool/mail; you will want to change this if you are using /var/mail (for example.)


maildir-script.sh is my own invention. It's not particularly efficent, but it only gets run once per user mbox file. It is called by convert-and-create. What it does is examine the status flags in each file and rename it properly with version 2 Maildir flags. Grab a copy and place it in root's $PATH, with permissions 700 or 744 or something like that. Don't let the license scare you, it's basically the BSD license.

Support tools

Like I said, you need safecat and procmail. You probably already have procmail installed, grab safecat and install it.

The process

Install the Maildir based imap/pop3 software of your choice, but don't switch it on just yet. You will want to shut down pop3 and imap services while performing the conversion. When done, you'll start up the new Maildir based imap/pop3 software instead of the old software.

You'll also want to shut down mail delivery to the mbox files while converting. You can safely switch your MTA over to Maildir delivery while doing the conversion; the Maildir algorithm handles this just fine. However, if you want to do a trial run, with the option of falling back to the old pop/imap server, you just want to shut delivery down for a little while.

Once you are sure that no other entities will be mucking about with the mboxes during the conversion, run the convert-and-create script. It will iterate over all the users, skipping ones for whom mail cannot be delivered. It will create Maildir hierarchies for all users who can receive email, and will convert the contents of /var/spool/mail for each user. It will not remove the old mbox files; if mail delivery is turned off, you can easily check the conversion and then decide if you want to turn UW-IMAP or the Maildir based IMAP back on.