Google Site SearchFN Site Search FN Blog Login FN Blog Login
Site Navigation:
 
 

Convert qmail-style Maildir into standard *Nix style mbox

by Brian Zammit on Jul 15, 2004

Original script location: http://www.systemsaligned.com/ossprojects/maildir2mbox

There are a number of utilities to convert from mbox to Maildir, but only one working utility to convert the other way around from Maildir to mbox. Qmail itself comes with the working Maildir to mbox conversion utility, intuitively named maildir2mbox, though it is limited when working with non-standard Maildir's.  The process for converting subdirectories of Maildir's for a large number of users is very time consuming and clumsy at best.

Recently a client required Systems Aligned to convert of a number of non-standard Maildir's with subdirectories, so I wrote this script to help speed up the conversion process. This utility uses the Qmail maildir2mbox program as a base, and adds simple intelligence to automatically convert all the Maildir folders in a given users directory into mbox format. Credit goes to Charles Cazabon from the qmail@list.cr.yp.to mailing list for getting the script started.

With the hopes more people will benefit from Systems Aligned's work in the private sector on this script, it is being released to the public under GPL.

This script was used on a production server running Red Hat Linux 9. The script should work unmodified on all Red Hat versions with /var/spool/mail/ as the default mbox mail spool, and on most other Linux distributions with little modification. A precompiled maildir2mbox executable is included with this how-to in case the system you are working on doesn't have qmail already installed. In this case, you would have to compile and install qmail to get the maildir2mbox executable.

********************
DISCAIMER
The script works well for me, though it may not work for you. I can not be held responsible for any damages it may inflict on your systems. But then again, you can read through the script to make sure its not going to do anything nasty, and provide any fixes it requires.
********************

  • Download the mbox-auto conversion program and the maildir2mbox executable from the links on this article. Save both files in the /usr/local/bin directory together. You can also save the files to any directory included in your users path.
  • Ensure regular users can access these files by running the following command as the root user:
# chmod +x /usr/local/bin/maildir2mbox /usr/local/bin/mbox-auto
  • nge to the user having the Maildir's you want to convert:
# su - username

  • Run the conversion utility:
$ mbox-auto
The mailboxes being converted are shown, along with the location where the mbox files have been created. Since this was written on Red Hat Linux 9,  the default mail location of /var/spool/mail is used for the main Inbox, while the users /home/username directory is used for any extra folders which may be found during the conversion process.</>
  • Follow the instructions from the output of the conversion program. Exit from the user back to a root user, and change the permissions on the /var/spool/mail/user file:
$ exit
# chown username:mail /var/spool/mail/username


Future enhancements may include modifying the utility to convert the Maildirs within every user in a /home directory, and automatically setting permissions of the mbox files.

I hope you find this script useful, and it saves you some time and sleep! Please contact me directly with any suggestions for improvement, code fixes, or questions.

  • Web Sites
  • mbox-auto Script
#!/bin/sh
#
#####################################################################
#
# mailbox-auto
# copyright (c) 2004 Systems Aligned Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# contact the author directly for more information at:
# bzammit at systemsaligned dot com
# Systems Aligned Inc.
# http://www.systemsaligned.com
#
#####################################################################
#
# history
#
# updated: july 08 2004
# version 0.2
# - first public release
# - added more comments to the code
# - added GPL licence text
# - fixed some spelling errors in echo text
#
# updated: june 23 2004
# version 0.1
# - internal release
# - Credit goes to Charles Cazabon from the qmail@list.cr.yp.to
# mailing list for getting the script started.
#
# start script
#
START=~/
# Check if the mail folder exists, OR create the folder.
[ -d "$START"mail ] || mkdir "$START"mail

echo "This is the start: $START"

#
# Find all the Maildir's in the directory by looking for cur,new,tmp sub direcories
#
find $START -type d | while read d ; do
[ -d "$d/cur" -a -d "$d/new" -a -d "$d/tmp" ] || continue
echo Processing "$d"
if [ "$d" = "$START"Maildir ]; then
#
# Enter this loop if the files found are for the Inbox
#
echo "$d is the first directory, your Inbox"
#
# Call the maildir2mbox conversion program with some environment variables set
#
env MAILDIR="$d" MAIL="$START""$USER" MAILTMP="$START"mbox.tmp maildir2mbox
chmod 660 "$START""$USER"
# chown $USER:mail "$START""$USER"
echo " "
echo "Change to the root user and change the ownership on the mailbox:"
echo "# chown $USER:mail /var/spool/$USER"
echo " "
#
# Move the new mbox file to the Red Hat 9 default location for mail spools
#
mv "$START""$USER" /var/spool/mail/
else
#
# Enter this loop if the files found are for subdirectories other than Inbox
#
USERDIR=`basename "$d" | sed 's/\.//'`
echo "Directory is: $d"
# echo "Directory without period is: $USERDIR"
echo Saved mail file is: "$START"mail/"$USERDIR"
#
# Call maildir2mbox conversion program with some environment variables set
#
env MAILDIR="$d" MAIL="$START"mail/"$USERDIR" MAILTMP="$START"mbox.tmp maildir2mbox
fi
done


# Mailboxlist block
#
# Create an mbox .mailboxlist file so programs know what extra
# directories are used, and where they can be found.
#
[ -f "$START".mailboxlist ] && rm "$START".mailboxlist

echo " "
for FILE in $( find "$START"mail/ -type f )
# The above string needs to be fixed for directories with whitespace.
# It breaks at the moment, by creating a directory for each
# whitespace separated word in an mbox file name.
do
MAILBOXFILE=mail/`basename "$FILE"`
echo .mailboxlist entry: "$MAILBOXFILE"
echo "$MAILBOXFILE" >> "$START".mailboxlist
done
exit 0
#
# end script
#