Migrating email between IMAP accounts

Often when people switch servers, or move to new providers, transferring email can be problematic. If you are using POP3, the email is already on your personal computer, however if you are using IMAP then you are stuck trying to add a second account and drag and drop for each user, or similar.

Never fear,there are tools around that help migrate things for you. Here i will explain the basics of imapsync which appears to be one of the better ones.

For this you will need a linux computer of any type, this can be at home, or you can do it on your VPS. You will need the git client installed, as well as a few dependencies, makepasswd rcs perl-doc libmail-imapclient-perl.
On debian based distros, these can be installed with the following command

apt-get update; apt-get install makepasswd rcs perl-doc libmail-imapclient-perl git libio-tee-perl libunicode-string-perl
git clone git://github.com/imapsync/imapsync.git
cd imapsync
mkdir dist
sudo make install

The easiest way to get this going in Centos or redhat based distros is to enable the rpmforge respositories (check http://repoforge.org/use/ for the correct URL for your version)

rpm -i http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
yum install imapsync

Once you have imapsync installed, you can run it like this

imapsync --syncinternaldates --useheader 'Message-Id' --host1 host1.com --user1 username --password1 password 
   --host2 host2.com --user2 username2  --password2 password2 --nofoldersizes --skipsize

Feel free to exclude directories by adding --exclude 'Drafts|Trash|Spam|Sent' to the end. You can also enable port 993/SSL using things like --ssl1 or --ssl2 as well as --authmech1 LOGIN --authmech2 LOGIN --split1 200 --split2 200

For multiple user migration you may want to automate a script. You can do this by adding something like this into a file (feel free to hardcode the hostnames or adapt)

#!/bin/bash
 
if [ -z $6 ];then
    echo "Usage: $0 host1 username1 password1 host2 username2 password2"
    exit
fi
# command line args
 SERVER1=$1
 UNAME1=$2
 PWORD1=$3
 SERVER2=$4
 UNAME2=$5
 PWORD2=$6
 
 
   imapsync --syncinternaldates --useheader 'Message-Id' 
   --host1 ${SERVER1} --user1 ${UNAME1} --password1 ${PWORD1} 
   --host2 ${SERVER2} --user2 ${UNAME2} --password2 ${PWORD2} 
   --nofoldersizes --skipsize

Now its as simple as just running it like this

./script.sh host1 username1 password1 host2 username2 password2

Or if you want unattended, put the user/pass details into another file in the same format and run something like this

cat users.txt | while read line ; do ./script.sh $line ; done
This entry was posted in HOWTO and tagged , , , . Bookmark the permalink.

Comments are closed.