Advertisement
Guest User

Untitled

a guest
May 14th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. import imaplib
  2. import imaplib_connect
  3. import email
  4. import re
  5. import ConfigParser
  6. import sys
  7.  
  8. from imaplib_list_parse import parse_list_response
  9. from imaplib_connect import open_connection
  10.  
  11. config = ConfigParser.ConfigParser()
  12.  
  13. hostname = "imap.gmail.com"
  14. username = "mfurlend"
  15. password = "pass8900"
  16. # open a connection
  17. c = open_connection(hostname,username,password)
  18. boxlist=[]
  19. msg_id_list=[]
  20. # get mailbox names
  21. typ, data = c.list()
  22. print c
  23. for line in data:
  24.         flags, delimiter, mailbox_name = parse_list_response(line)
  25.         if mailbox_name != "[Gmail]":
  26.                 c.select(mailbox_name, readonly=True)
  27.                 typ, msg_ids = c.search(None, 'ALL')  
  28.                 boxlist.append(mailbox_name)
  29.                 msg_id_list.append(msg_ids)        
  30.                 #print c.status(mailbox_name, '(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)')
  31.                                    
  32. for boxIndex, eachBox in enumerate (boxlist): #for each mailbox
  33.         c.select(eachBox, readonly=True) #select the mailboxbox
  34.         config.add_section (eachBox)
  35.         print "Mailbox: ",eachBox
  36.         for num in msg_id_list[boxIndex][0].split(): #get each message's header
  37.                 typ,msg_data = c.fetch(num,'(BODY.PEEK[HEADER])' )
  38.  
  39.                 for response_part in msg_data: #parse header
  40.                         if isinstance(response_part, tuple):
  41.                                 msg = email.message_from_string(response_part[1])
  42.  
  43.                                 if eachBox != "Sent": #record the FROM field for all boxes other than 'Sent'
  44.                                         for header in ['from' ]:
  45.                                                # print (msg[header])
  46.                                                 config.set(eachBox,num,(msg[header]))
  47.                                 else:
  48.                                         for header in ['to' ]: #record the TO field for 'Sent' box
  49.                                                # print (msg[header])
  50.                                                 config.set(eachBox,num,(msg[header]))
  51. with open(username+'.cfg', 'wb') as configfile:
  52.     config.write(configfile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement