Temps : Selon la boite mail
Pré-requis : testé avec Ubuntu, Fedora (mais possible avec d'autres Linux), Python

Intro

Le premier script ne sert qu'à convertir une boite sans dossier, le second cherche tous les sous dossiers pour une conversion complète. L'origine des deux scripts n'est pas de moi, mais j'ai repris le second pour l'améliorer et qu'il soit bien efficace (récursivité, respect de la casse, etc.).
Je vous les glisse en pièces jointes ;-)

Premier script (en python) : maildir2mbox.py

# -*- coding: utf-8 -*-
"""
maildir2mbox.py
Nathan R. Yergler, 6 June 2010
This file does not contain sufficient creative expression to invoke
assertion of copyright.  No warranty is expressed or implied; use at
your own risk.
---
Uses Python's included mailbox library to convert mail archives from
maildir [http://en.wikipedia.org/wiki/Maildir] to
mbox [http://en.wikipedia.org/wiki/Mbox] format.
See http://docs.python.org/library/mailbox.html#mailbox.Mailbox for
full documentation on this library.
---
To run, save as maildir2mbox.py and run:
$ python maildir2mbox.py [maildir_path] [mbox_filename]
[maildir_path] should be the the path to the actual maildir (containing new, cur, tmp);
[mbox_filename] will be newly created.
"""
import mailbox
import sys
import email
# open the existing maildir and the target mbox file
maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file)
mbox = mailbox.mbox(sys.argv[-1])
# lock the mbox
mbox.lock()
# iterate over messages in the maildir and add to the mbox
for msg in maildir:
mbox.add(msg)
# close and unlock
mbox.close()
maildir.close()

Second script (Bash) : mdir2mbox.sh

#!/bin/bash
# created by: Chris Backhouse
# modified by Mickael Martin
# date: June 2010, April 2012
# web: http://www.sudwebdesign.com and http://tosri.free.fr
# Please feel free to copy and duplicate but would appreciate leaving links alone. thanks
# This script can to contain some errors (or missing test). You can change it !
# check number of arguments
if [ $# -ne 2 ]
then
echo "usage mdir2mobx.sh mails(mailbox_kmail_or_kontact) mails_conv(mailbox_thunderbird)"
echo "The input directory must be named 'mails'"
exit 1
fi
IN_DIR=$1
OUT_DIR=$2
if [ ! -d $IN_DIR ]; then
echo "WARNING: Cannot find Input Directory, exiting"
exit 2
fi
#if output directory not exist, create it
if [ ! -d $OUT_DIR ]; then
mkdir $OUT_DIR
fi
# Loop through files in current directory
# looking for /cur sub folders .
# Then take the name of the parent
find $IN_DIR -name 'cur' | grep -v ibex | while read filename
do
res=$(echo "$filename" | sed s/cur//g)
#test if directory cur is empty (no mail in this directory)
if [ `ls -A "${res}cur" | wc -c` -ne 0 ];then
testfn=`cd "$filename"; cd ..;pwd`
fn=$(basename "$testfn")
if [ -d "$testfn" ]; then
#delete "mails" at the beginning of the path
subdir=$(echo $res | sed "s/^mails\///g")
#change "directory" on sdb in the path
subdir=$(echo $subdir | sed "s/directory/sbd/g")
#delete all dots in the path
subdir=$(echo $subdir | sed "s/\.//g")
#delete the last slash of the path
subdir=$(echo $subdir | sed "s/\/$//g")
subdirT=$subdir
#get the path of the parent directory of the current directory
subdir=$(echo $subdir | nawk -F / 'BEGIN {OFS="/"}; {$(NF--)=""; print}')
#change sdb to .(dot)sbd to obtain the correct extension for thunderbird
subdir=$(echo $subdir | sed "s/sbd/.sbd/g")
#get the last directory to generate a file with the correct name
finalFic=$(echo $subdirT | nawk -F / '{print $NF}' )
if [ `echo $res | grep directory | wc -l` -ne 0 ];then
#create directory recursively
mkdir -p "$OUT_DIR/$subdir"
fi
echo -n "$OUT_DIR/$subdir$finalFic ..."
#launch python script
python maildir2mbox.py "$res" "$OUT_DIR/$subdir$finalFic"
echo "Done !"
fi
fi
done

Utilisation

"mdir2mbox.sh mails mails_conv" où :

  • mdir2mbox.sh est executable
  • Les deux scripts sont dans le même dossier
  • Les deux dossiers mails et mails_conv sont dans ce même dossier

Remerciements

Un grand merci à ceux qui m'ont permis de ne pas réinventer la roue :