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.

Tiago - https://github.com/tlopo

Hi Very helpful article, I did figure out how to configure new version of stunnel, you need to create a configuration file like this:

# cat tunnel.cfg
client = yes
debug = 5
foreground = yes

accept = 1143
connect = secure.emailsrvr.com:993

accept = 1144
connect = imap.gmail.com:993

and start stunnel with the file name as paramenter that's all


Hi Dear,
Thanks ofr providing useful information.
I got a problem. When I use ImapCopy tool to copy my emails from one server to another all my emails are being marked as unread and so outlook starts copying all the emails again which causes usage of bandwidth.

Can you help me fix this.

Is it possible to perform ImapCopy by leaving the mail read/unread status as it is in source server.

Any help will be appreciated.

Thank you


Dear hero, I love you.

PS. the cfg file should be named ImapCopy.cfg or imapcopy.cfg otherwise it won't be recognised (July 2016)


Hi, if a password has a quotation, escaping it doesn't work. How to specify password? e.g. "aCa12&"pP" the middle " is part of the password. I've " and using apostraphe

manu - http://manurevah.com

Good question Yasin, I've not tried it and can't do so now (I'm a bit "away from the computer" these days).
(you've tried simple quotes right ?)

Maybe a dirty workaround would be to change the password temporarily.. It's not very elegant, but it works.

Let me know if you figure out a way, if not I can try it out in a few days to see.

Good luck.
: ]


Yes that's what I had to do in the end. I tried urlencode %22, escape, double/tripple quote, changing the surrounding " to ' but no luck. Maybe it's just not possible. Changing password and then changing it back had to do. Thanks for the reply


Hi and thx for the infos.
I'm in the situation to copy multiple mailboxes from hosted mailserver to my own debian mailserver.

Copy seems ok but the source mailserver has localized folder names like "gesendete objekte" instead of sent. Is there an option to rename foldernames during copy?


Hi Chris,

Sorry for the late reply, there's been so much comment spam lately. Anyway, congratulations on your move to your own server.

I'm guessing the question is now obsolete. If not, or for others, you may want to check out Imapsync (I don't have a tutorial for that, perhaps one day soon).
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).