Migrate emails with Imapcopy

I remember a while back migrating someone's email from one ISP to another using Thunderbird ! Well, duh, I just found a tool that seems to be a bit underrated, Imapcopy. There are many tools out there, but this one seemed simple yet efficient. However, for some odd reason it wasn't clear to me at first how simple and easy this was to use.

Install Imapcopy

It's in the Debian repositories, so just do (for other distributions just RTFM): apt-get install imapcopy

Configure and test

You can work from any directory on any system as any user (you don't need to be on the source or destination server). Simply create a file called imapcopy.cfg. There's an example here /usr/share/doc/imapcopy/examples/ImapCopy.cfg, however I'll show you a simple config to start off.

SourceServer imap.foo.tld SourcePort 143 DestServer imap.bar.tld DestPort 143 # SourceUser SourcePassword DestinationUser DestinationPassword Copy "user@foo.tld" "SuperPassowrd" "user@bar.tld" "OtherGreatPassword" Copy "user2@foo.tld" "SuperPassowrd" "user2@bar.tld" "OtherGreatPassword"

The first thing you might want to do is use port 993 TLS/SSL. This does not work with Imapcopy, but no need to panic, here's a very simple workaround using stunnel.

NOTE: Stunnel doesn't work the same way since Debian Jessie (not sure since which version of stunnel). I've not dug in to it and hence do not know what the new syntax should be (if you know, let me know).

apt-get install stunnel

We'll setup stunnel for both servers, source and destination, in this example.

stunnel -c -f -d 1143 -r imap.foo.tld:993 -P '' stunnel -c -f -d 1144 -r imap.bar.tld:993 -P ''

What happens here is that stunnel will create a secure tunnel from (and to the IMAP servers on their ports 993. You can replace 1143 and 1144 with whatever you want. now becomes the source and destination servers we will connect to.

So back to our example configuration, we need to change the servers and ports to: SourceServer SourcePort 1143 DestServer DestPort 1144

Configuration test

You can simply run imapcopy -t or with the -i option. -i will just connect to the servers, without logging in, -t will test the logins and show some infos (number of messages, folders, etc).

imapcopy -t

There's also an option to copy only 1 message per folder, -1, options to subscribe to folders and so on. Ideally you should create a test account on both servers, generate a bunch of emails, folders, etc on the source and see how the copy goes. Check imapcopy -h for other options.

Copy emails

Funny enough, I was wondering where the option was to start copying stuff, without using any of the options, it turns out it's not an option... .. . So to start copying just run the program, optionless:


That's just about it.. except for one thing. If you execute the copy twice, you will re-import the messages to the destination sever.. and so on.

There are probably more elegant solutions for keeping IMAP accounts synced, that wasn't my goal in this task, but I'd be glad to hear about it.


I am grateful

Great! Thanks a lot! It worked perfectly. I wanted to migrate all my GMail company accounts (> 5 GB of mails spread on 200 mail accounts) to my own Debian Squeeze server.


Great post thank you !

Walter Schmidt


idem2lyon - http://www.geekandmore.fr

That's just about it.. except for one thing. If you execute the copy twice, you will re-import the messages to the destination sever.. and so on.

Does it means that mails will be duplicated ?


where does the config file go?


aha i understand the config file needs to be in the directory from which you execute it. i have done so and the tests looked good so i executed the transfer. now when i go to my roundcube to see if the mails are there i get an error. can't establish a connection with database please contact system administrator. any idea what happened?

manu - http://manurevah.com

Cool that you figured it out, I was searching where I had put the conf file (I wrote this howto so long ago).

I'm not sure about your DB error that RC is throwing at you. Because ImapCopy acts as an IMAP client between 2 IMAP accounts I don't understand how or why your IMAP server wouldn't let you back in.

Have you checked RoundCube logs, or your email server logs (if you have access to those) ?


hi thanks for following up!
i'm not sure what happened with my webmail. i just went through and reset the mysql password to what i had in a config somewhere, flushed the privileges and it started working again. maybe you're right and it was unrelated to the imapcopy just a coincidence?

i ended up using the other version of imapcopy that doesn't install with wget but that you unzip and run from a shell script and some java files. maybe it's the same code but the command consisted of options in the command line instead of a config file. importing all the mail twice made me nervous with the version you have made the tutorial for. for you or anyone else interested i followed from here: http://www.howtoforge.com/how-to-migrate-mailboxes-between-imap-servers-with-imapcopy

thanks for your help!

manu - http://manurevah.com

You're welcome Billy.

By the way, I've switched from ImapCopy to OfflineImap which is more like Rsync for IMAP. If I find the courage (one of these days) I'll write a quick howto (though the documentation is actually pretty good).


Is it possible to have a "move" command? Maybe copy, verify it was copied successfully and if so delete the source message?

manu - http://manurevah.com

PB, I don't think so. That said, I've not used Imapcopy since I published this howto.


Trying with ssl/stunnel, little programm not working ... errno: (*__errno_location ()) ... Cannot open configuration file ... running on jessie ... any hints?

manu - http://manurevah.com

JG, it seems like stunnel no longer works as it used to. I just tested on Wheezy, the above instructions function, on Jessie it no longer works.

I'll update the article.

I think OfflineImap should be able to handle encryption without requiring stunnel, one of these days I'll write a howto. In the meantime, check it out, perhaps you'll be able to figure it out, if not, let me know and I'll try to at least send over some notes.
Leave a comment
You may use the following HTML tags: <p> <a> <strong> <b> <em> <i> <cite> <blockquote> <code> <pre>

Your comments WILL NOT be submitted to any third party (not even for anti spam verification).