aboutsummaryrefslogtreecommitdiff
path: root/utils/client.py
diff options
context:
space:
mode:
authorhyfc <[email protected]>2018-06-24 14:34:35 +0800
committerhyfc <[email protected]>2020-10-22 11:37:42 +0800
commit9535e3329a5602979cee23ca4072c199449872b1 (patch)
treedfcfb032f8ae9dfbcb98a413d5124ca5995fbb21 /utils/client.py
parent49f3ae04c9831aff10a75e2380eab48eac8144b8 (diff)
downloadtelegram-mail-bot-9535e3329a5602979cee23ca4072c199449872b1.tar.gz
telegram-mail-bot-9535e3329a5602979cee23ca4072c199449872b1.tar.bz2
telegram-mail-bot-9535e3329a5602979cee23ca4072c199449872b1.zip
Using pyzmail36 to parse raw mail data
Diffstat (limited to 'utils/client.py')
-rw-r--r--utils/client.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/utils/client.py b/utils/client.py
new file mode 100644
index 0000000..07518bb
--- /dev/null
+++ b/utils/client.py
@@ -0,0 +1,63 @@
+import logging
+import poplib
+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.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
+ logger.info(server.getwelcome().decode('utf8'))
+ # authenticating
+ server.user(self.email_account)
+ server.pass_(self.password)
+ return server
+
+ def get_mails_list(self):
+ _, mails, _ = self.server.list()
+ return mails
+
+ def get_mails_count(self):
+ mails = self.get_mails_list()
+ return len(mails)
+
+ def get_mail_by_index(self, index):
+ resp_status, mail_lines, mail_octets = self.server.retr(index)
+ return Email(mail_lines)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ if exc_type is None:
+ print('exited normally\n')
+ self.server.quit()
+ else:
+ print('raise an exception! ' + str(exc_type))
+ self.server.close()
+ return False # Propagate
+
+
+
+if __name__ == '__main__':
+ useraccount = "XXXXX"
+ password = "XXXXXX"
+
+ client = EmailClient(useraccount, password)
+ num = client.get_mails_count()
+ print(num)
+ for i in range(1, num):
+ print(client.get_mail_by_index(i)) \ No newline at end of file