How do I transfer emails from my old provider?

Learn how to use imapsync to transfer emails between IMAP servers

269ac00f765508cb74b25d006dbf5bb2

Written by Dennis Nind

Last published at: February 8th, 2019

About the tool

Official website: Official imapsync migration tool ( release 1.727 ) -- a LOT of info here, so go and read it!

NOTE: You can keep running this script over and over. It will skip messages that already exist on the new server. So, you can copy once, then change MX/DNS, then run it again later, to catch any messages that came in after the first run.

imapsync using a browser

There is a simple version of IMAPSync available online...

https://imapsync.lamiral.info/X/

Its usage is simple, but more limited than the version available from the command line. For basic mailbox to mailbox migration however its excellent!

imapsync using a command line

Check to see if you have imapsync installed already:

Issue the command: imapsync -v
If you have it installed, it will report back the version number.
If you do not have it installed, of course it will just result in an error.

Install imapsync:

imapsync requires the epel repo. On my cPanel servers, I don't have the epel repo installed or enabled, so my instructions will show how to do that also. Your mileage may vary.

  1. Make a directory to store the download (optional): mkdir /root/imapsync
  2. Move into the directory (optional): cd /root/imapsync
  3. Download the epel repo: wgethttps://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
  4. Install epel repo: yum install epel-release-latest-6.noarch.rpm
  5. Install imapsync: yum install imapsync
  6. Disable epel repo: Edit file /etc/yum.repos.d/epel.repo and change all references of enabled=1 to enabled=0
  7. Edit your Firewall to allow outbound TCP connections on ports 143 (non-SSL IMAP) and/or 993 (SSL IMAP)

Upgrade imapsync: 

(for future reference)

Because we have disabled the epel repo, you won't get auto updates of imapsync. To upgrade imapsync:

  1. Enable epel repo: Edit file /etc/yum.repos.d/epel.repo and change all references of enabled=0 to enabled=1
  2. Upgrade imapsync: yum update imapsync
  3. Disable epel repo: Edit file /etc/yum.repos.d/epel.repo and change all references of enabled=1 to enabled=0


Use imapsync to transfer one IMAP mailbox: 

(basic example)

Here is a basic script to transfer one mailbox, without using any of the many available option flags. This will use non-SSL port 143.


Code:

/usr/bin/imapsync \
  --host1 oldserver.example.com --user1 foo@example.com --password1  "somepass" \
  --host2 newserver.example.com --user2 foo@example.com --password2 "somepass"


Use imapsync to transfer one IMAP mailbox to GMAIL, with extra flags/options:

GMail requires that you use the SSL port 993. This example forces port 993, uses one of GMail's IPs as the server (using a FQDN didn't work for some reason), does 100 messages at a time (GMail needs this), and does some other things that I can't remember now (do your own research -- just sharing this example which consistently works great for me to move a customer's mail from one of our cPanel servers to a GMail mailbox:


Code:

/usr/bin/imapsync --no-modules_version --showpasswords --addheader \
  --host1 cpanel.example.com --user1 foo@example.com --password1 'somepass' \
  --host2 173.194.194.108 --user2 foo@gmail.com --password2 'somepass' \
  --port2 993 --ssl1 --ssl2 --split1 100 --split2 100 --syncinternaldates \
  --noauthmd5 --authmech1 LOGIN --authmech2 LOGIN --allowsizemismatch --useheader Message-ID

Move MULTIPLE mailboxes at a time with imapsync:

Moving multiple IMAP mailboxes at a time is similar to the examples above, with the difference being that you create a text file of email addresses and passwords and then you run your move script against that list. 

First, create a file called list1.txt and put in a list of emails and passwords. the format is email address on old server, password on old server, email address on new server, password on new server. Each item is separated by a semi-colon.

Code:

oldserveremail1@example.com;oldserverpass1;newserveremail1@example.com;newserverpass1
oldserveremail2@example.com;oldserverpass2;newserveremail2@example.com;newserverpass2
oldserveremail3@example.com;oldserverpass3;newserveremail3@example.com;newserverpass3

Next, create your script that will look like the single mailbox moving one-liner, but will read in your list of emails and passwords and process them one at a time:

Code:

{ while IFS=';' read  u1 p1 u2 p2
    do
        { echo "$u1" | egrep "^#" ; } > /dev/null && continue # this skips commented lines in list1.txt
        echo "==== Syncing user $u1 to user $u2 ===="

        imapsync --host1 oldserver.example.com --addheader --user1  "$u1" --password1 "$p1" \
                 --host2 newserver.example.com --user2 "$u2" --password2 "$p2"

        echo "==== End syncing user $u1 to user $u2 ===="

        echo
    done
} < list1.txt

Again, you will adjust this to have the right servers, perhaps changing the port number, or adding any of the many flags/options that could help you with your particular situation (read the docs!)

What I do is to put a # in front of each line except the first, to comment them all out but the first one, to test things out. Then remove the #'s and run the whole thing.

Troubleshooting:

Each time you run imapsync, it creates a log in a folder called LOG_imapsync, which will be in the same folder as where you downloaded and installed imapsync.

To change it, there are option flags (yes, read the docs!) use --logfile filepath or use --nolog to turn off logging