09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 15 ■ IMAP<br />

» » print<br />

def explore_message(c, uid):<br />

» """Let the user view various parts <strong>of</strong> a given message."""<br />

» msgdict = c.fetch(uid, ['BODYSTRUCTURE', 'FLAGS'])<br />

» while True:<br />

» » print<br />

» » print 'Flags:',<br />

» » flaglist = msgdict[uid]['FLAGS']<br />

» » if flaglist:<br />

» » » print ' '.join(flaglist)<br />

» » else:<br />

» » » print 'none'<br />

» » display_structure(msgdict[uid]['BODYSTRUCTURE'])<br />

» » print<br />

» » reply = raw_input('Message %s - type a part name, or "q" to quit: '<br />

» » » » » » % uid).strip()<br />

» » print<br />

» » if reply.lower().startswith('q'):<br />

» » » break<br />

» » key = 'BODY[%s]' % reply<br />

» » try:<br />

» » » msgdict2 = c.fetch(uid, [key])<br />

» » except c._imap.error:<br />

» » » print 'Error - cannot fetch section %r' % reply<br />

» » else:<br />

» » » content = msgdict2[uid][key]<br />

» » » if content:<br />

» » » » print banner<br />

» » » » print content.strip()<br />

» » » » print banner<br />

» » » else:<br />

» » » » print '(No such section)'<br />

def explore_folder(c, name):<br />

» """List the messages in folder `name` and let the user choose one."""<br />

» while True:<br />

» » c.select_folder(name, readonly=True)<br />

» » msgdict = c.fetch('1:*', ['BODY.PEEK[HEADER.FIELDS (FROM SUBJECT)]',<br />

» » » » » » » » 'FLAGS', 'INTERNALDATE', 'RFC822.SIZE'])<br />

» » print<br />

» » for uid in sorted(msgdict):<br />

» » » items = msgdict[uid]<br />

» » » print '%6d %20s %6d bytes %s' % (<br />

» » » » uid, items['INTERNALDATE'], items['RFC822.SIZE'],<br />

» » » » ' '.join(items['FLAGS']))<br />

» » » for i in items['BODY[HEADER.FIELDS (FROM SUBJECT)]'].splitlines():<br />

» » » » print ' ' * 6, i.strip()<br />

» » reply = raw_input('Folder %s - type a message UID, or "q" to quit: '<br />

» » » » » » % name).strip()<br />

» » if reply.lower().startswith('q'):<br />

254

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!