Why Email Migration with IMAP Is Harder Than You Think
When you move a website between two hosts, it's usually enough to transfer files and the database. But email migration is a different story. If your mailboxes on the previous host were working with the IMAP protocol, all messages, folders, labels, and even read/unread status are stored on the server. If you don't transfer this data correctly, your users may permanently lose their old emails.
Many people assume that by changing DNS records and recreating mailboxes on the new server, everything will be transferred automatically. This assumption is wrong. DNS only changes the route for sending and receiving new emails; it doesn't move any of the old messages. For a successful email migration, you need to transfer the data directly from the source server to the destination.
Preparation Before Starting Email Migration
Before taking any action, create a detailed checklist. Rushing at this stage is the most common cause of data loss.
1. Identify the Protocol and Source Server Settings
First, make sure your mailboxes are actually using IMAP, not POP3. With POP3, messages are usually downloaded to the local client, and the server only has a temporary copy. If you're using POP3, email migration must be done through local files, not through the protocol.
To check, look at your email client settings (such as Outlook or Thunderbird). If you see port 143 or 993 (SSL), it's IMAP. Port 110 or 995 indicates POP3.
Also, obtain this information from the source host:
- IMAP server address (e.g.,
mail.example.com) - Port and encryption type (SSL/TLS or STARTTLS)
- Username and password for each mailbox
- Complete list of mailboxes and email addresses
2. Check Disk Usage and Destination Server Limits
Before starting, measure the total data size. You can use the following command on the source server:
du -sh /home/*/mail/*
On cPanel hosts, this command usually shows the size of each mailbox. If the total size exceeds the destination host's disk quota, you'll need to either purchase more space or archive old emails. Otherwise, the email migration will be incomplete, and extra messages will remain in the queue.
3. Take a Full Backup of Mailboxes
Never start email migration without a backup. The simplest method is to use the host's own backup feature. In cPanel, from the Backup section, select Download a Full Website Backup and enable the email section. This file includes all mailboxes with their folder structure.
If you have SSH access, you can directly back up the mail directory:
tar -czf mail_backup.tar.gz /home/username/mail/
Keep this backup on your local system or a cloud storage. If something goes wrong during the migration, you can revert to the previous state.
Main Methods for Email Migration with IMAP
There are three common methods for migrating email between two IMAP servers. The choice of method depends on the number of mailboxes, data volume, and your level of access.
Method One: Using the imapsync Tool (Recommended)
imapsync is the most popular open-source tool for this task. It synchronizes folders, messages, flags, and even calendars between two IMAP servers. Installing it on Linux is simple:
sudo apt-get install imapsync # For Ubuntu/Debian
sudo yum install imapsync # For CentOS/RHEL
Then, for each mailbox, run the following command:
imapsync --host1 mail.oldserver.com --user1 info@example.com --password1 'OldPass123' \
--host2 mail.newserver.com --user2 info@example.com --password2 'NewPass456' \
--ssl1 --ssl2 --syncinternaldates
Explanation of parameters:
--host1and--host2: Source and destination server addresses--user1and--user2: Full email address--ssl1and--ssl2: Enable SSL encryption--syncinternaldates: Preserve the original date of message receipt
If you have many mailboxes, write a loop script:
#!/bin/bash
while IFS=',' read -r email pass; do
imapsync --host1 mail.oldserver.com --user1 "$email" --password1 "$pass" \
--host2 mail.newserver.com --user2 "$email" --password2 "$pass" \
--ssl1 --ssl2 --syncinternaldates
done < accounts.txt
Create the accounts.txt file with the format email,password on each line.
SSL certificate verify failed error, the source server's SSL certificate is likely invalid. For testing, you can use the parameter --ssl1 --sslargs1 SSL_verify_mode=0, but only do this temporarily and fix the certificate after the migration.Method Two: Manual Transfer with an Email Client (For Low Volume)
If you only have a few mailboxes with low volume (e.g., less than 2 GB), you can use Thunderbird. Steps:
- Add the old email account to Thunderbird (IMAP).
- Wait for all messages to download (this may take several hours).
- Add the new account as well.
- Drag and drop folders from the old account to the new account.
This method is not suitable for migrating large emails because the client may consume memory and the process may be interrupted. Also, folder ordering and some flags may be disrupted.
Method Three: Direct File Transfer (Only for Similar Hosts)
If both hosts use cPanel or DirectAdmin and the file structure is identical, you can directly copy the mail files. First, on the source server:
tar -czf mail_transfer.tar.gz /home/username/mail/
Then transfer the file to the destination server (e.g., with scp):
scp mail_transfer.tar.gz user@newserver.com:/home/username/
On the destination server, extract the file:
cd /home/username/
tar -xzf mail_transfer.tar.gz
Then fix file ownership:
chown -R username:mail /home/username/mail/
Warning: This method only works when the mail storage format (such as Maildir or mbox) and the control panel version are the same. Otherwise, messages may be corrupted or unreadable.
After Email Migration: Verification and DNS Changes
After completing the migration, before changing DNS, verify the data integrity.
Check the Number of Messages and Folders
In your email client, compare the number of messages in each folder on the old and new servers. You can use the following command on both servers:
find /home/username/mail/ -type f -name "cur/*" | wc -l
This command shows the number of message files in the cur folder (read messages). The numbers should match.
Change DNS Records
Once you're sure all data has been transferred, change the MX and A records related to email. In your DNS, point the MX record to the new server address:
example.com. MX 10 mail.newserver.com.
Also, change the A record for mail.example.com to the new server's IP. After the change, it takes between 1 and 24 hours for DNS to update across the internet.
Additional Tips for a Hassle-Free Email Migration
Manage the Send and Receive Queue
It's best to perform the migration during low-traffic hours. If new emails are added to mailboxes during the migration, they may be missed. You can stop sending emails to the old server for a few hours or use tools like imapsync with the --regextrans2 parameter, which only synchronizes new messages.
Full Testing After DNS Change
After changing DNS, test the following:
- Sending and receiving emails to internal and external addresses
- Logging into webmail with the new password
- Connecting desktop and mobile clients
- Checking Spam and Drafts folders
Keep the Old Server for a Few Days
Don't shut down the old server for at least 72 hours. If a user notices their emails are incomplete, you can re-run the migration from the old server. After full confirmation, you can delete the mailboxes on the old server.
Conclusion
Email migration with IMAP is a manageable process if done with the right tools and careful planning. The key point is to never view DNS as a migration tool; DNS only changes the route, it doesn't move data. By using imapsync, taking a full backup, and thoroughly verifying after the migration, you can be confident that no messages will be lost.
If you're looking for a host with proper infrastructure for email hosting, ServerNet offers web hosting and cloud server services that can be a suitable destination for your migration. But in any case, follow the technical steps in this article to ensure a secure and error-free migration.