def explore_folder(c, name):
 while True:
  c.select_folder(name, readonly=True)
  msgdict = c.fetch('1:*', ['BODY.PEEK[HEADER.FIELDS (FROM SUBJECT)]', 'FLAGS', 'INTERNALDATE', 'RFC822.SIZE'])
  print
  for uid in sorted(msgdict):
   items = msgdict[uid]
   print ('%6d  %20s  %6d bytes  %s' % (uid, items['INTERNALDATE'], items['RFC822.SIZE'], ' '.join(items['FLAGS'])))
   for i in items['BODY[HEADER.FIELDS (FROM SUBJECT)]'].splitlines():
    print (' ' * 6), i.strip()
  reply = raw_input(('Folder %s - type a message UID, or "q" to quit: ' % name)).strip()
  if reply.lower().startswith('q'):
   break
  try:
   reply = int(reply)
  except ValueError:
   print 'Please type an integer or "q" to quit'
  else:
   if (reply in msgdict):
    explore_message(c, reply)
 c.close_folder()

def explore_folder ( c , name ) : while True : c . select_folder ( name , readonly = True ) msgdict = c . fetch ( '1:*' , [ 'BODY.PEEK[HEADER.FIELDS (FROM SUBJECT)]' , 'FLAGS' , 'INTERNALDATE' , 'RFC822.SIZE' ] ) print for uid in sorted ( msgdict ) : items = msgdict [ uid ] print ( '%6d  %20s  %6d bytes  %s' % ( uid , items [ 'INTERNALDATE' ] , items [ 'RFC822.SIZE' ] , ' ' . join ( items [ 'FLAGS' ] ) ) ) for i in items [ 'BODY[HEADER.FIELDS (FROM SUBJECT)]' ] . splitlines ( ) : print ( ' ' * 6 ) , i . strip ( ) reply = raw_input ( ( 'Folder %s - type a message UID, or "q" to quit: ' % name ) ) . strip ( ) if reply . lower ( ) . startswith ( 'q' ) : break try : reply = int ( reply ) except ValueError : print 'Please type an integer or "q" to quit' else : if ( reply in msgdict ) : explore_message ( c , reply ) c . close_folder ( )
list the messages in folder name and let the user choose one .