Call us Toll-Free:
Live ChatEmail us


How to convert mbox mailboxes to the maildir format

Adrian Singer, 09-20-2009
This weekend we converted all SPI mutlihomed hosting email mailboxes from the mbox format to maildir.

What is the difference between mbox and maildir?

Mbox is the traditional way of storing mail messages in the Unix world. In this format, a regular text file which serves as the mail user's mailbox file is created.

Pros: Format is universally supported, Appending a new mail into the mailbox file is fast, Searching text inside a single mailbox file is fast.

Cons: Has file locking problems, Has problems when used with network file systems, Format is prone to corruption.

Maildir is a new way of storing mail messages. In this format, a directory usually named Maildir is created for each mail user. Under this directory are three more directories named new, cur and tmp.

Pros: Locating, retrieving and deleting a specific mail is fast, Minimal to no file locking needed, Immune to mailbox corruption (assuming the hardware will not fail).

Cons: Some filesystems may not efficiently handle a large number of small files, Searching text, which requires all mail files to be opened is slow.

Why we chose Maildir

We've decided to upgrade to Maildir because its more reliable (entire mailbox never fully corrupts) and thanks to no locking it is noticeably much faster to access your mail over Maildir.

Ongoing mbox file locking issues were driving us nuts.

SPI is using the Dovecot mail server on all machines. Dovecot is one of the highest performing IMAP servers. Luckily it supports both mbox and maildir, so all we had to do was: Convert old email messages from mbox to maildir.


Mb2md is a Perl script that takes one or more Mbox format mailbox files in a directory and convert them to Maildir format mailboxes.

Replace USERNAME below with the unix username whose mailbox you are converting

Step 1: Set environment variables for Mb2md

setenv MAIL /var/mail/USERNAME

Step 2: Convert the inbox folder and create Maildir folder

sudo -u USERNAME mb2md -m

Step 3: Convert other folders the user created

sudo -u USERNAME mb2md -s /home/USERNAME/mail/ -R

Step 4: Copy IMAP subscriptions

cp /home/USERNAME/mail/.subscriptions /home/USERNAME/Maildir/subscriptions

Step 5: Tell sendmail to deliver new mail to the user's /Maildir

cd /home/USERNAME
echo '"|IFS='"' '"' && exec /usr/local/bin/procmail -f || exit 75 #YOUR EMAIL NAME"' >> .forward
chmod 644 .forward
echo "DEFAULT="'$'"HOME/Maildir/" >> .procmailrc
chmod 644 .procmailrc
chown USERNAME:USERNAME .forward
chown USERNAME:USERNAME .procmailrc

Step 6: Delete old mbox files

rm /var/mail/USERNAME
rm -fdr /home/USERNAME/mail

Testing & Troubleshooting

To test, use sendmail to send a test message to your user:

sendmail USEREMAIL
subject: Test, please ignore

This is a test

If all works well, your email message will be delivered to the user's Maildir.

If you see the mail appended to /var/mail/USERNAME (do a tail -f /var/mail/USERNAME), this means sendmail is failing to invoke .procmailrc and deliver the email message to Maildir.

This is usually due to the /home or /home/USERNAME folders not having the correct file permissions.

Run this command to troubleshoot:

sendmail -v -Am -d11 USERNAME < /dev/null

Marcio Carneiro, 02-04-2018
I get the error:

MacBook-MAC:mail marcio$ mb2md -m
-bash: mb2md: command not found
MacBook-MAC:mail marcio$ python -m
File "", line 383
use strict;
SyntaxError: invalid syntax
And I do not know python.

Can you help me?

Thank you.
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

Post your comments

Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.

About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions