-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmbox_parser.py
55 lines (45 loc) · 1.84 KB
/
mbox_parser.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
43
44
45
46
47
48
49
50
51
52
53
54
55
import mailbox
import email.utils
import os
from db_manager import create_db, store_data
create_db()
def parse_mbox_file(mbox_file):
data = []
mbox = mailbox.mbox(mbox_file)
for message in mbox:
# Extract email details
subject = message['subject']
sender_info = message['from']
date = message['date']
receiver_info = message['to']
sender_name, sender_email = email.utils.parseaddr(sender_info)
receiver_name, receiver_email = email.utils.parseaddr(receiver_info)
for part in message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
attachment_name = part.get_filename()
content_type = part.get_content_type()
attachment_data = part.get_payload(decode=True)
if attachment_data:
save_dir = 'attachments'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
attachment_path = os.path.join(save_dir, attachment_name)
with open(attachment_path, 'wb') as file:
file.write(attachment_data)
email_data = {
'subject': subject,
'sender_name': sender_name,
'sender_email': sender_email,
'receiver_name': receiver_name,
'receiver_email': receiver_email,
'attachment_name': attachment_name,
'content_type': content_type,
'datetime': date,
}
data.append(email_data)
store_data(email_data)
return data
# print(parse_mbox_file('D:\Languages\Python\Email Attachments\cindyloh3333_gmail.com.mbox'))