Migrating IMAP Inboxes with imapsync


Imapsync is an IMAP transfer tool used for transferring emails from one IMAP server to another. It's a command-line based tool (which means you'll have to use it with SSH), however it doesn't require much to set up.

You generally would only use this tool if you have no other access but to the mail accounts themselves. It requires knowing the usernames and passwords to work.


To install it on a Centos machine, you can install it with yum and the epel repository
Install epel:
yum install epel-release

Install imapsync
yum install imapsync


Imapsync is a command-line based utility. To get a list of it's options and capabilities, you can run imapsync --help

An example of a simple copy job of one mailbox:

imapsync --host1 imap.source.example.com  \
	   --user1 user@example.com 	    \
	   --password1 sourcepass  	    \
	   --ssl1			    \
	   --host2 imap.dest.example.com    \
	   --user2 user@example.com 	    \
	   --password2 destinationpass    \

--host1 - Specifies the hostname of the source server (you can also use it's IP address)
--user1 - The email username of which you want to copy
--password1 - The password to the source mailbox
--ssl1 – Instructs imapsync to connect to host1 over SSL
--host2 - Specifies the hostname of the destination server (you can also use it's IP address)
--user2 - The destination account the emails will copy to
--password2 - The password to the destination mailbox
--ssl2 – Instructs imapsync to connect to host2 over SSL

Other options that are commonly used are:

--dry - Instructs imapsync to show what it would do. It doesn't actually copy the data. This is good for testing.
--exclude - This option provides a filter to exclude folders from the copy. It's based on regular expression (regex). For example: --exclude "^Trash" to exclude the Trash folder. 


If doing more than one account, it's better to make a simple file with the accounts and another with a script to loop through the accounts.

For example, you would create a text file called accounts. Inside the file would be:

Where the values are separated by a semicolon.

Then create an imapsync script called imapcopy.sh. It would have a while loop to read the accounts and split up each value as needed:

{ while IFS=';' read h1 u1 p1 h2 u2 p2
echo "==== Starting imapsync from host1 $h1 user1 $u1 to host2 $h2 user2 $u2 ===="
imapsync --host1 "$h1" --user1 "$u1" --password1 "$p1" \
--host2 "$h2" --user2 "$u2" --password2 "$p2"
echo "==== Ended imapsync from host1 $h1 user1 $u1 to host2 $h2 user2 $u2 ===="
} < accounts

You would then run the copy by typing:
sh imapcopy.sh

Note: Imapsync is NOT designed to keep two active inboxes in sync with each other. If the mailbox on the destination has emails moved or deleted, imapsync will copy them again from the source server, creating duplicate emails.

Have more questions? Submit a request


Powered by Zendesk