Skip to content

Instantly share code, notes, and snippets.

@sebw
Created July 14, 2022 23:52
Show Gist options
  • Save sebw/ac7b67bb6faea5be227e7360c0afb908 to your computer and use it in GitHub Desktop.
Save sebw/ac7b67bb6faea5be227e7360c0afb908 to your computer and use it in GitHub Desktop.
Count imap unread emails
#!/usr/bin/env python
"""
small script to check for unread count on imap inbox
"""
import imaplib
IMAPSERVER = 'imap.fastmail.com'
USER = 'user@domain'
PASSWORD = 'xxxxxxxx'
try:
mail = imaplib.IMAP4_SSL(IMAPSERVER)
mail.login(USER, PASSWORD)
mail.select("INBOX", True) # connect to inbox.
return_code, mail_ids = mail.search(None, 'UnSeen')
converted = str(mail_ids[0], "UTF-8")
if converted == "":
count = 0
else:
count = len(converted.split(" "))
except:
count = 0
if count == 0:
print("<span color='#a3be8c'>📪 " + str(count) + "</span>")
else:
print("<span color='#bf616a'>📬 " + str(count) + "</span>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment