-
Notifications
You must be signed in to change notification settings - Fork 5
/
extract.py
executable file
·41 lines (35 loc) · 1.07 KB
/
extract.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
#!/usr/bin/env python3
import dmarc
import email
import os
ACCEPTED_MIME = [
'application/gzip',
'application/x-gzip',
'application/zip',
'application/x-zip-compressed',
'application/octet-stream',
]
def save_file(attachment, mtime):
f_name = attachment.get_filename()
dest = os.path.join('./reports', f_name)
if os.path.exists(dest):
return
with open(dest, 'wb') as f:
f.write(attachment.get_payload(decode=True))
os.utime(dest, (mtime, mtime))
if not os.path.isdir('./reports'):
os.mkdir('./reports')
for root, dirs, files in os.walk('./mails/INBOX/'):
for f in files:
mail_file = os.path.join(root, f)
mtime = os.path.getmtime(mail_file)
msg = email.message_from_file(open(mail_file))
if msg.is_multipart():
for attach in msg.get_payload():
if attach.get_content_type() in ACCEPTED_MIME:
save_file(attach, mtime)
else:
save_file(msg, mtime)
i_dmarc = dmarc.dmarc()
i_dmarc.parse()
i_dmarc.render()