Migrate Maildir to Gmail

Posted by ads on Thursday, 2017-01-12
Posted in [Software]

Recently it was time to refurbish our email infrastructure. For a very long time I was hosting everything for myself, but things don’t get easier and over time even the spam filters couldn’t catch up. Therefore I looked into a number of hosted services, and in the end settled with Google Gmail.

End of last year I ran a test, migrated one of my domains over to Gmail and tried out if Gmail offers all the required functionality. As with every change, not everything works flawlessy, but mostly I found ways to get things working. As example we have different domains, several users, multiple identities (one per domain), other servers (like a webserver) can create emails as well (needs to play well with SPF and DKIM), a number of different email programs, mobile support and more.

Pro tip: if you run a test with Gmail and close the account before they charge you, you not only have to cancel the service, you also have to close the account. Else the domain is still associated with the test account.

For my wife it was mostly moving the emails in Thunderbird from one account to another, and create new identities. For me it is a bit more complicated, as I was previously relying on a combination of mutt, synced emails between two servers using unison, and sending emails from my laptop. This setup was not very mobile friendly, and a number encrypted emails didn’t made things easier.┬áDid I mention that it was time to overhaul everything?

Many years back I switched from the not very friendly Mbox format to Maildir format. This comes handy if mails are to be synced between multiple servers (unison), but doesn’t play well with IMAP. Therefore I needed a way to import the Maildir folders into Gmail.

Scott Yang wrote a script named maildir2gmail.py, which I tried out last year, and added more features. Mostly the flag for handling unread emails, and create folders in IMAP on the fly. The script itself keeps track which emails are already copied, therefore it can be interrupted at any time, and will continue when restarted. Very handy, because I have gathered quite a number of emails over the years …

After enabling less secure apps in Gmail, and creating an account just for the import, I ran the following two commands for every Maildir folder:

for dir in maildir1 maildir2 dir/maildir3 dir/dir/maildir4
    echo "Directory: $dir"
    python maildir2gmail.py -f "transfer/$dir" -u <account> -p <password> -n "mail/$dir/new"
    python maildir2gmail.py -f "transfer/$dir" -u <account> -p <password> "mail/$dir/cur"

This will create a new subdirectory under the “transfer” IMAP folder, and copy unread and read emails into this folder, using the appropriate flag. By using a “transfer” folder I did not directly mix all new emails into the existing folders/labels, but gave myself a chance to move them around as I wish, once the upload was complete.

Categories: [Software]