I used this to upload all of my old chats saved as .eml files directly into my new Gmail account. It uses IMAP’s append to do this. I had tried Thunderbird and the ImportExportTools plugin, but ran into trouble.
This puts all of the messages into a label called “oldchats”. This label needs to exist before this program is run. It also deletes each message after it is uploaded. I made a backup copy of my messages.
import email import os import time import imaplib imap = imaplib.IMAP4_SSL('imap.gmail.com', 993) imap.login('email@example.com', 'password') os.chdir("chats") for filename in os.listdir("."): print filename, raw_eml = file(filename).read() msg = email.message_from_string(raw_eml) date = email.utils.parsedate(msg['Date']) print date, imap.append('oldchats', None, date, raw_eml) print "done" os.remove(filename)