Creating MH directories from Maildir


How to convert maildir to MH? The script on this page - which "converts" a Maildir to MH folder - is one I wrote myself for a specific purpose and is put here primarily for my own benefit. Any use others make of it is entirely at their own risk. In particular it will certainly need editing to your own particular setup. You have been warned.

The Problem:
Your email is kept on a central server which uses IMAP and contains a large number of emails. You have a dual boot linux/windows laptop and wish to have offline access to your email which will allow you to deal with your email under both linux and windows if necessary, keeping only one copy of your emails on the laptop which is accessible to both linux and windows. You do not want to use the mbox system since for very large mailboxes this gets very cumbersome and is not good for any incremental backup system.

My resolution:
Store a local copy of your email under linux in Maildir format (1 file per message) which is robust and is amenable to incremental backup systems. Access to this copy can be achieved via a local imap server (I use dovecot) so works for any linux email client. Use offlineimap to sync your local copy with the main server. However, you still need to get this to be accesible to a windows system and there are a couple of problems with that. The first is that the Maildir structure does not work well with windows filesystems because ":" is a forbidden character in filenames but is part of the Maildir spec. The second is related to the first and is that there are no windows email clients that work directly with an unmodified Maildir storage system. My resolution is to use ext2ifs to mount my linux partitions under windows. I then use a script to create a hard linked copy of the maildir files in MH format (which is also 1 file per message). Being hard linked it of course takes very little disk space to contain this second copy of the emails. There are a number of clients which will work directly with MH format under windows and I prefer sylpheed. The only remaining problem is the need for a script which will create the MH folders from the maildir system.

My script to create MH folders from maildir:
This is a copy of my maildir2mh script. You will need to modify it for your own use.

What does it do?:
This script takes a maildir folder (by default assumed to be in ~/Maildir but can be overridden with the -i option). It then creates a MH folder (by default assumed to be in ~/Mail but can be overridden with the -o option). Now since there are multiple ways of defining maildir's and their mapping to the appropriate directories in MH format then you need to modify the script appropriately. There is an array which defines the mapping via keys. In fact it has two definitions as it is assumed that you will have a number of folders which do not require regular updating and so these can be specified with the -f option for a full creation. You also need to specify the trash folder.

The script also attempts to create a .sylpheed_mark file which records which emails have been read and replied to (this works with sylpheed version 2.5.0 and 3.0.0). It will also sort the MH folder by date using the external command sortm which is part of the nmh suite. You can skip this behaviour using the -q option when the MH folders will be sorted by mtime which may be less accurate but I have always found to work pretty well.

Finally, the script has a -p option which attempt to preserve any existing MH structure it finds (and does not delete the .sylpheed_cache file). It then looks for files in the maildir which only have 1 hard link and adds then to the appropriate MH folder with file numbers beyond the previous ones. It does recreate the .sylpheed_mark file for all emails (thereby updating reply labels etc).