Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import imaplib
- import imaplib_connect
- import email
- import re
- import ConfigParser
- import sys
- from imaplib_list_parse import parse_list_response
- from imaplib_connect import open_connection
- config = ConfigParser.ConfigParser()
- hostname = "imap.gmail.com"
- username = "mfurlend"
- password = "pass8900"
- # open a connection
- c = open_connection(hostname,username,password)
- boxlist=[]
- msg_id_list=[]
- # get mailbox names
- typ, data = c.list()
- print c
- for line in data:
- flags, delimiter, mailbox_name = parse_list_response(line)
- if mailbox_name != "[Gmail]":
- c.select(mailbox_name, readonly=True)
- typ, msg_ids = c.search(None, 'ALL')
- boxlist.append(mailbox_name)
- msg_id_list.append(msg_ids)
- #print c.status(mailbox_name, '(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)')
- for boxIndex, eachBox in enumerate (boxlist): #for each mailbox
- c.select(eachBox, readonly=True) #select the mailboxbox
- config.add_section (eachBox)
- print "Mailbox: ",eachBox
- for num in msg_id_list[boxIndex][0].split(): #get each message's header
- typ,msg_data = c.fetch(num,'(BODY.PEEK[HEADER])' )
- for response_part in msg_data: #parse header
- if isinstance(response_part, tuple):
- msg = email.message_from_string(response_part[1])
- if eachBox != "Sent": #record the FROM field for all boxes other than 'Sent'
- for header in ['from' ]:
- # print (msg[header])
- config.set(eachBox,num,(msg[header]))
- else:
- for header in ['to' ]: #record the TO field for 'Sent' box
- # print (msg[header])
- config.set(eachBox,num,(msg[header]))
- with open(username+'.cfg', 'wb') as configfile:
- config.write(configfile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement