aboutsummaryrefslogtreecommitdiff
path: root/utils/mail.py
blob: 3dc346fd311b2f9906d8eb726d1b271be5289917 (plain)
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
import poplib


class MailInfo(object):
    """
    Class for storing mail's metadata
    """
    def __init__(self):
        self.index = 0
        self.size = 0
        self.status = ""
        self.data = ""

class EmailClient(object):
    def __init__(self, email_account, password):
        self.email_account = email_account
        self.password = password
        self.server = self.connect(self)

    @staticmethod
    def connect(self):
        # parse the server's hostname from email account
        pop3_server = 'pop.'+self.email_account.split('@')[-1]

        server = poplib.POP3_SSL(pop3_server)

        # display the welcome info received from server,
        # indicating the connection is set up properly
        print(server.getwelcome().decode('utf8'))

        # authenticating
        server.user(self.email_account)
        server.pass_(self.password)

        return server

    def get_mail_count(self):
        _, mails, _ = self.server.list()
        return len(mails)



if __name__ == '__main__':
    useraccount = "XXXXX"
    password = "XXXXXX"

    client = EmailClient(useraccount, password)
    client.get_mail_count()