Migrate all email from IMAP server to Gmail using OfflineIMAP

OfflineIMAP (github, website) – a great way to migrate email (docs here. Just moved my email from my webhost IMAP-server to a gmail account.

I had some problems connecting to gmail until I enabled “less secure apps” to access my account at https://www.google.com/settings/security/lesssecureapps. Also, Gmail automatically blocked my first access, so I recieved an email that I had to respond to (click a link and verify that it actually was me that tried to access my email – good feature).

Also, OfflineIMAP might complain about unknown certificate fingerprints and print them out. I just added them to the servers that needed them and hoped nobody had manipulated them.

Here is a template for migrating for future reference.

# Offlineimap sample configuration file

[general]
metadata = ~/.offlineimap
accounts = migration
ui = TTYUI

[Account migration]
localrepository = gmail-trg
remoterepository = imap-src

# use sqlite backend rather than plain text
status_backend = sqlite

[Repository imap-src]
type = IMAP
remotehost = imap.yourdomain.com
ssl = yes
remoteport = 993
remoteuser = imap.username
remotepass = imap.password
usecompression = no
maxconnections = 1

# don't write any changes to the imap server
readonly = True

# you might need to fill this in if you use SSL
#cert_fingerprint = 

[Repository gmail-trg]
# You can use Gmail as type, by I had some problems connecting.
type = IMAP
remotehost = imap.gmail.com
ssl = yes
remoteport = 993
remoteuser = gmail.username
remotepass = gmail.password
usecompression = no
maxconnections = 1
readonly = False

# Set cert_fingerprint to whatever you need
#cert_fingerprint = 

# Do not sync [Gmail]/All mail etc folders
folderfilter = lambda foldername: not re.search('^\[Gmail\]', foldername)
Advertisement

New email setup

I think I am going to be quite happy with my new email setup. It has taken a while, actually I’m not quite done yet, but I think it is going to be awesome.

I started writing a lengthy post about this, but it grew to long. So, here’s the tl;dw (too long, did not write) version:

  • I had too much email in my inbox that came from different areas of responsibility.
  • After moving my email from my gmail to my work imap (Exchange), I discovered Exchange does not support keywords (custom flags).
  • I forward my work email to a third party IMAP that supports keywords (Dovecot), but still use my work SMTP.

Copying/Migrating email via IMAP using larch

For a couple of years, I have forwarded all my email to my gmail account. This has been quite convenient. However, this has also resulted in workflow issues, or plainly put, me checking and replying to work related email messages when not working and vice versa.

So, I decided to separate my email accounts again. I have during these years, set up a number of labels etc in gmail that I wish to preserve.

After dragging and dropping email messages for a while, I started looking into how to automate this task. I have ended up using larch which works great. I create a config file where I enter all my details (source and target servers, source and target folders, folders to exclude, etc). larch can be used to recusively sync a specified folder tree which is the option I went for. It also automatically retries if there is a glitch.