My Awesome Config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.2KB

  1. #!/usr/bin/python
  2. # Create ~/.local/share/keyrings/mail_loop_keys.py:
  3. # mymail_server = 'imap.example.com'
  4. # mymail_port = 143
  5. # mymail_login = 'username'
  6. # mymail_password = 'password'
  7. import imaplib,ssl,sys,os
  8. # http://stackoverflow.com/questions/9713055/certificate-authority-for-imaplib-and-poplib-python
  9. import imaplib,ssl
  10. def IMAP_starttls(self, keyfile=None, certfile=None,cert_reqs=ssl.CERT_NONE,ca_certs=None):
  11. if not 'STARTTLS' in self.capabilities:
  12. raise self.error("STARTTLS extension not supported by server.")
  13. (resp, reply) = self._simple_command("STARTTLS")
  14. self.sock = ssl.wrap_socket(self.sock, keyfile, certfile,cert_reqs=cert_reqs,ca_certs=ca_certs)
  15. self.file = self.sock.makefile('rb')
  16. imaplib.IMAP4.__dict__['starttls']=IMAP_starttls
  17. imaplib.Commands['STARTTLS']=('NONAUTH',)
  18. try:
  19. sys.path.insert (0, os.getenv("HOME")+"/.local/share/keyrings")
  20. from mail_loop_keys import mymail_server,mymail_port,mymail_login,mymail_password
  21. M=imaplib.IMAP4(mymail_server, mymail_port)
  22. M.starttls()
  23. M.login(mymail_login,mymail_password)
  24. status, counts = M.status("Inbox","(MESSAGES UNSEEN)")
  25. unread = counts[0].split()[4][:-1]
  26. M.logout()
  27. print(int(unread))
  28. except:
  29. print("?")