summaryrefslogtreecommitdiff
path: root/count_email.py
blob: 6b519710ed2ebe52873c1bb1c21a12bccca7aa54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python

# A small script to pull unread email counts from servers in .authinfo

from imaplib import IMAP4_SSL
from subprocess import check_output

# Server
servers=["posteo","gmail"]
result = []
# Decrypt the file, split the lines
data = check_output(["gpg","--decrypt",".authinfo.gpg"]).decode('utf-8').split('\n')
# Collect results
for server in servers:
    # Get password and username
    line = [l for l in data if ("imap" in l and server in l)][0].split()
    host,user,password = line[1], line[3], line[5]
    # Login
    imap = IMAP4_SSL(host)
    imap.login(user,password)
    imap.select("INBOX", 1)
    # Count emails
    _, res = imap.search(None, "UNSEEN")
    count = len(res[0].split())
    imap.logout()
    # Store count if there's something
    if count>0: result.append(user+": "+str(count))
# What did we find
print(", ".join(result))