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
DenyFlags "\Recent"
# SourceUser SourcePassword DestinationUser DestinationPassword
Copy "user@foo.tld" "SuperPassowrd" "user@bar.tld" "OtherGreatPassword"
Copy "user2@foo.tld" "SuperPassowrd" "user2@bar.tld" "OtherGreatPassword"
You are strongly advised to use DenyFlags “\Recent”. If the mailbox you are trying to copy has some messages marked by the server “\Recent” this flag can not be carried over (as per RFC). With the denyflag, IMAPcopy will ignore the resent flag. Otherwise, this will result in an error. And the /resent messages will not be copied.
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.
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 127.0.0.1:1143 (and 127.0.0.1:1144) to the IMAP servers on their ports 993. You can replace 1143 and 1144 with whatever you want. 127.0.0.1 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 127.0.0.1
SourcePort 1143 DestServer 127.0.0.1 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:
imapcopy
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.