Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to import messages into Mail from KMail, etc. UNIX
After I couldn't find solution for mail import from KMail, I wrote a simple shell script that converts the maildir hierarchy into importable mbox files.
  1. Copy this script and save it as convert.sh into the directory containing all your maildirs (Mail).
  2. Run the script (make sure it's executable with chmod +x convert.sh, of course). It will create *.mbox files having the full path converted into their names.
  3. In Mail.app, choose Import -> Other and specify the directory (Mail).
Hope this helps!
    •    
  • Currently 1.50 / 5
  You rated: 2 / 5 (4 votes cast)
 
[16,341 views]  

A script to import messages into Mail from KMail, etc. | 9 comments | Create New Account
Click here to return to the 'A script to import messages into Mail from KMail, etc.' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Incompatible with Mail in Tiger
Authored by: fungus on Oct 05, '05 12:12:14PM

Mail in Tiger uses a different structure for Mailboxes than previous versions, and will not work with this script.



[ Reply to This | # ]
Incompatible with Mail in Tiger
Authored by: wnorris on Dec 27, '05 11:03:25PM
correct, but a tiger compatible version of the script is even simpler -- you simply need to pipe the maildir messages through /usr/bin/formail. so the script becomes
#!/bin/bash

mdirname='cur'

find . -name $mdirname | while read from; do
    nm=`dirname $from | sed 's/[^a-zA-Z0-9]/-/g; s/^-*//; s/--*/-/' `
    echo "Converting $from into $nm"

    /bin/ls -1 "$from" | while read message; do 
        cat $from/$message | /usr/bin/formail
    done > $nm.mbox
done


[ Reply to This | # ]
Filename restrictions
Authored by: cran on Mar 02, '06 05:15:58AM

Anyway (if you're using tiger or not) make sure your mailbox/maildir names do not contain spaces, otherwise the script will fail.

-Sebastian



[ Reply to This | # ]
Filename restrictions
Authored by: palpitate on Sep 07, '06 07:51:08AM
The script is not carefull enough with quoting variables. Try:
#!/bin/bash

mdirname='cur'

find . -name $mdirname | while read from; do
 nm=`dirname "$from" | sed 's/[^a-zA-Z0-9]/-/g; s/^-*//; s/--*/-/' `
 echo "Converting $from into $nm"

 /bin/ls -1 "$from" | while read message; do 
  echo -n `cat "$from/$message" | grep '^From:' | head -n1 | sed 's/.*]*)>.*/From 1/'`
  date '+%a %b %e %H:%M:%S %Y'
  cat "$from/$message" | sed 's/^From /From:/'
 done > $nm.mbox
done


[ Reply to This | # ]
A script to import messages into Mail from KMail, etc.
Authored by: antheo on Jun 20, '06 08:51:21PM

The company hosting my website and my emails screw up my IMAP email account.
They converted all the emails from the standard IMAP format "mbox" a la UNIX to the new Courier- IMAP "maildir.

I tried to convert my emails with this script and import them into Mail (Mac Os 10.4.6) but it failed.

Any hints how to do the conversion for Mac Os 10.4.6?

Then i found that my folder ~/Library/Mail/ contains a copy of my email in a elmx format. I tried to import them and it did not work either.

Any idea?





[ Reply to This | # ]
A script to import messages into Mail from KMail, etc.
Authored by: antheo on Jun 21, '06 07:23:57PM

The script works great it converted my email in maildir format in mbox format but when i try to import them in Mail.app. Mail recognizes the files as mbox but refuses to import them. It does not do anything neither it creates the folders nor it displays an error.

How could i change the script or set up Mail to import the maildir emails?



[ Reply to This | # ]
A script to import messages into Mail from KMail, etc.
Authored by: yksrais on Nov 04, '06 05:22:00PM
The same for me, so I had a look on the code and fix two things: 1. From: -> From Error 2. the original script ignore the date, the E-Mail was sent, just create for all mails the same date Here my working version:
#!/bin/bash

mdirname='cur'

find . -name $mdirname | while read from; do
nm=`dirname $from | sed 's/[^a-zA-Z0-9]/-/g; s/^-*//; s/--*/-/' `
echo "Converting $from into $nm"
/bin/ls -1 "$from" | while read message; do
echo -n `cat $from/$message | grep '^From:' | head -n1 | sed 's/.*]*)>.*/From 1/' | sed 's/^From:/From /'; cat $from/$message | grep -m 1 '^Date:' | sed 's/,//g' | awk '{print $2" "$4" "$3" "$6" "$5 }'`
echo
cat $from/$message | sed 's/^From /From:/'
done > $nm.mbox
done
(C) 2006 linux2macSwitcher

[ Reply to This | # ]
A script to import messages into Mail from KMail, etc.
Authored by: yksrais on Nov 04, '06 05:27:33PM
Corrected working version:

1. uses the correct time date of E-Mail
2. bug fix (Email: -> Email

#!/bin/bash

mdirname='cur'

find . -name $mdirname | while read from; do
nm=`dirname $from | sed 's/[^a-zA-Z0-9]/-/g; s/^-*//; s/--*/-/' `
echo "Converting $from into $nm"

/bin/ls -1 "$from" | while read message; do
echo -n `cat $from/$message | grep '^From:' | head -n1 | sed 's/.*]*)>.*/From 1/' | sed 's/^From:/From /'; cat $from/$message | grep -m 1 '^Date:' | sed 's/,//g' | awk '{print $2" "$4" "$3" "$6" "$5 }'`
echo
cat $from/$message | sed 's/^From /From:/'
done > $nm.mbox
done
(C) 2006 linux2macSwitcher

[ Reply to This | # ]
A script to import messages into Mail from KMail, etc.
Authored by: mipmip on Sep 24, '07 11:33:56AM
&tI improved the script so it can now accept spaces and other symbols in the mail directory names.

#!/bin/bash

mdirname='cur'

find . -name $mdirname | while read from; do
echo $nm
nm=`dirname "$from" | sed 's/[^a-zA-Z0-9]/-/g; s/^-*//; s/--*/-/' `
echo "Converting $from into $nm"

/bin/ls -1 "$from" | while read message; do
echo -n `cat "$from/$message" | grep '^From:' | head -n1 | sed 's/.*]*)>.*/From 1/' | sed 's/^From:/From /'; cat "$from/$message" | grep -m 1 '^Date:' | sed 's/,//g'
 | awk '{print $2" "$4" "$3; "$6" "$5 }'`
echo
cat "$from/$message" | sed 's/^From /From:/'
done > $nm.mbox
done


[ Reply to This | # ]