Skip to content

Instantly share code, notes, and snippets.

@pratyushmittal
Last active September 7, 2022 07:04
Show Gist options
  • Save pratyushmittal/1e8a6dd3c3ead9dc60d8cfa537b125b7 to your computer and use it in GitHub Desktop.
Save pratyushmittal/1e8a6dd3c3ead9dc60d8cfa537b125b7 to your computer and use it in GitHub Desktop.
A SwiftBar script to show unread emails in menu-bar
[email protected]
PASSWORD=your-app-password

This is a simple SwiftBar script to show unread-emails in menu-bar.

We use it for showing emails count from our error logs. In Django, all the errors are forwarded to an email. This shows the count of unhandled errors.

We archive the emails once the error has been handled.

Installation

pip install python-dotenv

Then copy the script in your SwiftBar Scripts folder.

#!/usr/bin/python
import imaplib
import os
from dotenv import dotenv_values
def _get_unread_emails():
current_path = os.path.dirname(os.path.abspath(__file__))
env_path = os.path.join(current_path, ".env")
env = dotenv_values(env_path)
username = env["EMAIL_ID"]
password = env["PASSWORD"]
# connect to the server
mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail.login(username, password)
# get count in inbox
status, inbox_count = mail.select()
return int(inbox_count[0].decode())
if __name__ == "__main__":
unread_count = _get_unread_emails()
if unread_count:
print(
f"⚡{unread_count} | href=https://mail.google.com/mail/u/0/#inbox"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment