Exporting OS X Mail to a GMail account

By , 06/10/2015 02:17

I have a client that started using Google Apps but had a huge history of emails in the Mail app on OS X which they wanted to have imported into GMail. While the Outlook mailboxes worked without a problem using the program provided by Google, for Mac this doesn’t seem to be maintained any more. The information on the web on this subject is also rather scarce so this is how I did it using some scripting, Dovecot and imapsync.

First I exported all the mailboxes from the Mail app to a USB drive. This included the Inbox, but also extra folders that were located outside of the Inbox, so this took a couple of extra clicks. Next I moved the USB drive to a Linux machine, you could do similar stuff on Mac or Windows I guess but that’s not what I’m most comfortable with.

The way that the Mail app exports the mbox files is not directly usable to do the import which I wanted to do with imapsync (to keep all the folders) so I had to transform it a bit.

First I generated a (processed) list of subfolders to use.

# find -type d | grep -v noindex | grep mbox | sed 's:^\./::g' | sed 's:.mbox::g' > toexport

Next I used this list to copy all the mbox files to a structure that Dovecot can use.

# while read line; do mkdir -p "/tmp/export/$line"; cp "$line.mbox/mbox" "/tmp/export/$line/"; done < toexport

After that I changed the dovecot config to point to the directory. I also had to disable ldap authentication on my Gentoo box.

protocols = imap
mail_location = mbox:/tmp/export:DIRNAME=mbox:INDEX=/tmp/export-index:CONTROL=/tmp/export-control

After starting Dovecot I checked using an email-client that the mails were being served properly by Dovecot. This takes a long time on a big mailbox as Dovecot will start reading and indexing all the mbox files. If you don’t do this check that’s fine but Dovecot will do the indexing the first time you use imapsync.

Finally I started syncing the mailboxes. I had to enable “unsafe” applications to be able to login to GMail using imapsync. Also note that you can only upload 500 MB a day to GMail through IMAP. imapsync has the handy option –exitwhenover for that.

/usr/bin/imapsync --host1 localhost --user1 user1 --passfile1 imapsecret1 --host2 imap.gmail.com --user2 user2 --passfile2 imapsecret2 --port2 993 --ssl2 --noauthmd5 --authmech2 LOGIN --syncinternaldates --split1 100 --split2 100 --allowsizemismatch --useheader Message-ID --nofoldersizes --addheader --exitwhenover 450000000

  • I used password files because the random characters were giving me issues when used on commandline
  • I limited the amount of data transfered to 450 MB as Google only allows 500 MB / day upload over IMAP
  • All the other options are based on experimentation and info I found on the net. It was the only way I could get the sync to start properly and continue where it left off

Flattr this!

Leave a Reply


Panorama Theme by Themocracy