The solution

Un semplice comandino (mailutil) risolve tutti questi problemi”>A simple command (MailUtil) solves all these problems

Migration pop3 -> Imap

The first part was the most complex because the folder concempt do not exist in pop3 so I could migrate only the INBOX folder. I had to export the folders one by one from the previous webmail provider. Fortunately the export was available to mbox format.

At this point, after having created the email addresses on the server, with the simple command

mailutil copy path/to/foo.mbox {mail.host.com/novalidate-cert/user=foo@bar.com}INBOX.foo

I have been able to copy a hypothetical folder ‘foo’ from the exported file foo.mbox to the folder ‘foo’ dell’indirizzo foo@bar.com

But I had to do this for each folder and it was not possible because they were too many so I created a folder called ‘mail’ and for each address I needed to migrate I created a subfolder where I moved all the exported files in mbox format representing the folders of that address.

After that I created the following python script that takes 3 parameters:

  1. account
  2. password
  3. host

and searches the ‘mail’ folder for a sub-folder ‘account’.
It takes all the files in that folder (INBOX, Trash, Sent, etc.) and creates the corresponding folders on the IMAP server.
Here’s the script:

import os
import sys
import pexpect
fileList = []">fileList = []
account = sys.argv [1]
password = sys.argv [2]
host = sys.argv [3]
rootdir = os.path.join ("mail" account)
for root, subfolders, files in os.walk (rootdir):
    root = root.split (os.path.sep) [2:]
    root and root = os.path.join (* root) or ""
    dest_root = root and root.replace ("", "\ \") + '.' or ""
    for f in files:
        prefix = "INBOX."
        if f == "INBOX":
            prefix = ""
            dest = '{mail.' + host + '/ novalidate-cert/user =' + account + '@' + host + '}' + prefix + dest_root f.replace ("", "\ \")
        src = os.path.join (rootdir, root, f)
        command = '. / MailUtil delete-verbose-debug' + dest
        (Command_output, exitstatus) = pexpect.run (command, events = {'password:': password + "\ n"}, withexitstatus = 1)
        print command + "\ n \ t" + command_output + "\ n \ t" + str (exitstatus)
        command = '. / MailUtil copy-debug-verbose' '+ src +' "'+ dest
        (Command_output, exitstatus) = pexpect.run (command, events = {'password:': password + "\ n"}, withexitstatus = 1)
        print command + "\ n \ t" + command_output + "\ n \ t" + str (exitstatus)

You would probably have to install pexpect with the usual ‘easy_install pexpect’ or ‘pip install pexpect’

Migrating Imap -> Imap

Here MailUtil gives its best… With the simple command:

mailutil transfer {mail.host1.com/novalidate-cert/user=foo@host1.com}{mail.host2.com/novalidate-cert/user=foo@hosr2.com}

all the folders and all the mail from an IMAP server at host1 will be copied to host2… It’s easy. Isn’t it?

MailUtil

You can find it http://www.washington.edu/pine/getpine/unix.html