archive about

Moving mails from Cyrus IMAP to GMail

Warning!!! The following article is supposed to be used by system administrators or experienced Linux/UNIX users. Do not try it UNLESS YOU UNDERSTAND EXACTLY WHAT YOU ARE DOING!

I've been looking for a way to move all emails stored in a Cyrus IMAP server (on Linux) to Gmail. Nothing that I found on the internet worked as I expected. So I came up with a quite simple solution, it's actually a one-liner! :-)

The only restriction is that you need shell access to your IMAP server (and that this is some kind of UNIX/Linux server, the following trick will not work on Win* machines). Depending on your server setup, you may need root privileges too.

If so, ssh/telnet to your IMAP server and go to the directory used by Cyrus IMAP to store your mails. For example mine was /var/spool/imap/user/panayotis/. Your mails are stored in the form of text files named "xxxxxx.". If you use IMAP folders, you will also see folders and subfolders containing similar files.

Then use the following command (replace YOURGMAILACCOUNT with your Gmail username) find . -type f -name "*?." -printf "%T@ %h/%f\n" |\ sort -n | colrm 1 11 | \ xargs -i bash -c "echo {} ; sendmail < {} YOURGMAILACCOUNT@gmail.com ; sleep 10"

Some notes: - I use %T@ will make "find" print the unix timestamp of the last modification time of the file. I then use "sort -n" to sort using this timestamp and "colrm" to remove it from each line. This way older mails are send first, and will probably appear in the right order in GMail. (however, the timestamp may have been changed later than the date received, if for example you moved the mail to an other IMAP folder later). - "sleep 10" is used to prevent GMail from considering this forward a spam attack. If you only have tens of mails, ther is no need to use it. If (like me) you want to transfer 1000s of mails, it will slow down the proccess, but you have many more chances of succeding. - Since the proccess may take a long time (depending on the amount of data), I found it useful to use screen or nohup to ensure a lost connection with my server will not stop it. - Before starting the transfer, make sure you empty your SPAM and Trash folders at GMail. It is quite possible that many of the mails transfered will be considered spam. If your spam folder is empty it is much easier to mark them "not spam".

I have the impression that the same technique could be used with other mailbox formats. If you come up with something better or with a more elegant/detailed description of the proccess please let me know so that I put a link to it.

It worked for me, I hope it works for you too. Good Luck!