forked from brandon-rhodes/fopnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_email.py
executable file
·42 lines (37 loc) · 1.53 KB
/
display_email.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
40
41
42
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter12/display_email.py
#
# Someday this script will use message.iter_attachments() instead of
# walking through them itself, but http://bugs.python.org/issue21079
import argparse, email.policy, sys
def main(binary_file):
policy = email.policy.SMTP
message = email.message_from_binary_file(binary_file, policy=policy)
for header in ['From', 'To', 'Date', 'Subject']:
print(header + ':', message.get(header, '(none)'))
print()
try:
body = message.get_body(preferencelist=('plain', 'html'))
except KeyError:
print('<This message lacks a printable text or HTML body>')
else:
print(body.get_content())
for part in message.walk():
cd = part['Content-Disposition']
is_attachment = cd and cd.split(';')[0].lower() == 'attachment'
if not is_attachment:
continue
content = part.get_content()
print('* {} attachment named {!r}: {} object of length {}'.format(
part.get_content_type(), part.get_filename(),
type(content).__name__, len(content)))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Parse and print an email')
parser.add_argument('filename', nargs='?', help='File containing an email')
args = parser.parse_args()
if args.filename is None:
main(sys.stdin.buffer)
else:
with open(args.filename, 'rb') as f:
main(f)