Bulk IMAP sync with imapsync
Published on 2026-08-10
A simple shell script to call imapsync for client mailboxes
Introduction
At my company, graphsling, I host websites with mailboxes for my clients. I have recently noticed that the provider I am with is getting a lot of spam, especially from other hosts on their side being hacked.
Getting fed up with this, I am moving all the clients over to Migadu, which is a nice hacker-friendly mail provider.
To help with the process, I decided to use imapsync which is a tool to
automate the process of moving mail folders and messages from one IMAP capable
server to another.
Here is the shell script:
#!/bin/bash
set -xeu
clear
EXTRA=""
NEW_HOST="imap.migadu.com"
do_sync() {
local user="$1"
local company="$1"
local mailbox="${user}@${company}"
local old_host="mail.${company}"
imapsync \
--user1 ${mailbox} \
--user2 ${mailbox} \
--passfile1 ./${company}-${user}-old \
--passfile2 ./${company}-${user}-new \
--host1 ${old_host} \
--host2 ${NEW_HOST} \
--ssl1 \
--ssl2 \
${EXTRA}
read -p "Press [ENTER] to continue..."
}
do_sync "user1" "alpha.com"
do_sync "user2" "bravo.com"
This will sync the mailboxes for user1@alpha.com and user2@bravo.com from
the xneelo side to the Migadu side.
For this process to work, we need to create two password files. The first first
file should be named alpha.com-user1-old and contain their from password,
while the second file alpha.com-user1-new should contain the password from
the new mailbox. Remember to change the file names according to your needs.
The easiest way to create these files is to get the passwords and execute this at the shell:
echo 'PASSWORD' > alpha.com-user1-old
xneelo always provides the mail.example.com DNS entry so it's easy to get to
their IMAP mailboxes. For Migadu, it's always imap.migadu.com.
The global EXTRA flag can be used to pass in extra flags such as --dry to
test the process before commiting. imapsync has a nice log at the end which
tells you how the process went.
Save this to a file named sync.sh, making sure the file is executable. Then
you can invoke it simply as:
./sync.sh
This will process each mailbox in turn, pausing at the end so that you can confirm that all mails have moved correctly.
You can read more about the imapsync tool here, which is a nice little
utility:
Check out my company, where I build digital brands for clients:
Comment