awesome/awesome-wm-widgets/email-widget/count_unread_emails.py

31 lines
542 B
Python
Raw Normal View History

2018-11-29 11:39:53 +01:00
#!/usr/bin/env python3
2018-11-28 15:31:35 +01:00
import imaplib
import email
2018-11-29 11:39:53 +01:00
try:
import credentials
except:
print("Couldn't read crendtials")
exit(1)
2018-11-28 15:31:35 +01:00
2018-11-29 11:39:53 +01:00
ok = True
unread = 0
2018-11-28 15:31:35 +01:00
2018-11-29 11:39:53 +01:00
for account in credentials.accounts:
mailbox = imaplib.IMAP4_SSL(account.host, 993)
mailbox.login(account.username, account.password)
status, counts = mailbox.status("INBOX","(MESSAGES UNSEEN)")
2018-11-28 15:31:35 +01:00
2018-11-29 11:39:53 +01:00
if status == "OK":
unread += int(counts[0].split()[4][:-1])
else:
ok = False
if not ok and unread == 0:
print('N/A')
else:
print(unread)