Chapter 15 ■ IMap
278
else:
explore_account(c)
finally:
c.logout()
def explore_account(c):
"""Display the folders in this IMAP account and let the user choose one."""
while True:
print()
folderflags = {}
data = c.list_folders()
for flags, delimiter, name in data:
folderflags[name] = flags
for name in sorted(folderflags.keys()):
print('%-30s %s' % (name, ' '.join(folderflags[name])))
print()
reply = input('Type a folder name, or "q" to quit: ').strip()
if reply.lower().startswith('q'):
break
if reply in folderflags:
explore_folder(c, reply)
else:
print('Error: no folder named', repr(reply))
def explore_folder(c, name):
"""List the messages in folder name
and let the user choose one."""
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 = 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')