LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-09-2005, 04:15 PM   #1
i_s
Member
 
Registered: Feb 2005
Posts: 61

Rep: Reputation: 15
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 xfmail2mbox.sh

Regards,

i.s.

Code:
#! /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>";
fi;

# 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;}'`;
    mboxfile=$dirname'.mbox';

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

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

    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:'
	shortfromflag='true';
	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
	    shortfromflag='false';
	    fromline=`grep 'X-From-Line:' $i`;
	    from=`echo $fromline | awk 'BEGIN{FS="Line:";}{print $2;}'`;
	    if [[ $from == "" ]]; then
		shortfromflag='true';
		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;
		    echo "WARNING: "$i": no 'From:' nor 'X-From-Line:' nor 'Reply-To:' field found." >&2;
		    continue;
		fi;
	    fi;
	fi;
	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)";
	    fi;
	    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;
	else
# output long MBOX mail header found in 'X-From-Line:' field
	    echo $from >> $mboxfile;
	fi;

# output mail itself
	cat $i >> $mboxfile;
    done;
    echo;
else
    echo $1": not a directory.";
fi;
 
Old 11-10-2005, 12:05 PM   #2
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
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.

Best.
 
Old 11-10-2005, 02:05 PM   #3
i_s
Member
 
Registered: Feb 2005
Posts: 61

Original Poster
Rep: Reputation: 15
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 http://developer.kde.org/~kmail/tools.html, the info is compatible with the comments in the script.

xfmail2mbox.sh, a shell script by Jörg Reinhardt <joeyhh(at)gmx.de> to convert xfmail folders to mbox (which is used by KMail).


Regards,

i.s.
 
Old 11-11-2005, 05:04 AM   #4
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
Yup, that should do Thanks.
 
Old 01-04-2008, 03:17 PM   #5
christian.noack
LQ Newbie
 
Registered: Jan 2008
Posts: 1

Rep: Reputation: 0
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 cutedgesystems.com). For this I had to convert from MailDir to mbox format. I worked perfectly for me without any trouble.

Thanks for your work,

Christian
 
Old 05-31-2008, 12:09 PM   #6
Dr3am3r
LQ Newbie
 
Registered: Dec 2006
Posts: 3

Rep: Reputation: 0
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:
http://diavol.noobwars.gr/scripts/maildir2mbox.rar
 
Old 11-26-2008, 09:53 AM   #7
Dr3am3r
LQ Newbie
 
Registered: Dec 2006
Posts: 3

Rep: Reputation: 0
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 phpclasses.org ( http://www.phpclasses.org/browse/package/4611.html ) or http://diavol.noobwars.gr/scripts/maildir2mbox.rar.
 
Old 12-14-2009, 03:20 PM   #8
granth
Member
 
Registered: Jul 2004
Location: USA
Distribution: Slackware64
Posts: 210

Rep: Reputation: 39
Lightbulb

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.

EG.
Code:
fromline=`grep -i 'From:' $i`;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with script to convert different Video file types to wmv format for streaming farmerjoe Programming 1 11-17-2005 01:12 PM
procmail + maildir + mailbox with spaces psychobyte Linux - Software 0 07-09-2005 01:07 PM
(Imail) mailbox to (courier-imap/postfix) maildir chakkerz Linux - Software 0 02-03-2005 09:42 PM
maildir format tools syl20 Linux - Software 5 01-19-2005 06:55 AM
Convert Maildir to Mbox leonardox Linux - Software 2 09-26-2003 11:23 PM


All times are GMT -5. The time now is 10:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration