How To Migrate Mailboxes Between IMAP Servers With IMAP TOOLS

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Tue, 2011-10-25 15:20. :: Linux | Email

How To Migrate Mailboxes Between IMAP Servers With IMAP TOOLS

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Follow me on Twitter
Last edited 10/04/2011

This guide explains how you can migrate mailboxes between IMAP servers with IMAP TOOLS. IMAP TOOLS is a collection of Perl scripts that allow you to do various tasks with IMAP servers and also POP3 servers. In this article I will focus on the scripts imapcopy.pl (copies messages and mailboxes from one IMAP server to another) and pop3toimap.pl (copies POP3 messages to an IMAP server). Both scripts support SSL. If you specify port 993 (995 for POP3) then an SSL connection is initiated. If the port number is 143 (110 for POP3) then it will try a non-SSL connection. With any other value the port will be tested to see if it supports SSL. If so, SSL will be used to make the connection; otherwise a non-SSL connection will be made.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I'm using two servers here:

  • server1.example.com (source)
  • server2.example.com (target)

I want to migrate the mailbox sales@example.com (username: sales@example.com, password: secret) on server1.example.com to the mailbox sales@example.com on server2.example.com (username and password are the same as on server1.example.com).

 

2 Before We Start

Before we start, let's take a look at both mailboxes to see what's in them (you can do this with an email client such as Outlook, Thunderbird, or a web-based email client such as SquirrelMail - I'm using SquirrelMail here).

server1.example.com:

server2.example.com:

After the migration, the three messages on server1.example.com should be available on server2.example.com.

 

3 IMAP TOOLS Installation

It doesn't matter if we install IMAP TOOLS on server1.example.com or server2.example.com - we can even install it on a third, uninvolved server.

We download and install the IMAP TOOLS scripts in the /usr/local/imap_tools/ directory as follows:

cd /usr/local/
mkdir imap_tools
cd imap_tools
wget http://www.athensfbc.com/imap_tools/files/imap_tools_V1.105.tar.gz
tar xvfz imap_tools_V1.105.tar.gz

Now we have to make the Perl scripts executable:

chmod +x /usr/local/imap_tools/*.pl

 

4 Migrating Email Messages From One IMAP Server To Another IMAP Server With imapcopy.pl

imapcopy.pl usage is as follows:

/usr/local/imap_tools/imapcopy.pl -S host1[:port]/user1/password1 -D host2[:port]/user2/password2

So the command to copy email messages from sales@example.com on server1.example.com to sales@example.com on server2.example.com is:

/usr/local/imap_tools/imapcopy.pl -S server1.example.com/sales@example.com/secret -D server2.example.com/sales@example.com/secret

Here's a sample output:

root@server1:/usr/local/imap_tools# /usr/local/imap_tools/imapcopy.pl -S server1.example.com/sales@example.com/secret -D server2.example.com/sales@example.com/secret
/usr/local/imap_tools/imapcopy.pl starting
Connected to server1.example.com on port 143
Authenticating to server1.example.com as sales@example.com
Connected to server2.example.com on port 143
Authenticating to server2.example.com as sales@example.com
Number of mailboxes to process: 5
   Drafts mailbox is empty
   Junk mailbox is empty
   Trash mailbox is empty
   Sent mailbox is empty
   Copied 3 messages to INBOX
Copied 3 total messages
root@server1:/usr/local/imap_tools#

On the target server (server2.example.com), you should now see the messages from server1.example.com:

server2.example.com:

 

5 Migrating Email Messages From A POP3 Server To An IMAP Server With pop3toimap.pl

pop3toimap.pl usage is as follows:

/usr/local/imap_tools/pop3toimap.pl -p POP3host[:port] -i IMAPhost[:port] -u users_file

users_file has the following format:

popUsername password imapUsername password

So let's create the file /usr/local/imap_tools/users_file that contains the login details:

vi /usr/local/imap_tools/users_file

sales@example.com secret sales@example.com secret

The command to copy email messages from sales@example.com on the POP3 server server1.example.com to sales@example.com on the IMAP server server2.example.com is:

/usr/local/imap_tools/pop3toimap.pl -p server1.example.com -i server2.example.com -u /usr/local/imap_tools/users_file

If you get errors like

Unexpected response to SELECT INBOX. command: 1 NO Mailbox does not exist, or must be subscribed to.
unexpected APPEND response: 1 NO Access denied for APPEND on INBOX. (ACL "i" required)

open pop3toimap.pl...

vi /usr/local/imap_tools/pop3toimap.pl

... and comment out line 123 ($mailbox = 'INBOX' unless $mailbox;) and add line 124 ($mailbox = 'INBOX';):

[...]
   foreach $msgnum ( @popMsgList ) {
      if ( $range ) {
         Log("msgnum $msgnum") if $debug;
         next if $msgnum < $lower;
         next if $msgnum > $upper;
      }
      Log("Fetching POP message $msgnum") if $debug;
      $msg = getPOPMsg( $msgnum, $p_conn );

      getFlag( \$msg, \$flag );
      getDate( \$msg, \$date );

      next if $msg eq '';

      #$mailbox = 'INBOX' unless $mailbox;
      $mailbox = 'INBOX';
      selectMbx( $mailbox, $i_conn );

      if ( insertMsg(*msg, $mailbox, $date, $flag, $i_conn ) ) {
         $copied++;
         $grandTotal++;
         Log("$copied messages migrated") if $copied/100 == int($copied/100);

         #  Delete the message from the POP server if the delete flag is set
         deletePOPMsg( $msgnum, $p_conn ) if $delete;

      }
   }
[...]

Then run the

/usr/local/imap_tools/pop3toimap.pl -p server1.example.com -i server2.example.com -u /usr/local/imap_tools/users_file

command again.

Here's a sample output:

root@server1:/usr/local/imap_tools# /usr/local/imap_tools/pop3toimap.pl -p server1.example.com -i server2.example.com -u /usr/local/imap_tools/users_file
pop3toimap 1.3 starting
There are 1 users to be migrated
Connected to server1.example.com on port 110
Connected to server2.example.com on port 143
Migrating sales@example.com on server1.example.com to sales@example.com on server2.example.com (3 messages)

       Summary of POP3 -> IMAP migration

Users migrated  1
Total messages  3
Total bytes     2912

root@server1:/usr/local/imap_tools#

On the target server (server2.example.com), you should now see the messages from server1.example.com:

server2.example.com:

 

 

6 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Tue, 2013-11-26 11:04.
IMAP TOOLS is commercial.  You should probably mention that.  It would make this post an advert and useless for 99% of people who land here.
Submitted by till (registered user) on Fri, 2014-04-11 20:18.
Imaptools were free at the time this tutorial was written (2011). The poster that commented before you posted a link from were you can still download the free version from google code.
Submitted by micschk (not registered) on Wed, 2012-11-21 14:43.

Since the author of imap tools has decided to ask for a (modest) fee, you'll have to wait for the paypal stuff to be handled.

While waiting, a free port of the scripts can be found here: http://code.google.com/p/imaputils/