-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailReader.py
More file actions
96 lines (56 loc) · 2.12 KB
/
Copy pathEmailReader.py
File metadata and controls
96 lines (56 loc) · 2.12 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import imaplib
import email
from email.header import decode_header
import webbrowser
import os
class EmailReader:
def __init__(self, username, password):
self.username = username
self.password = password
self.spam_list = []
self.pastrami_list = []
def clean(text):
# clean text for creating a folder
return "".join(c if c.isalnum() else "_" for c in text)
def psak(self):
print("How many emails would you like to classify")
N = int(str(input()))
# create an IMAP4 class with SSL
imap = imaplib.IMAP4_SSL("imap.gmail.com")
# authenticate
imap.login(self.username, self.password)
status, messages = imap.select("INBOX")
# number of top emails to fetch
# total number of emails
messages = int(messages[0])
count =0
print('If an email belongs in spam, type \"s\". If it does not belong in spam, type \"n\".')
for i in range(messages, messages-N, -1):
# fetch the email message by ID
res, msg = imap.fetch(str(i), "(RFC822)")
for response in msg:
if isinstance(response, tuple):
# parse a bytes email into a message object
msg = email.message_from_bytes(response[1])
# decode the email subject
subject, encoding = decode_header(msg["Subject"])[0]
if isinstance(subject, bytes):
# if it's a bytes, decode to str
if encoding is not None:
subject = subject.decode(encoding)
count = count+1
print('Subject:{}{}{}'.format(count, " " ,subject))
while True:
response=str(input())
if response =="s":
self.spam_list.append(subject)
break
elif response =="n":
self.pastrami_list.append(subject)
break
else:
print('Invalid response: If an email belongs in spam, type \"s\". If it does not belong in spam, type \"n\".')
imap.close()
imap.logout()
#print(self.spam_list)
#print(self.pastrami_list)