This documentation is for Dovecot v2.x, see wiki1 for v1.x documentation.

Converting between mailbox formats

Converting from MBX to MBOX

If you are using UW-IMAP and using the MBX format, you will need to convert it to MBOX format. The conversion process isn't pretty, but here is a script that works. You will need to get and compile the mailutil program from the UW-IMAP web site.

WARNING: Script versions listed here before 2006-05-18 11:40 UTC converted into mbx rather than mbox format, i. e. older versions did the opposite of what they were supposed to do. Use the version below:

# Written by Marc Perkel - public domain
# overhauled by Matthias Andree, 2006
# Usage: mbx-convert <filename>
# This code assumes there a user named "marc" with the primary group "marc".
# Change to any real user on your system.
# Yes - it look bizzare - but it gets the job done
# abort on error
set -e
user=marc
group=marc
homedir=/home/$user
if [ $# -ne 1 ] ; then
  echo >&2 "Usage: $0 <filename>"
  exit 1
fi
# set up automatic cleanup
trap 'rm -f "${homedir}"/in.$$ "${homedir}"/out.$$' 0
# First copy to users home dir and make the user the owner
cp "$1" "${homedir}/in.$$"
chown "$user":"$group" "${homedir}/in.$$"
# Run mailutil to convert as the user other than root
# mailutil requires this
su "$user" -c "mailutil copy in.$$ \#driver.unix/out.$$"
# create new file with same permissions/owner as old
cp -p "$1" "${1}.new"
# cat instead of copy leaves the original owner and permissions alone
if cat "${homedir}/out.$$" >"${1}.new" ; then
  # cat succeeded, rename file into place
  mv "${1}.new" "$1"
else
  # cat failed, remove temp file
  rm -f "${1}.new"
  exit 1
fi

Make a copy of some folders and test it first. Once you are satisfied that it works then:

User comments:

Converting from MBOX to Maildir

[http://batleth.sapienti-sat.org/projects/mb2md/ mb2md] is a script which can convert the files for you.

Check also the User-Contributed Maildir Support section [http://www.qmail.org/top.html#usersoft on the qmail community site] for more choices.

Example (user's mail in ~someuser/mail and inbox in /var/mail/someuser):

cd ~someuser
mb2md-3.20.pl -s mail -R
mb2md-3.20.pl -m -d /var/mail/incoming/someuser
mv mail mail.old

Now the mail will be in ~someuser/Maildir.

If you are migrating from UW-IMAP, you also need to do:

cp -p .mailboxlist Maildir/subscriptions

Note that maildir handles subdirectories differently. You need to replace "/" with "." in the subscriptions file:

sed "s/\//\./" subscriptions > subscriptions.new
mv subscriptions.new subscriptions

Don't forget to update mail_location in dovecot.conf (as needed)

User comments: * If you have sub-folders within sub-folders where you store your mail. For example "Friends/Close Friends/Johns-Mail" you have to replace both "/" with a "." Then the sed commmand should be:

Converting from Maildir to MBOX

This is especially helpful if you want to archive your mail to a single file for storage on a CD, a PC, etc. But it can also be helpful if you want to use MBOX with dovecot.

Use the reformail program that comes with [http://www.courier-mta.org/maildrop/ maildrop]. You can also use the formail program that comes with [http://www.procmail.org/ procmail]. Here is a simple script showing how this works.

To use it, adjust the script to invoke the right command according to your system.

Then cd to the user's home directory (one level above Maildir) and run the script with two arguments: the mailbox name (You can use "." for the top-level folder), and the output mbox filename, for example:

cd ~hans
perl dw-maildirtombox.pl . >/tmp/hans-inbox
perl dw-maildirtombox.pl Sent >/tmp/hans-sent

WARNING: versions of the script provided here before 2006-05-18 11:40 UTC did not save the new/ directory contents, i. e. they missed unread messages!

# dw-maildirtombox.pl
# dw = Dovecot Wiki :-)
# NOTE! The output file must not contain single quotes (')!
# figure out which program to run
$cmd="reformail -f1";
system("$cmd </dev/null >/dev/null 2>/dev/null") == 0 or $cmd="formail";
system("$cmd </dev/null >/dev/null 2>/dev/null") == 0
or die "cannot find reformail or formail on your \$PATH!\nAborting";
$dir=$ARGV[0];
$outputfile=$ARGV[1];
if (($outputfile eq '') || ($dir eq ''))
{ die "Usage: ./archivemail.pl mailbox outputfile\nAborting"; }
if (!stat("Maildir/$dir/cur") || !stat("Maildir/$dir/new"))
{ die "Maildir/$dir is not a maildir.\nAborting"; }
@files = (<Maildir/$dir/cur/*>,<Maildir/$dir/new/*>);
foreach $file (@files) {
  next unless -f $file; # skip non-regular files
  next unless -s $file; # skip empty files
  next unless -r $file; # skip unreadable files
  $file =~ s/'/'"'"'/;  # escape ' (single quote)
  $run = "cat '$file' | $cmd >>'$outputfile'";
  system($run) == 0 or warn "cannot run \"$run\".";
}