Transfer email accounts between hostings

Transfer email accounts between hostings

There are times when we need to transfer a client’s email accounts from one hosting to another destination. If we migrate to a good hosting that includes the service for free, as is the case of LucusHost, it is a piece of cake, they take care of everything and I assure you that they do a great job, as I have tested it on several occasions.

But sometimes migrations are between other servers, we have special cases, etc. and we have to deal with the migration ourselves.

Years ago I have made these transfers with an email client, for example Thunderbird, in which I configured the accounts of both servers and dragged the emails between one and the other account.

I have also used some graphical software that allowed me to perform these transfers in a similar way to the Thunderbird method. But it is clear that this is a very traditional method with disadvantages (downloading all the mail to our computer before uploading it to the new server, slowness problems in graphical environments, etc.).

Transfer via web services

A viable method is the use of a web service where we configure both accounts and the web takes care of passing from one server to the target server.

For this option we have some recognized services such as OVH’s: https ://omm.ovh.net/Migration/Create or MDD Hosting’s: https ://imapsync.net/.

But if you are going to use a web service I would recommend Gilles LAMIRAL’s https ://imapsync.lamiral.info/X/ which I will tell you why shortly and that, in addition, MDD Hosting’s system is also based on LAMIRAL’s system.

imapsync online

Anyway think that you are sending to a third party server the emails of your customers with all that this implies.

Transfer emails between hostings from your computer

It is clear that the best option is not to depend on third parties. Ideally, the transfer should be made from the source server or from the destination server, but in most cases this will not be possible, since we will not be able to install anything on either of the two services.

Because of this, we will use a third party, our computer, which will be in charge of getting each mail from the source and sending it to the destination.

For that we will use the system behind two of the online services we have shown before: imapsync by Gilles LAMIRAL created under NO LIMIT PUBLIC LICENSE.

It is a very complete software made in Perl and with a lot of options, do not forget to consult the documentation.

In my case I am going to install it in WSL2 in Windows 11, that is to say, in an Ubuntu 22.04.4 LTS, so the process is the same in this OS and very similar in other Linux or OSX, just check the installation options in its documentation.

In WSL2 I run the following commands in console to install the dependencies:

sudo apt-get install  \
libauthen-ntlm-perl \
libclass-load-perl \
libcrypt-openssl-rsa-perl \
libcrypt-ssleay-perl \
libdata-uniqid-perl \
libdigest-hmac-perl \
libdist-checkconflicts-perl \
libencode-imaputf7-perl \
libfile-copy-recursive-perl \
libfile-tail-perl \
libio-compress-perl \
libio-socket-inet6-perl \
libio-socket-ssl-perl \
libio-tee-perl \
libjson-webtoken-perl \
libmail-imapclient-perl \
libmodule-scandeps-perl \
libnet-dbus-perl \
libnet-ssleay-perl \
libpar-packer-perl \
libproc-processtable-perl \
libreadonly-perl \
libregexp-common-perl \
libsys-meminfo-perl \
libterm-readkey-perl \
libtest-fatal-perl \
libtest-mock-guard-perl \
libtest-mockobject-perl \
libtest-pod-perl \
libtest-requires-perl \
libtest-simple-perl \
libunicode-string-perl \
liburi-perl \
libtest-nowarnings-perl \
libtest-deep-perl \
libtest-warn-perl \
make \
time \
cpanminus

At the end of the installation of the modules, I update the Perl module Mail::IMAPClient:

sudo cpanm Mail::IMAPClient

I then run each of the following commands and configure them if necessary (the default options are sufficient):

cpan Encode::IMAPUTF7

cpan File::Copy::Recursive

cpan IO::Tee

cpan Mail::IMAPClient

cpan Term::ReadKey

cpan Unicode::String

cpan Readonly

cpan Sys::MemInfo

cpan Regexp::Common

cpan File::Tail

cpan Test::MockObject

And now I download imapsync, give it run permissions, copy it to /usr/bin so it can be run from any path and delete the downloaded file.

sudo wget -N https://imapsync.lamiral.info/imapsync

sudo chmod +x imapsync

sudo cp imapsync /usr/bin/

sudo rm imapsync

And finally I perform the following two tests to verify that it will work correctly:

imapsync --tests

imapsync --testslive

Before transferring e-mails

Prior to the transfer of emails between both servers/hostings, we have to make sure that the accounts that we are going to copy from the source server exist in the destination server.

We go to the destination server and create each one of the accounts, the best thing is to create them with the same password that they had in origin, the client will take care later of changing the passwords of its newly migrated accounts.

If we want to know the origin server to connect to, we can look it up in the hosting configuration or by consulting the MX record from the console with the command dig, host or nslookup, the easiest option is to use dig with the modifier +short to show only what we are interested in:

dig MX tabernawp.com

dig MX +short tabernawp.com

host -t mx tabernawp.com

nslookup -type=mx tabernawp.com

Or consult it directly from an online service such as https://mxtoolbox.com/MXLookup.aspx.

Mail transfer

And once the initial work is finished, which we only have to do once, we can transfer the emails by passing 6 parameters, three for the source server and three for the destination server.

The three parameters of each server are host, user and password. 1 :

imapsync --host1 mx.correo.origen --user1 [email protected] --password1  'clave.usuario.origen' --host2 mx.correo.destino --user2 [email protected] --password2 'clave.usuario.origen'

If we are copying mailbox data between two servers, the only thing that will change is the host, although we can migrate between different users or with different keys.

Transferring multiple users in batches

But of course, if we want to transfer several users simultaneously and not have to execute the same command one by one, it is clear that we can automate something, right?

Lamiral already gives it to us. We download the file sync_loop_unix.sh or our modified version 2 Create a text file with our accounts following the format indicated in your example (or the modified version):

# Each line contains at least 6 columns
# The separator is the character semi-colon ";"
# The columns are the values for the parameters
# --host1 --user1 --password1 --host2 --user2 --password2
#
# A extra column can be used to pass some extra parameters but the script reading
# this file have to be able to get them.
#
# Don't forget the last semicolon.
# Lines starting with a # are usually comments and ignored
# Blank lines are ignored as well

# Now the data example
mx.correo.origen;[email protected];clave.usuario.origen1;mx.correo.destino;[email protected];clave.usuario.origen1;;
mx.correo.origen;[email protected];clave.usuario.origen2;mx.correo.destino;[email protected];clave.usuario.origen2;;
mx.correo.origen;[email protected];clave.usuario.origen3;mx.correo.destino;[email protected];clave.usuario.origen3;;

And now we can synchronize our email accounts between two servers, copy them for transferring, etc.

imapsync execution

Final considerations

If we run imapsync --help we will see that this command has a number of options such as copy only from a date, ignore folders, synchronize Gmail labels, etc., but a couple of useful parameters are:

  • --dry which will not execute the command, it will only show us what it would do.
  • --justlogin just connects to both servers and then exits, to check if the credentials are correct.
  • --justfoldersizes shows us the sizes of the folders and exits.

Once two mailboxes have been synchronized, if we run it again, it only copies the new mails from the source server to the destination. The correct copying strategy would be to perform the initial synchronization, which can take minutes or in very large accounts up to several hours and at the time of the final change, resynchronize again, which will take only a few minutes, make DNS changes and even resynchronize again if we connect to the source server using its IP or a name that does not influence the DNS change (for example mx.hosting-testing.com).

And as always, if you are not used to the console, the email protocols, MX, IMAP, etc., leave this job to your hosting or to a professional who will do the job for you.

Leave a Comment