Imapsync: migrate every company mailbox without losing a single email

Blog11 min read

Moving box with a key tied to it

What imapsync does, how to install and test it, the useful options, the method to switch MX records without interrupting incoming mail and a script to migrate several mailboxes at once.

By Mouhamed BANKOLE, CEO at ITNET Technologies · Updated 23 November 2026 · Reading time: 10 min

Changing email provider is scary for one simple reason: nobody wants to lose ten years of conversations with clients, or miss emails on switchover day. Copying messages by hand is out of the question once you have more than a few mailboxes. Imapsync, often searched for as "IMAP sync", is the reference tool for this job: it copies the folders and messages of one IMAP mailbox to another, without duplicates. This guide explains how it works, how to use it step by step and, above all, in which order to switch the DNS records so that no email is lost.

In short

  • Imapsync copies one IMAP mailbox to another, one way and without duplicates: you can run it again as many times as needed.
  • It deletes nothing by default on the destination mailbox; a strict sync requires the --delete2 option.
  • The right method for a business: a first copy, the MX record switch, then a final copy to catch the last messages.
  • Always test first with --justlogin, then --dry and --justfolders, and protect passwords with --passfile1 and --passfile2.
  • Run migrations from a server (a VPS, for example) rather than a workstation, so you do not depend on a computer that goes to sleep.

Contents

  1. What is imapsync?
  2. When should you use imapsync to migrate email?
  3. What should you prepare before an imapsync migration?
  4. How do you install and run imapsync?
  5. Which imapsync options should you know?
  6. How do you migrate a company's mailboxes without interrupting incoming mail?
  7. How do you migrate several mailboxes at once?
  8. Which mistakes should you avoid during an imapsync migration?
  9. Imapsync or a built-in migration tool: which should you choose?

What is imapsync?

Imapsync is a command-line tool that syncs two IMAP accounts, one way and without creating duplicates (imapsync documentation). It is released under a free licence called NLPL (imapsync documentation).

Its main characteristics:

  • it copies the whole folder tree, recursively, from the source mailbox to the destination (imapsync documentation);
  • it is incremental: a message already copied is not copied again, because the tool recognises it by its "Message-Id" and "Received" headers (imapsync documentation);
  • it keeps the messages' received dates by default, thanks to the --syncinternaldates option (imapsync documentation);
  • it deletes nothing on the destination unless you ask it to (imapsync documentation);
  • it connects over encrypted IMAP (SSL/TLS, port 993) whenever the server allows it (imapsync documentation).

To understand the protocol it relies on, our guide to IMAP, POP and SMTP explains the role of each.

When should you use imapsync to migrate email?

Use imapsync whenever you need to move emails between two servers that speak IMAP, especially for several mailboxes or large volumes. That covers most provider changes.

Typical situations:

  • moving from email included with web hosting to a dedicated professional email service;
  • changing email provider without changing address or domain name;
  • merging several mailboxes into one, for example when a colleague leaves;
  • leaving Microsoft 365 or Gmail, for which imapsync offers ready-made options (--office1, --gmail1) (imapsync documentation);
  • a one-off backup of a mailbox to another server before a risky operation.

On the other hand, imapsync only migrates emails and folders: contacts, calendars and filtering rules are moved by other means.

What should you prepare before an imapsync migration?

Before running imapsync, you need the list of mailboxes with their credentials, the destination mailboxes already created and access to the domain's DNS zone. A successful migration is mostly won in the preparation.

The checklist:

  • an inventory of mailboxes: address, size, number of messages and linked aliases;
  • the source IMAP details: server, port and password (or an app password if two-factor authentication is on);
  • the destination mailboxes created with the new provider, with enough space;
  • access to the DNS zone to change the MX, SPF, DKIM and DMARC records;
  • a lower TTL on the MX records a few days before the switch, so the change spreads quickly;
  • a machine to run the copies, which stays on for the whole transfer;
  • a switch date shared with the teams, ideally at the end of the week.

