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

32 lines
553 B
Python
Raw Normal View History

2018-11-29 10:39:53 +00:00
#!/usr/bin/env python3
2018-11-28 14:31:35 +00:00
import imaplib
import email
2021-11-16 17:06:38 +00:00
import sys
2018-11-28 14:31:35 +00:00
2018-11-29 10:39:53 +00:00
try:
import credentials
except:
print("Couldn't read crendtials")
exit(1)
2018-11-28 14:31:35 +00:00
2018-11-29 10:39:53 +00:00
ok = True
unread = 0
2018-11-28 14:31:35 +00:00
2018-11-29 10:39:53 +00: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 14:31:35 +00:00
2018-11-29 10:39:53 +00: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)