Migrating Emails from KMail to Mac

From korrekt.org

Here I explain how to migrate emails from KDE's email client KMail into essentially any other client, even in a situation where no running KMail is available (clearly, at least the mail files should be available though). I am doing this on a Mac but the approach can be directly applied to any operating system that supports the required software (certainly Linux).

This guide was written in July 2010 to recover email data from KMail 4.3.2 as shipped with Kubuntu Jaunty. It should work with older versions of KMail, and may or may not work with future versions (who knows).

Note: If you still have a running instance of KMail, then you may first want to check if your version offers any built-in export options that may be easier to use.

Overall strategy

We set up a mail server that runs on our local computer. It reads our existing mails from disk and offers them to email clients in arbitrary formats such as IMAP or POP3. Clearly, this idea would also work if components would run on multiple machines, but I assume that all communication is on the local computer only.

What we thus need to do is:

  • Install a mail server
  • Make sure the mail server is able to read KMails data as an input
  • Download the mails from our server to any email client of our choice

If you use another (non-UNIX-like) operating system, and if it does not support the required software, then you could use a two-machine solution where a Linux machine, possibly booted from a Live CD, runs the software while your target machine only runs the email client.

Installing a mail server

I selected Dovecot as a mail server. To install it on Mac, I used MacPorts. Steps are as follows:

port install dovecot
This gave me dovecot version 1.2.11. Chances are that some changes have happened in dovecot version 2, so consult its documentation if the below fails for you with such newer versions.
  • Find your dovecot configuration files:
cd /opt/local/etc/dovecot
  • In that directory, create a basic configuration file:
cp dovecot-example.conf dovecot.conf
  • Edit this file dovecot.conf to get the basic configuration. Here are the settings I changed from the default:
# Do not require SSL (simpler setup):
ssl: no
# Allow unencrypted login:
disable_plaintext_auth: no
# The below were reported by dovecot -n as changed settings
# but I think I did not have to set them:
login_dir: /opt/local/var/run/dovecot/login
login_executable: /opt/local/libexec/dovecot/imap-login
# Basic authentication using local user accounts:
passdb pam {
  # Reuse Mac's /etc/pam.d/login which suits our needs:
  args = login
# Use sysetm user data (I think this was also preset for me) 
userdb passwd {
Note that these settings are deactivating all security. This is okay if you are running the server only locally, and have a firewall enabled that disallows (most) incoming connections (see System Preferences).

With this you should already be able to start dovecot by running


on the command line as root (preprend sudo if you are not root).

You should now be able to connect to your local mail server with any email client. Create a new IMAP account for server localhost using your system user and password. Dovecot tries to find user mailboxes but normally you should not have emails yet.

You can kill dovecot from the command line: simply run dovecot again to learn about its PID and run

kill the-pid-you-found

After this, the email client should no longer be able to connect to dovecot and report some server error. Remember to always kill and restart dovecot when making configuration changes.

Getting KMail emails into the mail server

KMail supports two formats for storing mail, mbox and maildir, and both are fairly standard and supported by dovecot. Unfortunately, KMail uses a modified non-standard variant of maildir, and it may mix mbox and maildir data in its folders.

To load KMail mails in dovecot, one thus needs to first convert the KMail mails to a more standard layout. I did this based on a script by Graham R. Cobb, which I modified to run on Snow Leopard. Do the conversion as follows:

  • (Optional) Make a copy of your KMail mails so that there is no risk of them being modified accidentally by mail clients (the script only creates symbolic links, so mail clients will access the original data if you don't make a copy):
cp -R <path-to-your-linux-home>/.kde/share/apps/kmail/mail ~/Kmaildir
The source path is the default location of mails in KMail. When not making a copy, you need to use this path below.
wget http://korrekt.org/div/kmail-imap-export.sh
By the way, I merely changed some command parameter names so they would work with the Mac versions of the respective programs.
  • Make the script executable:
chmod a+x kmail-imap-export.sh
  • Run the script:
./kmail-imap-export.sh ~/Kmaildir ~/dovecot

The directory ~/dovecot now has a structure that is accessible to dovecot. This requires mbox and maildir data from KMail to be separated. If this happened, you will have to merge it manually when copying the files.

Now it remains to tell dovecot where to find the files. Edit dovecot.conf to contain the following lines:

namespace private {
 separator = /
 prefix =
 location = maildir:~/dovecot
 inbox = yes
 hidden = no
namespace private {
 separator = /
 prefix = mbox/
 location = mbox:~/dovecot/mbox
 inbox = no
 hidden = no

This loads both maildir and mbox emails separately. I had to make one of the two "inbox" to prevent dovecot from complaining. Search the dovecot.conf file for "namespace" to find more documentation on this.

This should be enough to get dovecot to see all mails. Start dovecot to see if there are errors:

sudo dovecot

The tool also writes error messages to /var/log/system.log by default, which may help for debugging.

Migrating your data

Once you started dovecot (see above), you can access the emails via any IMAP capable mail client (POP3 would also be possible, if preferred). The relevant credentials are as follows:

  • Server: localhost
  • Authentication: unencrypted (plain text); login
  • Username: your system user name
  • Password: your system password

Email clients might warn you that this login method is insecure, but it should not be an issue if you are running a firewall-protected single user machine.

The standard Mac email client Mail.app worked best for me when importing the emails (although I do not use it as I dislike its editing/displaying behaviour). It also failed in showing the right times for the old mails, indicating that they had been received at import time instead of preserving their original time stamps.

Thunderbird does not implement IMAP very well and will at first not show you (all) emails. The reason is that in Thunderbird, one has to "subscribe" to email folders in IMAP. Confusingly, this is done via the menu File -> Subscribe ... where you can tick all relevant folders to be read.

Beware: Thunderbird (I used version 3.1) does not subscribe folders recursively, and it failed showing folders below the first level in the subscription dialogue on my machine. Hence, I did not see all emails, and copying the directories only copied their contents but not their sub-directories. I do not know of a good fix for this yet; as a workaround, I copied sub-folders into new locations in Mail.app. Changing the subfolders seemed to make them work.

Maybe it would be worth trying out POP3 to import more reliably into Thunderbird. But be careful when deleting emails from the server: it will modify the directory you used when running the conversion script above.

Other email clients should work in a similar fashion.

Comments, feedback

Comments and feedback for this article can be sent to me via email: markus at this domain.