Email & webmail

Migrate email between servers with imapsync

Copy mailboxes from one IMAP server to another with imapsync: install it, run a dry run, migrate single or many accounts from a list, switch MX records and run a final sync.

IntermediateEmail migration3 min read

imapsync copies mailboxes from one IMAP server to another, keeping folders, read/unread flags and dates. It works between any providers – cPanel, Plesk, Google Workspace, Microsoft 365 or a self-hosted server – and can be re-run safely to copy only new messages. This guide shows a clean email migration with imapsync.

Before you start

  • Login details for every mailbox on the old server and the new server (create the new accounts first).
  • A Linux machine to run imapsync on, such as a VPS. It needs network access to both servers on port 993.
  • Moving between cPanel servers? A full account transfer with the WHM Transfer Tool moves email automatically; imapsync is for other cases.

Install imapsync

  • Debian/Ubuntu: install the Perl dependencies listed on the imapsync website, then download the imapsync script from the official GitHub repository and make it executable.
  • AlmaLinux/Rocky: sudo dnf install -y epel-release && sudo dnf install -y imapsync.

Check it works: imapsync --version.

Run a dry run

imapsync --host1 mail.oldhost.com --user1 info@example.com --password1 'OldPass' --ssl1 \
         --host2 mail.newhost.com --user2 info@example.com --password2 'NewPass' --ssl2 \
         --dry --justfolders

This logs in to both, lists folders and shows what would be created, without copying anything.

Migrate one mailbox

Remove --dry --justfolders to copy everything:

imapsync --host1 mail.oldhost.com --user1 info@example.com --passfile1 /root/p1 --ssl1 \
         --host2 mail.newhost.com --user2 info@example.com --passfile2 /root/p2 --ssl2 \
         --automap
  • --passfile1/2 keeps passwords out of your shell history (files containing only the password, permission 600).
  • --automap matches special folders like Sent, Trash and Junk even when names differ.
  • A log is saved in the LOG_imapsync folder.

Migrate many mailboxes

  1. Create accounts.txt with one line per mailbox: user1;pass1;user2;pass2.
  2. Loop over it:
    while IFS=';' read -r u1 p1 u2 p2; do
      imapsync --host1 mail.oldhost.com --user1 "$u1" --password1 "$p1" --ssl1 \
               --host2 mail.newhost.com --user2 "$u2" --password2 "$p2" --ssl2 --automap
    done < accounts.txt
  3. Delete accounts.txt when finished – it contains passwords.

Cut over with no lost mail

  1. A day before, lower the MX record TTL to 300 seconds.
  2. Run the first full sync (may take hours for large mailboxes).
  3. Change the MX (and related SPF/DKIM) records to the new server.
  4. Wait for the old TTL to expire, then run imapsync again – it copies only messages that arrived meanwhile.
  5. Update mail apps to the new server settings.

Troubleshooting

  • Authentication failed: wrong password, or the provider needs an app password or OAuth (common for Google and Microsoft).
  • Messages too large: some servers limit message size; check the log and use --maxsize to skip huge ones if needed.
  • Duplicates after re-runs: imapsync normally detects existing messages by Message-ID; avoid changing folder mapping between runs.
  • Slow transfer: run imapsync on a server close to both hosts; migrate mailboxes in parallel in moderation.

FAQ: imapsync email migration

Does imapsync delete mail from the old server?

No, not unless you add options like --delete1. The old mailbox stays intact.

Can it migrate contacts and calendars?

No. imapsync copies IMAP mail only; export contacts and calendars separately (CardDAV/CalDAV or files).

Can I migrate from POP3?

No. Mail downloaded via POP3 lives on your computer; upload it to the new IMAP account from your mail app.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.