# coding=utf-8 import imaplib # Python password input module import getpass import os # File containing IMAP_USER's PASSWORD. # If file not found, will fall back to getpass # PyCharm's working directory is the folder containing # the python source file. PASSWORD_FILE = 'password.txt' # Modify the following lines as directed in the exercise. # Initially, no mailbox is selected for display. IMAP_SERVER = 'outlook.office365.com' IMAP_USER = 'student@msoe.edu' IMAP_MAILBOX = '' IMAP_MESSAGE_NUM = '' IMAP_MESSAGE_FETCH_SPEC = '(BODY[])' IMAP_PRINT_FETCH_DATA = False # You can experiment with some of these message fetch specifications. # IMAP_MESSAGE_FETCH_SPEC = '(BODY)' # IMAP_MESSAGE_FETCH_SPEC = '(BODY[HEADER])' # IMAP_MESSAGE_FETCH_SPEC = '(BODY[1])' # IMAP_MESSAGE_FETCH_SPEC = '(BODY[HEADER.FIELDS (FROM SUBJECT)])' # IMAP_MESSAGE_FETCH_SPEC = '(BODYSTRUCTURE)' # IMAP_MESSAGE_FETCH_SPEC = '(BODY[TEXT])' def main(): # Prompt user for email password if os.path.isfile(PASSWORD_FILE): with open(PASSWORD_FILE) as f: password = f.read() # or similar else: print 'Warning: Could not open',PASSWORD_FILE+'. Falling back to getpass' password = getpass.getpass('Could not open '+PASSWORD_FILE+'. Please enter your password: ') imap_interface = imaplib.IMAP4_SSL(IMAP_SERVER) imap_interface.login(IMAP_USER,password) password = '' # Clear password once used for extra security (especially when debugging) # Uncomment the ".debug" line to get IMAP debugging output. # Don't turn on debug earlier than this point in the code, # or your password will appear in clear text in the output!! imap_interface.debug = 4 mailboxes = imap_interface.list() # print "mailboxes=",mailboxes # Status is in the first element of "list" return value print '### List status = {0}'.format(mailboxes[0]) # Mailboxes are in the second element of "list" return value for mbox in mailboxes[1]: print '### Mailbox = {0}'.format(mbox) # If a mailbox was specified, fetch its contents. if IMAP_MAILBOX: print '### Fetching mailbox "{0}"'.format(IMAP_MAILBOX) imap_interface.select(IMAP_MAILBOX) # Search for "ALL" messages in mailbox. search_criteria = "ALL" (status,data) = imap_interface.search(None,search_criteria) print '### Search status = "{0}"'.format(status) # print 'data="{0}"'.format(data) # Get string with message numbers, and separate them into a list. message_numbers = data[0].split() print '### Found {0} messages in mailbox'.format(len(message_numbers)) # print 'message_numbers=',message_numbers # Limit number of messages to list? # Modify the "slice" specification if you want to change the messages listed. message_numbers = message_numbers[0:5] # print 'message_numbers limited =',message_numbers for num in message_numbers: status,data = imap_interface.fetch(num,'(UID BODY[TEXT])') print '### Fetch status = "{0}"'.format(status) print '### Message {0}:\n {1}\n\n'.format(num,data[0][1]) if IMAP_MESSAGE_NUM: print '### Processing message {0} . . .'.format(IMAP_MESSAGE_NUM) status,data = imap_interface.fetch(IMAP_MESSAGE_NUM,IMAP_MESSAGE_FETCH_SPEC) print '### Message {0} fetch status = {1} data length = {2}'.format(IMAP_MESSAGE_NUM,status,len(data)) if IMAP_PRINT_FETCH_DATA: for dd in data: if isinstance(dd,tuple) and len(dd) == 2: print '### Fetch data: envelope = {0}, data:\n####Begin####{1}\n####End####'.format(dd[0],dd[1]) else: print '### Fetch data length = {0} data: {1}'.format(len(dd),dd) with open('imaplib-mail.txt','wb') as outfile: # Element 'data[0]' should (almost?) always be a tuple (pair) with envelope # info (fetch spec and length) and data, # but handle the case when it might not be, just in case. if isinstance(data[0],tuple) and len(data[0]) == 2: print '### Writing data from fetch tuple, envelope: "{0}"'.format(data[0][0]) outfile.write(data[0][1]) else: print "### Writing data from fetch object" outfile.write(data[0]) # Close the selected mailbox imap_interface.close() imap_interface.logout() main()