At Wayhost, the domain name page lists an interface to manage A, MX, CNAME and TXT records (checked on 23 November 2026), and the FAQ on the professional email page covers keeping your old emails (checked on 23 November 2026).

Pro tip: ask each user to empty their trash and spam folders before the migration. That is less volume to transfer, and fewer useless messages cluttering the new mailbox.

How do you install and run imapsync?

Imapsync installs from the packages of many Linux distributions or from its official repository, then runs with the addresses of both servers and the credentials of both accounts. The project's repository is published on GitHub.

The steps, in order:

  1. install imapsync on the machine that will run the copies, ideally a Linux server;
  2. create two password files, readable only by you, each holding the password on its first line;
  3. test the connection with the --justlogin option, which logs in to both servers and then stops (imapsync documentation);
  4. simulate the copy with --dry, which prints what would be done without changing anything (imapsync documentation);
  5. run the real copy by removing --dry, then run it again as often as needed.

A typical command looks like this:

imapsync --host1 imap.old-provider.com --user1 contact@your-company.com --passfile1 ./pw1 \
         --host2 imap.new-provider.com --user2 contact@your-company.com --passfile2 ./pw2 \
         --automap

Running imapsync from a VPS means you do not depend on a laptop that goes to sleep or changes network during a transfer lasting several hours.

Which imapsync options should you know?

About ten options cover most migrations: the ones that test, the ones that protect passwords, the ones that handle folders and the ones that filter messages. All are described in the imapsync documentation.

OptionRoleWhen to use it
--justloginLogs in to both servers and stopsFirst credentials test
--dryPrints what would be done, without doing itBefore any first copy
--justfoldersHandles folders only, not messagesChecking the folder tree created
--passfile1 / --passfile2Reads the password from a fileAlways, instead of --password1
--automapMaps well-known folders (Sent, Drafts, Junk…)Between providers with different folder names
--excludeSkips folders matching an expressionExcluding trash or archives
--maxage / --maxsizeSkips messages that are too old or too largePartial migration
--delete2Deletes on the destination what no longer exists on the sourceStrict sync, with care
--office1 / --gmail1Presets for Microsoft 365 and GmailLeaving those platforms

Two points to watch, from the same documentation:

  • --password1 exposes the password in the process list and the terminal history: the documentation describes it as an insecure practice;
  • without --delete2, nothing is deleted on the destination, which is reassuring but can leave messages that were deleted between two copies.

How do you migrate a company's mailboxes without interrupting incoming mail?

To lose no email, make a first full copy, switch the MX records to the new provider, then run imapsync again to catch the messages that reached the old mailbox while the change spread. The tool's incremental behaviour is what makes this method possible.

The recommended sequence:

  1. D-7: lower the TTL of the MX records (to a few minutes, for example);
  2. D-2 to D-1: run the first full copy of every mailbox, the longest one;
  3. D-day: change the MX records to point to the new server, and update SPF and DKIM;
  4. D-day, a few hours later: run imapsync again on every mailbox; only new messages are copied;
  5. D+1 to D+7: run a daily copy until every sending server sees the new MX records;
  6. after D+7: shut down the old email service once the last copies bring no new messages.

During this period, users already work in the new mailbox: each copy only adds the missing messages, with no duplicates.

Pro tip: publish a DMARC record in monitoring mode at switchover time. Its reports quickly show whether a forgotten sending tool still uses the old server. Our DMARC guide explains how.

How do you migrate several mailboxes at once?

To migrate several mailboxes, prepare a file listing source and destination accounts, then run imapsync in a loop over each line with a script. The principle stays the same as for a single mailbox.

A setup that works:

  • a text file with, on each line, the source user, their password file, the destination user and theirs;
  • a script that reads the file and runs one imapsync command per line;
  • one log per mailbox, to spot errors without rereading all the output;
  • runs in small batches of mailboxes, so as not to overload the source server;
  • a final check of the number of messages per folder on source and destination.

An example of a simple shell loop:

