-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinbox.py
More file actions
42 lines (31 loc) · 1.18 KB
/
Copy pathinbox.py
File metadata and controls
42 lines (31 loc) · 1.18 KB
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
import imaplib
import email
host = 'imap.gmail.com'
username = 'pyjohndoepy@gmail.com'
password = 'pythonpython20'
def get_inbox():
mail = imaplib.IMAP4_SSL(host)
mail.login(username,password)
mail.select('inbox')
_, search_data = mail.search(None, 'UNSEEN')
my_message = []
for num in search_data[0].split():
email_data = {}
_, data = mail.fetch(num , '(RFC822)')
_, b = data[0]
email_message = email.message_from_bytes(b)
for header in ['subject', 'to' ,'from ', 'date']:
print('{} : {}'.format(header , email_message[header]))
email_data['header'] = email_message['header']
for part in email_message.walk():
if part.get_content_type() == 'text/plain':
body = part.get_payload(decode=True)
email_data['body'] = body.decode()
elif part.get_content_type() == 'html':
html_body = part.get_payload(decode=True)
email_data['html_body'] = html_body.decode()
my_message.append(email_data)
return my_message
if __name__ == "__main__":
my_inbox = get_inbox()
print(my_inbox)