-   Linux - General (
-   -   A script to convert maildir to mailbox format (

i_s 11-09-2005 04:15 PM

A script to convert maildir to mailbox format
This is a script that you can use to convert your mail folders from maildir format to mbox format. This is useful if you want to move your mail between programs like kmail and evolution or thunderbird.

I did not creat this script my self, and I do not claim it to my self. I came upon it while googling to solve my mdir to mbox conversion problems and it worked great. It is a useful script and I thought some people might find it helpful too.

The script file is named




#! /bin/sh
# Get a directory name as input and convert all mail files inside
# to mbox format
# NOTE: processing of subdirectories not yet implemented correctly:
#      all mails in subfolders are put into the same mbox
#      (it would be better if an mbox file will be generated for
#      each subfolder)
# NOTE: calculation of message date in case of 'From:' doesn't recognise
#      time zones
# History:
# Feb 06 2001 Joerg Reinhardt
# - first edition
# Feb 07 2001 Joerg Reinhardt
# - added usage output
# Feb 12 2001 Joerg Reinhardt
# - mails not containing a 'From:' field but an 'X-From-Line:' or a
#  'Reply-To:' field are now recognised and also processed (e.g. put into
#  the mbox file); this works fine for all my mails
# - added progress information
# - warning about corrupt files is now written to stderr

# check for argument or help argument respectively
if [[ ($1 == "") ||
    ($1 == "-h") ||
    ($1 == "--help") ||
    ($1 == "-help") ]]; then
    echo "Usage: "$0" <Xfmail-mail-directory>";

# check if parameter is a directory
if [[ -d $1 ]]; then
# set target filename
    dirname=`echo $1 | awk '{while(substr($0,length($0),1)=="/"){$0=substr($0,1,length($0)-1);}print $0;}'`;

# check if directory is empty
    if [[ `find $dirname -type f` == "" ]]; then
        echo $dirname": directory empty."
        exit 1;

# prevent automatic overwriting of target
    if [[ -e $mboxfile ]]; then \
        dialogtext="Write file "$mboxfile"?";
        if dialog --yesno "$dialogtext" 10 60; then
            rm -vf $mboxfile;
            clear; exit 1;

    echo "writing xfmail mail directory '$1' to '$mboxfile'.";

# collect files inside Xfmail mail-directory and produce MBOX format
# target file
    for i in `find $1/* -type f`; do
# output progress information
        echo -n -e \\r"                                                                              "
        echo -n -e \\rprocessing $i
# look for senders email address in the order
# 'From:'
# 'X-From-Line:'
# 'Reply-To:'
        fromline=`grep 'From:' $i`;
# parse 'From:' field
        from=`echo $fromline | awk 'BEGIN{FS="<";}{if($0~/</) {pos=index($2,">");if(pos!=0) {print substr($2,1,pos-1);}} else {pos=index($0,":");print substr($0,pos+1);}}'`;
        if [[ $from == "" ]]; then
            fromline=`grep 'X-From-Line:' $i`;
            from=`echo $fromline | awk 'BEGIN{FS="Line:";}{print $2;}'`;
            if [[ $from == "" ]]; then
                fromline=`grep 'Reply-To:' $i`;
# parse 'Reply-To:' field
                from=`echo $fromline | awk 'BEGIN{FS="<";}{if($0~/</) {pos=index($2,">");if(pos!=0) {print substr($2,1,pos-1);}} else {pos=index($0,":");print substr($0,pos+1);}}'`;
                if [[ $from == "" ]]; then
                    echo "WARNING: "$i": no 'From:' nor 'X-From-Line:' nor 'Reply-To:' field found." >&2;
        if [[ $shortfromflag == "true" ]]; then
# parse date field
            dateline=`grep 'Date:' $i`;
            if [[ $dateline == "" ]]; then
# set dummy date if no date field found
                dateline="Date: Thu, 01 Jan 1970 00:00:00 +0000 (GMT)";
            weekday=`echo $dateline | awk '{gsub(/,/,"",$2);print $2;}'`;
            day=`echo $dateline | awk '{print $3;}'`;
            month=`echo $dateline | awk '{print $4;}'`;
            year=`echo $dateline | awk '{print $5;}'`;
            time=`echo $dateline | awk '{print $6;}'`;
            diffGMT=`echo $dateline | awk '{print $7;}'`;
            timezone=`echo $dateline | awk '{print $8;}'`;

# output MBOX mail header
            echo "From " $from $weekday $month $day $time $year >> $mboxfile;
# output long MBOX mail header found in 'X-From-Line:' field
            echo $from >> $mboxfile;

# output mail itself
        cat $i >> $mboxfile;
    echo $1": not a directory.";

LinuxLala 11-10-2005 12:05 PM

i_s, thanks for sharing this. Could you also please post the link to the webpage you got this script from so that the original author gets proper mileage.


i_s 11-10-2005 02:05 PM

I don't really remember the webpage I downloaded it from, its been almost 3 weeks since I downloaded it. However, the file has some comments regarding the history of the file and they are included with the script shown.

I also googled it again and found some info regarding it on, the info is compatible with the comments in the script., a shell script by Jörg Reinhardt <joeyhh(at)> to convert xfmail folders to mbox (which is used by KMail).



LinuxLala 11-11-2005 05:04 AM

Yup, that should do :) Thanks.

christian.noack 01-04-2008 03:17 PM

Scripts works perfectly for transition from Courier to UW Imap
Last week I moved my mail server from Courier on Debian Linux to UW IMAP on OS X Leopard (using the excellent administration tool Mail Serve from Bernard Teo - look for MailServe on the homepage of For this I had to convert from MailDir to mbox format. I worked perfectly for me without any trouble.

Thanks for your work,


Dr3am3r 05-31-2008 12:09 PM

Hello everyone,

I wrote a similar script in PHP which can work with very big maildir folders (does not keep much in memory before writing to the mbox file thus not exhausting the php memory limits) and copes with many Header information like "X-From-Line:" and "Reply-To:" in case "From:" is missing.

I have tested it on my ubuntu 8.04 with PHP 5.2.4 and works fine.

The file is located at:

Dr3am3r 11-26-2008 09:53 AM

The above script have been updated and supports subdirectory convertion too. For example it can convert qmail's maildir or kmail's maildir formats (the latter supports sub-directories) to an mbox structure.

The script can be found at ( ) or

granth 12-14-2009 03:20 PM

I recently used this script when migrating from Kmail to Mutt.

It works pretty good, but choked on messages with different-case from, x-from-line, and reply-to fields.

Luckily, this is easy to fix. Just add the -i switch to the grep commands, so they ignores case.


fromline=`grep -i 'From:' $i`;

All times are GMT -5. The time now is 10:33 AM.