Chapter 15 ■ IMap
280
if parentparts:
name = '.'.join(parentparts)
else:
print(' HEADER')
name = 'TEXT'
Print a simple, non-multipart MIME part. Include its disposition,
if available.
is_multipart = not isinstance(structure[0], str)
if not is_multipart:
parttype = ('%s/%s' % structure[:2]).lower()
print(' %-9s' % name, parttype, end=' ')
if structure[6]:
print('size=%s' % structure[6], end=' ')
if structure[9]:
print('disposition=%s' % structure[9][0],
' '.join('{}={}'.format(k, v) for k, v in structure[9][1:]),
end=' ')
print()
return
For a multipart part, print all of its subordinate parts.
parttype = 'multipart/%s' % structure[1].lower()
print(' %-9s' % name, parttype, end=' ')
print()
subparts = structure[0]
for i in range(len(subparts)):
display_structure(subparts[i], parentparts + [ str(i + 1) ])
if name == 'main':
main()
You can see that the outer function uses a simple list_folders() call to present the users with a list of e-mail
folders, like some of the program listings previously discussed. Each folder’s IMAP flags are also displayed. This lets
the program give users a choice between folders:
INBOX \HasNoChildren
Receipts \HasNoChildren
Travel \HasNoChildren
Work \HasNoChildren
Type a folder name, or "q" to quit:
Once a user has selected a folder, things become more interesting: a summary has to be printed for each
message. Different e-mail clients make different choices about what information to present about each message in a
folder. The code in Listing 15-7 chooses to select a few header fields together with the message’s date and size. Note
that it is careful to use BODY.PEEK instead of BODY to fetch these items, since the IMAP server would otherwise mark the
messages as \Seen merely because they had been displayed in a summary!