while IFS=';' read -r u1 p1 u2 p2; do
  imapsync --host1 imap.old-provider.com --user1 "$u1" --passfile1 "$p1" \
           --host2 imap.new-provider.com --user2 "$u2" --passfile2 "$p2" \
           --automap > "log-$u1.txt" 2>&1
done < accounts.txt

Delete the password files once the migration is over: they contain secrets in plain text.

Which mistakes should you avoid during an imapsync migration?

The most common mistakes are switching the MX records before the first copy, using --delete2 too early and forgetting app passwords. Each one is easy to prevent.

MistakeConsequenceHow to avoid it
Switching MX records before the first copyUsers land in an empty mailboxCopy first, switch afterwards
Using --delete2 on the first copyUnintended deletions on the destinationKeep it for a final strict sync
Plain-text password with --password1Secret visible in history and processesUse --passfile1 and --passfile2
Forgetting two-factor authentication on the sourceLogin failureCreate an app password
Different folder names between providersSent messages filed in a separate "Sent" folderTurn on --automap or force it with --f1f2
Shutting down the old service too earlyMessages lost while DNS changes spreadKeep the old mailbox for a week

The documentation states that --f1f2 forces one folder to be synced to another and overrides --automap (imapsync documentation).

Imapsync or a built-in migration tool: which should you choose?

Choose the new provider's built-in migration tool if it exists and covers your mailboxes, and imapsync when you want to stay in control, migrate in several passes or handle many accounts. Both rely on the same IMAP protocol.

  • Built-in tools are simpler: Google Workspace, for example, offers a service to migrate email from an IMAP account from its admin console (Google Workspace help);
  • imapsync gives more control: incremental reruns, filters by folder, age or size, and scripts for dozens of mailboxes;
  • for a single personal mailbox, the IMAP import in your email client or webmail may be enough.

Whatever the tool, the MX switch schedule stays the same: copy, switch, catch up.

FAQ

Is imapsync free?

Imapsync is free software released under the NLPL licence, and its code is available on GitHub. Its documentation states that it is "not always gratis" depending on how you get it. Installing it from the repository or from a Linux distribution's packages costs nothing.

Does imapsync delete emails from the old mailbox?

No, imapsync does not change the source mailbox by default. It copies messages to the destination, one way. Deletion options exist but must be added on purpose.

Can you run imapsync several times?

Yes, it is even the recommended method. Imapsync recognises messages already copied by their headers and only transfers new ones. A run after the MX switch catches the last emails.

How long does an imapsync migration take?

It depends on the size of the mailboxes and the speed of both servers. A small mailbox copies in minutes, a mailbox of several gigabytes can take hours. Run the first copy in advance so the final copy is quick.

Does imapsync migrate contacts and calendars?

No, imapsync only handles emails and folders, over IMAP. Contacts and calendars are exported separately, for example in vCard and iCalendar formats. Remember to recreate filtering rules and signatures too.

Do you need to change the MX records to migrate email?

Yes, if you keep the same address with a new provider. MX records tell the world where to deliver your domain's mail. Lower their TTL a few days before so the switch spreads quickly.

How do you avoid typing the password in the imapsync command?

Use the --passfile1 and --passfile2 options, which read each password from a file. The documentation advises against --password1, which shows up in the history and the process list. Delete these files after the migration.

Key takeaways

  • Imapsync copies one IMAP mailbox to another, one way, without duplicates and without deleting anything by default.
  • Test first with --justlogin, --dry and --justfolders, and protect passwords with --passfile1 and --passfile2.
  • Follow the copy, switch, catch-up order: a first full copy, the MX change, then reruns until the change has spread.
  • Automate multi-mailbox migrations with an accounts file and a script, from a server that stays on.
  • Keep the old email service for a week before shutting it down, until every server sees the new MX records.

To host your migrated mailboxes on your own domain, discover Wayhost professional email.

Article written by Mouhamed BANKOLE, CEO at ITNET Technologies.

Related articles

Have a specific question?

Our step-by-step articles answer the most common questions.

Knowledge base