.awesome/mail_loop_mymail.py

38 lines
1.2 KiB
Python
Raw Normal View History

2014-08-15 00:40:31 +04:00
#!/usr/bin/python
2014-08-16 15:19:58 +04:00
# Create ~/.local/share/keyrings/mail_loop_keys.py:
# mymail_server = 'imap.example.com'
# mymail_port = 143
# mymail_login = 'username'
# mymail_password = 'password'
2014-08-15 00:40:31 +04:00
2014-08-16 15:19:58 +04:00
import imaplib,ssl,sys,os
2014-08-15 00:40:31 +04:00
# http://stackoverflow.com/questions/9713055/certificate-authority-for-imaplib-and-poplib-python
import imaplib,ssl
def IMAP_starttls(self, keyfile=None, certfile=None,cert_reqs=ssl.CERT_NONE,ca_certs=None):
if not 'STARTTLS' in self.capabilities:
raise self.error("STARTTLS extension not supported by server.")
(resp, reply) = self._simple_command("STARTTLS")
self.sock = ssl.wrap_socket(self.sock, keyfile, certfile,cert_reqs=cert_reqs,ca_certs=ca_certs)
self.file = self.sock.makefile('rb')
imaplib.IMAP4.__dict__['starttls']=IMAP_starttls
imaplib.Commands['STARTTLS']=('NONAUTH',)
2014-08-15 09:28:31 +04:00
try:
2014-08-16 15:19:58 +04:00
sys.path.insert (0, os.getenv("HOME")+"/.local/share/keyrings")
from mail_loop_keys import mymail_server,mymail_port,mymail_login,mymail_password
2014-08-15 00:40:31 +04:00
2014-08-15 09:28:31 +04:00
M=imaplib.IMAP4(mymail_server, mymail_port)
M.starttls()
M.login(mymail_login,mymail_password)
2014-08-15 00:40:31 +04:00
2014-08-15 09:28:31 +04:00
status, counts = M.status("Inbox","(MESSAGES UNSEEN)")
2014-08-15 00:40:31 +04:00
2014-08-15 09:28:31 +04:00
unread = counts[0].split()[4][:-1]
M.logout()
print(int(unread))
except:
print("?")