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
|
import logging
from imapclient import IMAPClient
from utils.mail import Email
logger = logging.getLogger(__name__)
class EmailClient(object):
def __init__(self, email_account, passwd):
self.email_account = email_account
self.password = passwd
self.server, self.number = self.connect(self)
@staticmethod
def connect(self):
# parse the server's hostname from email account
imap4_server = "mx1." + self.email_account.split("@")[-1]
server = IMAPClient(host=imap4_server, use_uid=True)
# display the welcome info received from server,
# indicating the connection is set up properly
logger.info(server.welcome)
# authenticating
server.login(self.email_account, self.password)
return server, server.select_folder("INBOX")[b"EXISTS"]
def get_mails_list(self):
messages = self.server.search()
dict = self.server.fetch(messages, "RFC822")
return sorted(dict.items(), key=lambda e: e[0]) # Do resorts
def get_mails_count(self):
return self.number
def get_mail_by_index(self, index):
list = self.get_mails_list()
msg_data = list[int(index) - 1][1]
return Email(msg_data[b"RFC822"])
def get_listed_mails(self):
list = self.get_mails_list()
listed_mails = []
for i in range(0, self.get_mails_count()):
email = Email(list[i][1][b"RFC822"])
listed_mails.append([email.subject, email.sender, email.date])
return listed_mails
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
logger.info("exited normally\n")
self.server.shutdown()
else:
logger.error("raise an exception! " + str(exc_type))
self.server.logout()
return False # Propagate
if __name__ == "__main__":
useraccount = "XXXXX"
password = "XXXXXX"
client = EmailClient(useraccount, password)
num = client.get_mails_count()
print(num)
for uid, msg_data in client.get_mails_list().items():
print(client.get_mail_by_index(uid))
|