-
-
Save zaiste/b0461993bf9d89cd0416f2eb51c93436 to your computer and use it in GitHub Desktop.
Simple script to dump an IMAP folder into eml files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.password | |
Downloads/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
#python ./dump-imap.py -s server.net -u [email protected] -r "Subfolder" | |
import imaplib | |
import getpass | |
import argparse | |
import os.path | |
argparser = argparse.ArgumentParser(description="Dump a IMAP folder into .eml files") | |
argparser.add_argument('-s', dest='host', help="IMAP host, like imap.gmail.com", required=True) | |
argparser.add_argument('-u', dest='username', help="IMAP username", required=True) | |
argparser.add_argument('-r', dest='remote_subfolder', help="Remote folder to download", default='') | |
args = argparser.parse_args() | |
file = open('.password', 'r') | |
password = file.read().strip() | |
local_folder = './Downloads/' + args.remote_subfolder | |
if not os.path.exists(local_folder): | |
os.makedirs(local_folder) | |
remote_folder = 'INBOX.' + args.remote_subfolder | |
gmail = imaplib.IMAP4_SSL(args.host) | |
gmail.login(args.username, password) | |
gmail.select(remote_folder) | |
typ, data = gmail.search(None, 'ALL') | |
for num in data[0].split(): | |
typ, data = gmail.fetch(num, '(RFC822)') | |
f = open('%s/%s.eml' %(local_folder, num), 'w') | |
print >> f, data[0][1] | |
gmail.close() | |
gmail.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment