Move your mails from one IMAP server to another using IMAPSYNC

I lived for almost 5 years with a @gmail address, and I was happy with that. But recently, personal concerns about privacy convinced me to switch to a self-run mail server (easy install thanks to iRedMail, mail.dir being stored in a ecryptfs container). It works perfectly, but I was still frequently logging back in my Gmail account to search my mail history. So I decided to move all my @gmail mails to my email server…And fortunately IMAPSYNC comes in handy for this task. Here are the few steps  I had to follow to assure a successful migration:

Install dependencies

Download the source code from git

In your /root directory for example:

git clone git://

Then cd into the imapsync directory, and try to compile.

cd imapsync
mkdir dist
make install

The compiling should fail…it’s normal, perl still misses some dependencies:

perl -MCPAN -e "install Authen::NTLM"
perl -MCPAN -e "install Data::Uniqid"
perl -MCPAN -e "install File::Copy::Recursive"
perl -MCPAN -e "install IO::Tee"

Then run the following command to be sure that all the needed dependencies are well installed, if yes, the return will be empty:

perl -mMail::IMAPClient -mDigest::MD5 -mTerm::ReadKey -mIO::Socket::SSL        -mDigest::HMAC_MD5 -mAuthen::NTLM -e -mTime::HiRes        -mData::Uniqid -mURI::Escape -mFile::Copy::Recursive -mIO::Tee ""

Then sudo make install  again, and confirm the installation went smoothly by finding out the version of imapsync  imapsync -version  .

Migrate your emails

Now it’s time to move your email from Gmail to your self-run IMAP server.

New mail account

I personally decided to create a new email on my mail server to specifically store all my gmail mails. So I had to create this new mail, which is a very simple process thanks to the iredadmin web interface bundled in iRedMail.

Tweak some Gmail settings

Gmail runs on top of an IMAP server, but it has its own way to classify mails, beginning with the folders. You can go in the settings of your Gmail account and disable some “custom gmail folder” if you don’t want them to be copied by imapsync during the migration.

Note: You can actually tell imapsync to map specific Gmail IMAP server folder to another folder on your new IMAP server, as well as tell him to not copy some folders, which let you avoid the step below. Refer to this post to see the script.


I also deleted all my gmail labels because the “tag” concept doesn’t exists in the IMAP, and so I suspected imapsync would maybe try to convert them into folders (but maybe not!). Anyway, I never really used these labels.

My IMAP server installed by iRedMail is Dovecot (which is supported by IMAPSYNC). All right, here is the final one command that worked for me (omit  –dry –justfolders   if you want to start the migration for real):

imapsync --host1 --authmech1 LOGIN --port1 993 --user1 --password1 "your_gmail_password" --host2 --user2 --password2 "password_of_new_email" --syncinternaldates --ssl1 -ssl2 --noauthmd5 --split1 100 --split2 100 --exclude "All Mail|Spam|Trash" --allowsizemismatch --useheader Message-ID --dry --justfolders

If you want to know more about each statement purpose, refer to this article.

I only had one small problem as I didn’t use the command to map folders together, it created a [GMAIL]/[SENT] folder in my new IMAP server. I used Thunderbird with a DRAGn’DROP to move them into the usual SENT folder of my IMAP server. But next time, I will add this to the command to map the Google directories with the dovecot architecture:

--folder "[Gmail]/Sent Mail" --regextrans2 's/Sent Mail/Sent/' --prefix1 '[Gmail]/' --folder "INBOX"

So I successfully moved 28 000+ emails to my new IMAP server ! 😉