Transferring mail from IMAP server to gmail

I use gmail as a mail dump:

  • ‘current’ and important mail is kept on my fastmail.fm account for access from everywhere
  • ‘old’ mail (>~12 months) from Sent and Trash are semi-regularly transferred to gmail where they can be searched etc whilst freeing up space on both my local machine and fastmail.fm

This means I have to transfer quite large volumes of mail every few months, importantly, keeping all tags, header info etc intact.

For this I use larch and run the following code either locally (with a good internet connection) or on a remote server (usually having fast connection).

#!/bin/sh

larch --from imaps://mail.messagingengine.com -u NAME@DOMAIN.COM -p PASSWORD \
--to imaps://imap.gmail.com -U NAME@gmail.com -P PASSWORD \
--from-folder 'INBOX/from_folder' --to-folder '[Gmail]/All Mail'

This is specific for fastmail, so just change the server information over and the password. Yes, this probably isn’t very secure what with the passwords sitting in a shell script. YMMV.