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

26 lines
590 B
Python
Raw Permalink Normal View History

2017-01-31 03:38:50 +01:00
import imaplib
2019-02-05 02:10:00 +01:00
import re
2023-11-26 13:16:29 +01:00
from dotenv import load_dotenv
from pathlib import Path
import os
path_to_env = Path(__file__).parent / '.env'
load_dotenv(path_to_env)
EMAIL = os.getenv("EMAIL")
PASSWORD = os.getenv("PASSWORD")
if not EMAIL or not PASSWORD:
print("ERROR:Email or password not set in .env file.")
exit(0)
2017-01-31 03:38:50 +01:00
2023-11-26 13:16:29 +01:00
M = imaplib.IMAP4_SSL("imap.gmail.com", 993)
M.login(EMAIL, PASSWORD)
2017-01-31 03:38:50 +01:00
2023-11-26 13:16:29 +01:00
status, counts = M.status("INBOX", "(MESSAGES UNSEEN)")
2017-01-31 03:38:50 +01:00
if status == "OK":
2023-11-26 13:16:29 +01:00
unread = re.search(r"UNSEEN\s(\d+)", counts[0].decode("utf-8")).group(1)
2017-01-31 03:38:50 +01:00
else:
2023-11-26 13:16:29 +01:00
unread = "N/A"
2017-01-31 03:38:50 +01:00
2019-02-05 02:10:00 +01:00
print(unread)