Converting mbox to Maildir

Neil Wilson neilw at
Mon Apr 23 17:00:52 IST 2007

Claude Gagné wrote:
> Hi,
> I need to convert emails in mbox to Maildir format. Anyone has something 
> to suggest ?
> Thanks !

I use the two scripts attached to convert all the mailboxes in one go.

Just check the paths in and rename files accordingly(remove .txt etc :)

I normally put both of these under /usr/local/bin/
then the only thing you need to change in it is...


Leave as is if your mboxes are here...

Change to the location of your Maildirs, mine are /home/user/Maildir so I just change this to...

Once it's done you need to change the permissions on your homedirs as the users and the 
group gets changed to root.

I use this all the time, hope it helps.


This email and all contents are subject to the following disclaimer:

-------------- next part --------------
#-This all correct - maybe change /home/vmail.
#-After running this chown vmail:vmail [vmail dir]
# 2002 - Luciano Linhares Martins

# Patch do programa para converter as mensagens

# SPOOL - Diretório onde os e-mais vão estar armazenados

# Diretório base onde as mensagens no formato Maildir vão ficar

# Loop que pega as mboxs do spool e gera o Maildir
for user in `ls $spool`

# Maildir BASE - Diretório base do Maildir do usuário
# Maildir do Usuário

# Arquivo onde os e-mails dos usuários ficam armazenados no spool

export MAILDIR=$mdir
export MAIL=$tmail
export MAILTMP=/tmp/tmp

mkdir -p $mbase
cp $email $tmail
perl $prog

-------------- next part --------------
#-!!!!!!!!!!DO NOT EDIT THIS!!!!!!!!!!!!!!! - USE
# mbox2maildir: coverts mbox file to maildir directory - the reverse of
# maildir2mbox from the qmail distribution.
# Usage: mbox2maildir uses the same environment variables as maildir2mbox:
# MAILDIR is the name of your maildir directory; MAIL is the name of your
# mbox file; MAILTMP is ignored.  MAIL is deleted after the conversion.
# WARNING: there is no locking; don't run more than one of these!  you
# have been warned.
# based on convert-and-create by Russell Nelson <nelson at>
# kludged into this by Ivan Kohler <ivan at> 97-sep-17

require '';

local $SIG{HUP} = 'IGNORE';
local $SIG{INT} = 'IGNORE';
local $SIG{QUIT} = 'IGNORE';
local $SIG{TERM} = 'IGNORE';
local $SIG{TSTP} = 'IGNORE';

($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) =

die "fatal: home dir $dir doesn't exist\n" unless -e $dir;
die "fatal: $name is $uid, but $dir is owned by $st_uid\n" if $uid != $st_uid;

chdir($dir) or die "fatal: unable to chdir to $dir\n";
$spoolname = "$ENV{MAILDIR}";
-d $spoolname or mkdir $spoolname,0700
  or die("fatal: $spoolname doesn't exist and can't be created.\n");

chdir($spoolname) or die("fatal: unable to chdir to $spoolname.\n");
-d "tmp" or mkdir("tmp",0700) or die("fatal: unable to make tmp/ subdir\n");
-d "new" or mkdir("new",0700) or die("fatal: unable to make new/ subdir\n");
-d "cur" or mkdir("cur",0700) or die("fatal: unable to make cur/ subdir\n");

open(SPOOL, "<$ENV{MAIL}")
  or die "Unable to open $ENV{$MAIL}\n";
$i = time;
while(<SPOOL>) {
  if (/^From /) {
    $fn = sprintf("new/%d.$$.mbox", $i);
    open(OUT, ">$fn") or die("fatal: unable to create new message");
  s/^>From /From /;
  print OUT or die("fatal: unable to write to new message");

More information about the MailScanner mailing list