mh2mbox.sh: Convert MH Mail Folders to MBOX Files

By Eric Lathrop on

I've wanted to switch my email program to Mozilla Thunderbird for a while, but I always gave up when trying to import my old mail. My old email client used the MH mail format, which Thunderbird cannot easily import. Luckily, I found a blog post that describes how to use the packf command to convert MH email to the MBOX mail format that Thunderbird uses.

I spent some time this weekend writing a shell script to process all folders and subfolders automatically. The script generates a parallel "mbox" folder structure that you should be able to copy into your Thunderbird profile and use directly. I copied the contents of the generated "mbox" folder to "~/.thunderbird/sjx2fsf.default/Mail/Local Folders/".

Ubuntu Prerequisite

If you're running Ubuntu, you'll need the "nmh" package, but the package will fail to install with this message:

The hostname -f command returned: $1

Your system needs to have a fully qualified domain name (fqdn) in
order to install the var-qmail packages.

The fix is to temporarily set up a FQDN by editing /etc/hostname. Change line 2 from:

127.0.1.1	mymachine

to:

127.0.1.1	mymachine.example.com mymachine

Once you've done this, you can install the nmh package:

sudo apt-get install nmh

The Script

Save this script in yout ~/Mail folder, then execute it. It will generate a new "mbox" folder with the converted mail.

#!/bin/bash
#
# mh2mbox.sh
# By Eric Lathrop <eric@ericlathrop.com>
#
# Convert MH mail folders to MBOX files
# Useful for switching to Mozilla Thunderbird
#
# Copy this script to ~/Mail/ and execute.
#
# Requires the "packf" command from the nmh package in Ubuntu

DEST="mbox"
# Note: in Ubuntu, packf gets installed under /usr/bin/mh/ so it's not normally in the $PATH
PACKF="/usr/bin/mh/packf"

(IFS='
'
for DIR in `find . -type d`; do
	if [ "$DIR" = "." ]; then
		continue
	fi
	if [ "$DIR" = "./$DEST" ]; then
		continue
	fi

	CONVDIR="."
	if [ `dirname $DIR` != "." ]; then
		CONVDIR=`dirname "$DIR" | sed -e "s/\.\///" | sed -e "s/\//.sbd\//g"`'.sbd'
	fi
	mkdir -p "$DEST/$CONVDIR"

	MAILBOX=`basename $DIR`
	MBOXPATH="$DEST/$CONVDIR/$MAILBOX"

	# create an empty file in case there aren't any messages
	# this fixes a thunderbird problem where it won't show subfolders
	# for a folder that doesn't have a matching file
	touch $MBOXPATH

	yes | $PACKF +"$DIR" -mbox -file $MBOXPATH
done
)

Post-processing

I ran into a strange issue with Thunderbird displaying the sender email address in the subject line. After much head scratching, I determined that there were strange "S:" headers that Thunderbird was misinterpreting as subject lines. A quick grep filtered those out:

grep -v "^S:" Inbox >Inbox.new
mv Inbox.new Inbox