-
Notifications
You must be signed in to change notification settings - Fork 0
/
readmail.py
38 lines (30 loc) · 947 Bytes
/
readmail.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
#!/usr/bin/env python3
import sys
import email
from email.policy import default
class MboxReader:
def __init__(self, filename):
self.handle = open(filename, 'rb')
assert self.handle.readline().startswith(b'From ')
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
self.handle.close()
def __iter__(self):
return iter(self.__next__())
def __next__(self):
lines = []
while True:
line = self.handle.readline()
if line == b'' or line.startswith(b'From '):
#yield email.message_from_bytes(b''.join(lines), policy=default)
if line == b'':
break
lines = []
continue
lines.append(line)
i=0
with MboxReader(sys.argv[1]) as mbox:
for message in mbox:
print(sys.argv[1], '-', i, ':', message.as_string())
i=i+1