-
-
Notifications
You must be signed in to change notification settings - Fork 641
/
folder_summary.py
39 lines (34 loc) · 1.29 KB
/
folder_summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter15/folder_summary.py
# Opening an IMAP connection with IMAPClient and retrieving mailbox messages.
import email, getpass, sys
from imapclient import IMAPClient
def main():
if len(sys.argv) != 4:
print('usage: %s hostname username foldername' % sys.argv[0])
sys.exit(2)
hostname, username, foldername = sys.argv[1:]
c = IMAPClient(hostname, ssl=True)
try:
c.login(username, getpass.getpass())
except c.Error as e:
print('Could not log in:', e)
else:
print_summary(c, foldername)
finally:
c.logout()
def print_summary(c, foldername):
c.select_folder(foldername, readonly=True)
msgdict = c.fetch('1:*', ['BODY.PEEK[]'])
for message_id, message in list(msgdict.items()):
e = email.message_from_string(message['BODY[]'])
print(message_id, e['From'])
payload = e.get_payload()
if isinstance(payload, list):
part_content_types = [ part.get_content_type() for part in payload ]
print(' Parts:', ' '.join(part_content_types))
else:
print(' ', ' '.join(payload[:60].split()), '...')
if __name__ == '__main__':
main()