Simple IMAP Mail Checker

Checks IMAP mail server and displays inbox count

Image preview of Simple IMAP Mail Checker plugin.

simple-mail-checker.1m.py

Edit
Open on GitHub
#!/usr/bin/env PYTHONIOENCODING=UTF-8 python3
#
# <xbar.title>Simple IMAP Mail Checker</xbar.title>
# <xbar.version>v0.1</xbar.version>
# <xbar.author>Ashley Anderson</xbar.author>
# <xbar.author.github>albinoloverats</xbar.author.github>
# <xbar.desc>Checks IMAP mail server and displays inbox count</xbar.desc>
# <xbar.image>https://user-images.githubusercontent.com/5545555/236855546-85c58709-498c-4749-b52f-0278ddca8b4d.png</xbar.image>
# <xbar.dependencies>python</xbar.dependencies>

import imaplib

# Add any email accounts here

accounts = [
		{
			"friendly": "Example 1",
			"hostname": "mail.example.com",
			"username": "[email protected]",
			"password": "huner2",
			"messages": 0
		},
		{
			"friendly": "Example 2",
			"hostname": "mail.example.org",
			"username": "another-user",
			"password": "correcthorsebatterystaple",
			"messages": 0
		}
	]

# Do not change anything below

ICON="| dropdown=false templateImage=JVBERi0xLjYNJeLjz9MNCjEwIDAgb2JqDTw8L0ZpbHRlci9GbGF0ZURlY29kZS9GaXJzdCA5L0xlbmd0aCAxNzYvTiAyL1R5cGUvT2JqU3RtPj5zdHJlYW0NCmjehE9BCoNADPxKXmDWWm0PslCleCgFUW/iYamhCNbI7gr29921Z9tDApmZTDIRCIghiSBNMedlshDibehNGzui6vBO/aAyXlsRCPB18C08BqLD5j0TlupJRkq3ftF2R5iNRP0Ol/NkabIGEn8Qc83zjvLXK6XSzgSizaMiw4t+kNkyjazrWT3ID7WAs5dIidfVFrVV1uOFw09f3DEVexgENnp4/Ysu5UeAAQDvyVbwDWVuZHN0cmVhbQ1lbmRvYmoNMSAwIG9iag08PC9NZXRhZGF0YSAyIDAgUi9QYWdlcyAzIDAgUi9UeXBlL0NhdGFsb2c+Pg1lbmRvYmoNMiAwIG9iag08PC9MZW5ndGggMzUwOC9TdWJ0eXBlL1hNTC9UeXBlL01ldGFkYXRhPj5zdHJ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xyZGY6QWx0PgogICAgICAgICAgICAgICA8cmRmOmxpIHhtbDpsYW5nPSJ4LWRlZmF1bHQiPjI8L3JkZjpsaT4KICAgICAgICAgICAgPC9yZGY6QWx0PgogICAgICAgICA8L2RjOnRpdGxlPgogICAgICA8L3JkZjpEZXNjcmlwdGlvbj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iPgogICAgICAgICA8eG1wTU06RG9jdW1lbnRJRD51dWlkOjgzYmQ3M2JlLTY3ZjAtNmY0OC05NzU5LWY3OTFjZjEyNGFkOTwveG1wTU06RG9jdW1lbnRJRD4KICAgICAgICAgPHhtcE1NOkluc3RhbmNlSUQ+dXVpZDozNzU5ZjM2YS04Mjc1LWQ3NDktOThlNi1jOWZmODgxNzc3Njc8L3htcE1NOkluc3RhbmNlSUQ+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczpwZGY9Imh0dHA6Ly9ucy5hZG9iZS5jb20vcGRmLzEuMy8iPgogICAgICAgICA8cGRmOlByb2R1Y2VyPkFkb2JlIFBERiBsaWJyYXJ5IDEwLjAxPC9wZGY6UHJvZHVjZXI+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgCjw/eHBhY2tldCBlbmQ9InciPz4NZW5kc3RyZWFtDWVuZG9iag00IDAgb2JqDTw8L0NyZWF0aW9uRGF0ZShEOjIwMTIwOTA5MTQwNzA0LTA0JzAwJykvQ3JlYXRvcihBZG9iZSBJbGx1c3RyYXRvciBDUzYgXChNYWNpbnRvc2hcKSkvTW9kRGF0ZShEOjIwMTIwOTA5MTQwNzA0LTA0JzAwJykvUHJvZHVjZXIoQWRvYmUg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i9XWzEgMiAxXT4+c3RyZWFtDQpo3mJiAAImRkZZBiYGBhsg6/9GBqb//P5A1seNjEz/BZj+AyWuAWUZC0BKFIHE+7lAQnoXA0CAAQAoXgr/DWVuZHN0cmVhbQ1lbmRvYmoNc3RhcnR4cmVmDTcxMTQNJSVFT0YN"

def count_messages(account):
	imap = None
	try:
		imap = imaplib.IMAP4_SSL(account["hostname"])
		imap.login(account["username"], account["password"])
		imap.select("Inbox")
		typ, messages = imap.search(None, "UNSEEN")
		if len(messages) > 1:
			account["messages"] = len(messages)
		elif len(messages[0]) > 0:
			account["messages"] = 1
	except:
		return
	finally:
		if imap is not None:
			imap.close()

total = 0
for account in accounts:
	count_messages(account)
	total = total + account["messages"]

if total > 0:
	print("%d %s" % (total, ICON))
else:
	print(ICON)

print("---")

for account in accounts:
	print("%d %s" % (account["messages"], account["friendly"]))