If you created a new Gmail account and wished you could migrate all your mail from an old Gmail account across, or if you yearned to make a seamless switch from Gmail to Google Apps, then this will provide a solution for you.
The Solution: imapsync
This method uses imapsync and preserves labels and mail, including Sent items. imapsync is available in many linux distributions.
You must have enabled IMAP access on both your source and destination Gmail accounts. If you have enabled the IMAP-Controls feature in Gmail labs you must ensure all labels have been tagged “Show in IMAP”
This script will sync all labels and mail from user1 to user2. The process is done in two steps, first all labels are created, then the mail is copied. Mail from Trash and Spam is excluded.
It requires you have your passwords in $HOME/.imap/passfile1 and passfile2 for authentication.
#!/bin/bash user1='username1@gmail.com' user2='username2@gmail.com' # escape the @ symbols user1e=${user1//@/\\@} user2e=${user2//@/\\@} # Create folders/labels first imapsync --host1 imap.gmail.com \ --port1 993 --user1 "$user1" \ --passfile1 $HOME/.imap/passfile1 --ssl1 \ --host2 imap.gmail.com \ --port2 993 --user2 "$user2" \ --passfile2 $HOME/.imap/passfile2 --ssl2 \ --syncinternaldates --split1 100 --split2 100 \ --authmech1 LOGIN --authmech2 LOGIN \ --exclude "\[Gmail\].*" \ --justfolders --nofoldersizes # Sync mail, only copy new items imapsync --host1 imap.gmail.com \ --port1 993 --user1 "$user1" \ --passfile1 $HOME/.imap/passfile1 --ssl1 \ --host2 imap.gmail.com \ --port2 993 --user2 "$user2" \ --passfile2 $HOME/.imap/passfile2 --ssl2 \ --syncinternaldates --split1 100 --split2 100 \ --authmech1 LOGIN --authmech2 LOGIN \ --useheader "Message-ID" --skipsize --allowsizemismatch \ --exclude "\[Gmail\]\/Spam|\[Gmail\]\/Trash" \ --regexmess "s/Delivered-To: $user1e/Delivered-To: $user2e/g"
References
Gmail to Google Apps Email Migration
Migrate email from Gmail to Google Apps
Other Options
Gmail-Backup (not tested)
Python migration script (not tested)