(DIR) ~steelexodus
       
       Bulk mailbox movement with imapsync
       
       Published on 2026-08-26
       
       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.
       
       The Script
       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"
       `
       
       Process
       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.
       
       imapsync.lamiral.info (https://imapsync.lamiral.info)
       graphsling.co.za (https://graphsling.co.za)
       Want to chat? Send me a mail!
 (HTM) hello@steelexodus.com
       
       Other sites:
 (HTM) jarnsaet.com - Clan Járnsæt - Iron & Sweet
 (HTM) steelexodus.com - The Iron
 (HTM) marshmallowqi.com - The Sweet
 (HTM) sr.ht/~steelexodus - My profile on the hackers forge
 (HTM) graphsling.co.za - Helping small businesses earn their place on the internet
       
 (HTM) 69483572d48b1124bbd62e0078c1df16d669bd72