Migrate any mail server to hestiacp (vestacp fork)

My friend asked me to help with the migration of mail from a server running ispmanager5 lite to a server running vestacp. But this script is suitable for migration from any imap server to hestiacp panel.
I found imapsync utility and I'll use it. The email.txt file should contain a list of mailboxes and passwords, one account per line.

#!/bin/bash
# hestiacp user
user="user"
# count mailboxes from email.txt
q=`cat email.txt | wc -l`
# create domains to create
domains=($(cat email.txt | cut -d @ -f2 | awk '{print $1}' | uniq))
for d in "${domains[@]}"
do
v-add-domain $user $d
done
# create account and sync all mailboxes
for ((i=1;i<=$q;i++));
do
# old mailserver hostname
host1="old.mailserver.com"
# new mailserver hostname
host2="new.mailserver.com"
# some useful variables
account=`head -n $i email.txt | tail -1 | awk '{print $1}' | cut -d @ -f1`
email=`head -n $i email.txt | tail -1 | awk '{print $1}'`
password=`head -n $i email.txt | tail -1 | awk '{print $2}'`
domain=`head -n $i email.txt | tail -1 | awk '{print $1}' | cut -d @ -f2`
# create mailboxes
v-add-mail-account "$user" "$domain" "$account" "$password"
# sync mail
/root/imapsync/imapsync \
--host1 $host1 --user1 $email --password1 $password \
--host2 $host2 --user2 $email --password2 $password
done