How to Migrate Email to a New Host With imapsync
In this article11 sections
To move mailboxes to a new email host without losing a message, copy first and switch second. Create the mailboxes on the new host, copy all existing mail across with imapsync while the old host is still receiving, lower the TTL on your MX record, switch the MX record, and then run imapsync again once or twice to collect anything that arrived at the old host during the changeover. imapsync skips messages it has already copied, so every extra run is safe and quick. Mail is never deleted from the old host until you decide to close it.
This guide is for moving a handful to a few dozen mailboxes between any two hosts that offer IMAP, which is nearly all of them. You need each mailbox’s password on both sides and access to the domain’s DNS.
The order that avoids lost mail
- Create every mailbox, alias and forwarder on the new host.
- Lower the MX record’s TTL to 300 seconds, at least a day before the switch.
- First imapsync pass: copies everything. This is the long one.
- Switch the MX record, and update SPF, DKIM and DMARC for the new host.
- Second pass a few hours later, and a third after two days.
- Move each person’s phone and computer to the new server.
- Keep the old mailboxes for at least a week, then close them.
Lost mail in a migration nearly always comes from doing step 4 before step 1, or from closing the old host before step 5.
Step 1: Prepare the new host
Create the mailboxes with the same addresses. Recreate aliases (sales@, info@) and forwarders too: they are settings, not mail, and imapsync does not copy them. Check that each new mailbox has at least as much space as the old one uses. A copy that hits the quota stops partway.
Step 2: Lower the MX TTL a day ahead
The TTL tells other mail servers how long to remember your MX record. If it is 14400 (four hours) when you switch, some senders keep delivering to the old host for four hours. Set it to 300 the day before:
dig +noall +answer MX example.com
The second column of the answer is the TTL in seconds. Do not switch until at least the old TTL has passed since you lowered it.
Step 3: Run imapsync
imapsync is not in Ubuntu’s package list. The simplest way to run it is the author’s Docker image, on any Linux machine or VPS with Docker:
docker pull gilleslamiral/imapsync
Put each password in its own file, readable only by you, so passwords never appear on the command line or in your shell history:
mkdir -p ~/mailmove && chmod 700 ~/mailmove && cd ~/mailmove
printf '%s' 'OLD-PASSWORD' > anna.old && printf '%s' 'NEW-PASSWORD' > anna.new && chmod 600 anna.*
Always start with a dry run. It logs in to both sides, lists the folders and shows what would be copied, without copying anything:
docker run --rm -v "$PWD":/pw gilleslamiral/imapsync imapsync \
--host1 mail.oldhost.example --user1 [email protected] --passfile1 /pw/anna.old --ssl1 \
--host2 mail.newhost.example --user2 [email protected] --passfile2 /pw/anna.new --ssl2 \
--automap --dry
Read the folder list in the output. --automap matches special folders that have different names on each side, so “Sent Items” on the old host lands in “Sent” on the new one instead of creating a second sent folder. When the list looks right, run the same command without --dry.
At the end, imapsync prints a summary. Three lines matter:
Messages transferred : 18342
Messages skipped : 0
Detected 0 errors
If errors are reported, run it again. Interrupted connections are the usual cause, and the second run only copies what is missing.
Several mailboxes at once
List them in a file named users.txt, one address per line, with password files named after the part before the @ as above. Then:
while read -r addr; do
u=${addr%%@*}
docker run --rm -v "$PWD":/pw gilleslamiral/imapsync imapsync \
--host1 mail.oldhost.example --user1 "$addr" --passfile1 "/pw/$u.old" --ssl1 \
--host2 mail.newhost.example --user2 "$addr" --passfile2 "/pw/$u.new" --ssl2 \
--automap > "$u.log" 2>&1 < /dev/null
echo "$addr: $(grep -E 'Detected [0-9]+ errors' "$u.log")"
done < users.txt
Each mailbox gets its own log file, and one line per mailbox is printed with its error count.
Step 4: Switch the MX record
Change the MX record to the new host’s mail server, and in the same sitting update the records that authorise it to send:
- SPF: add the new host’s
include:. Keep the old one for a week, then remove it. - DKIM: add the new host’s DKIM record. It has its own selector name, so it sits beside the old one without conflict.
- DMARC: no change needed, but if yours is at
p=reject, confirm SPF and DKIM for the new host first. Our DMARC rollout guide explains how to check.
Verify that the world sees the change, then send a test message from an outside account such as Gmail and confirm it arrives in the new mailbox:
dig +short MX example.com @1.1.1.1
dig +short MX example.com @8.8.8.8
Step 5: Second and third passes
For a while after the switch, some servers still deliver to the old host. Run exactly the same imapsync command a few hours later and again two days later. Each run takes minutes, because only new messages are copied. When a run reports Messages transferred : 0, the old host has stopped receiving.
Warning. Do not add --delete1 or --delete2 to these commands. The first removes mail from the old host as it copies; the second deletes messages on the new host that are not on the old one, which would wipe everything that has arrived there since the switch.
Step 6: Move the devices
On each phone and computer, add the mailbox as a new account with the new server’s settings, check that the folders and recent mail are there, then remove the old account. Do not edit the server name inside the existing account: mail apps often keep a local cache tied to the old server and get confused.
Anyone who used POP3 needs special care. POP3 downloads mail to one device and usually deletes it from the server, so their history is not on the old host for imapsync to copy. Their old mail must be moved from that device, by adding the new account over IMAP in the same mail app and dragging the folders across.
Common mistakes to avoid
- Switching MX before the new mailboxes exist. Mail to an address that does not exist yet bounces back to the sender.
- Running imapsync only once. The mail that arrives during the changeover is exactly what a single pass misses.
- Forgetting SPF and DKIM. Incoming mail works, but your replies start landing in spam.
- Cancelling the old host on switch day. Keep it a week. It costs little, and it is your safety copy.
- Typing passwords into the command. They stay in the shell history and are visible to other users of the machine while the command runs.
- Leaving the password files behind. Delete the
~/mailmovefolder when you finish, and ask everyone to set a new password.
Accounts that need an app password
Gmail, Google Workspace and Microsoft 365 do not accept a normal password over IMAP. For Google accounts, turn on two-step verification and create an app password for the migration, then delete it afterwards. Microsoft 365 needs modern authentication, which imapsync supports with extra setup; for more than a few mailboxes on Microsoft 365, ask the new host for help.
Having it done for you
With SiteHarbour business email, our team sets up your mailboxes, adds the MX, SPF, DKIM and DMARC records, and helps move existing messages and folders from your current provider. Email and website hosting are independent, so only the mail records change and your website can stay where it is.
Frequently asked questions
Will imapsync create duplicates if I run it twice?
No. It identifies each message by its headers and skips anything already present on the new host. Running it several times is the intended way to use it.
How long does the first pass take?
It depends on both hosts’ speed more than on your connection. As a rough guide, allow an hour for every 5 to 10 GB, and start it the evening before the switch.
Are read and unread states and flags kept?
Yes. Flags such as read, replied and starred are copied with each message, and folders keep their structure.
Does it copy contacts and calendars?
No. IMAP carries mail only. Export contacts and calendars from the old service and import them into the new one separately.
Can people keep working during the move?
Yes. They keep using the old mailbox until you move their devices in step 6. Nothing is removed from the old host at any point.
The short version: build the new mailboxes, copy everything, switch MX with a short TTL, copy again until nothing new arrives, move the devices, and only then let the old host go.