📬
Hosting

Migrating Mailboxes with imapsync Without Losing a Single Email

15.11.2026
← All articles

When you switch hosts, mail is usually the most delicate part. Site files can be uploaded again, but years of staff correspondence, contracts and client threads cannot be recreated. imapsync is the standard tool for the job: it connects to two IMAP servers at once and copies messages, folders and read flags from one mailbox to the other.

How it works

imapsync behaves like a mail client and talks IMAP, so it does not matter whether the servers run Dovecot, Exchange, Gmail or something else. It checks which messages already exist on the destination and copies only the missing ones, which makes repeated runs safe: nothing gets duplicated. For each mailbox you need both server addresses and the login and password on each side.

Example: a construction company in Namangan

The company has twelve mailboxes, and accounting alone holds several gigabytes. The move has to happen without stopping work: on Monday morning everyone should open their mail and find everything in place.

Installing

imapsync is written in Perl. The simplest option is the author's official Docker image, which ships all dependencies:

docker run --rm gilleslamiral/imapsync imapsync --version

Otherwise install it from your distribution's packages or follow the project's official instructions. Run it on a server with a good connection, since every message is downloaded and uploaded again.

Test first

imapsync --host1 eski.example.uz --user1 buxgalter@misol.uz --password1 'ESKI_PAROL' \
         --host2 mail.example.uz --user2 buxgalter@misol.uz --password2 'YANGI_PAROL' \
         --ssl1 --ssl2 --justlogin

--justlogin only checks that both logins work. Next, run with --dry to see which folders would be created and how many messages would move, without writing anything. Add --automap so standard folders like Sent Messages and Sent are matched automatically.

The real run

imapsync --host1 eski.example.uz --user1 buxgalter@misol.uz --password1 'ESKI_PAROL' \
         --host2 mail.example.uz --user2 buxgalter@misol.uz --password2 'YANGI_PAROL' \
         --ssl1 --ssl2 --automap

At the end imapsync prints a summary of transferred and skipped messages and errors, and writes a detailed log for each run. Aim for zero errors.

Many mailboxes at once

Put accounts in qutilar.csv as old login, old password, new login, new password separated by semicolons, and loop over them:

while IFS=';' read -r u1 p1 u2 p2; do
  imapsync --host1 eski.example.uz --user1 "$u1" --password1 "$p1" \
           --host2 mail.example.uz --user2 "$u2" --password2 "$p2" \
           --ssl1 --ssl2 --automap || echo "XATO: $u1" >> xatolar.txt
done < qutilar.csv

Only failed accounts end up in xatolar.txt. Delete the CSV with passwords when you are done. The --passfile1 and --passfile2 options keep passwords out of the server's process list.

Switching without downtime

Do the first full sync before touching DNS, since it takes longest. Then point the domain's MX record to the new server. Some mail may still reach the old server while DNS updates, so run imapsync again a day or two later; this pass copies only new messages and finishes in minutes. For the construction company: first sync on Friday evening, MX change on Saturday, final sync on Monday morning. With sayt.uz, mail domains and mailboxes are created in the hosting panel, and MX records are managed on the domain page of your account.

Common problems

Never use --delete1 on the first run, because it removes copied messages from the old server. Leave the old mailbox untouched until you are sure everything arrived.

Related articles

🤖 Telegram bot "Conflict: terminated by other getUpdates" error: cause and fix 🔁 Migrating a WordPress Site: Replacing URLs Without Breaking Serialized Data 📊 Grafana and Prometheus: server monitoring basics 🪶 Can you use SQLite in production?
🌐 Language
🇺🇿 O'zbek 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