Created
March 5, 2017 01:36
-
-
Save drakerehfeld/1535d6ff65da85f5df8a5c2bd841f1a2 to your computer and use it in GitHub Desktop.
Python IMAP Client for Downloading Email to Text
This file contains 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
import getpass, imaplib | |
import os | |
os.chdir("emails") | |
M = imaplib.IMAP4('<insert IMAP server domain>') | |
M.login('<insert email address>', '<insert email password>') | |
M.select() | |
typ, data = M.search(None, 'ALL') | |
#data is an array of strings with one element, the ids of emails split by spaces, ex: ["1 2 3 4 5"] | |
data = data[0].split() | |
for each in data: | |
#writing each email to its own file | |
filename = str(each) + "-messageBody.txt" | |
with open(filename,"w") as fo: | |
typ, data = M.fetch(each, '(BODY.PEEK[TEXT])') #this is the weird message_parts imap standard -- I will never understand | |
fo.write(data[0][1]) | |
M.close() | |
M.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment