fdd76fc5 |
1 | #!/usr/bin/env python3 |
2 | |
3 | """ |
4 | A very simple imap e-mail notifier. |
5 | Tested (and working) with Debian 8 (Jessie) + Xfce. |
6 | """ |
7 | |
8 | import imaplib |
9 | import re |
10 | import time |
11 | import sys |
12 | import os |
13 | import argparse |
14 | |
15 | |
16 | def MainProgram(): |
17 | server_error = 0 |
18 | status = "" |
19 | unread = 0 |
20 | |
21 | parser = argparse.ArgumentParser(description='Check your email.') |
22 | parser.add_argument("-g", "--gmail", help="connect to gmail imap server", action="store_true") |
23 | parser.add_argument("-c", "--custom", help="connect to custom imap server", type=str, default=argparse.SUPPRESS) |
24 | parser.add_argument("-u", "--user", help="Username", type=str, required=True) |
25 | parser.add_argument("-p", "--pword", help="Password", type=str, required=True) |
26 | parser.add_argument("-r", "--port", help="Port to connect to", default="993", type=int) |
27 | parser.add_argument("-d", "--display", help="Display Name", type=str) |
28 | |
29 | args = parser.parse_args() |
30 | |
31 | if args.gmail: |
32 | imapServer = "imap.gmail.com" |
33 | else: |
34 | if args.custom: |
35 | imapServer = args.custom |
36 | |
37 | if args.display: |
38 | displayName = args.display |
39 | else: |
40 | displayName = imapUserName |
41 | |
42 | try: |
43 | imapUserName = args.user |
44 | except Exception as e: |
45 | raise |
46 | |
47 | try: |
48 | imapPass = args.pword |
49 | except Exception as e: |
50 | raise |
51 | |
52 | try: |
53 | imapPort = args.port |
54 | except Exception as e: |
55 | raise |
56 | |
57 | |
58 | # # Enter your imap settings below |
59 | # imapServer = "imap.gmail.com" |
60 | # # imapUserName = "danixland@gmail.com" |
61 | # imapUserName = "it.danilo.macri@gmail.com" |
62 | # # imapPass = "jxibrnebktqbkhmx" |
63 | # imapPass = "udykutvueszpxfkd" |
64 | # imapPort = 993 |
65 | # # displayName = "gmail - danixland" |
66 | # displayName = "gmail - Danilo Macri" |
67 | |
68 | # Try to connect to imap server |
69 | try: |
70 | server = imaplib.IMAP4_SSL(imapServer, imapPort) |
71 | server.login(imapUserName, imapPass) |
72 | status, unread = server.status('INBOX', "(UNSEEN)") |
73 | except Exception: |
74 | pass |
75 | |
76 | if status != "OK": |
77 | server_error = 1 |
78 | |
79 | # Retry a couple of times even though we got wrong response from server |
80 | while server_error >= 1 and server_error < 4: |
81 | time.sleep(2) |
82 | if status == "OK": |
83 | server_error = 0 |
84 | else: |
85 | server_error += 1 |
86 | MainProgram() # Try to connect again |
87 | |
88 | CHECK_UNREAD = str(unread) |
89 | NO_OF_UNREAD = re.sub(r'\D', "", CHECK_UNREAD) |
90 | |
91 | if server_error > 3: |
92 | # os.system('notify-send "Problem connecting to imap server"') |
93 | print("Problem connecting to imap server") |
94 | elif int(NO_OF_UNREAD) > 0: |
95 | # os.system('notify-send "You have new mail"') |
96 | print("/usr/share/icons/MB-Mango-Suru-GLOW/panel/16/new-messages-red.svg") |
97 | print("{}: {} new mail".format(displayName, NO_OF_UNREAD)) |
98 | elif int(NO_OF_UNREAD) == 0: |
99 | print("/usr/share/icons/MB-Mango-Suru-GLOW/panel/16/applications-email-panel.svg") |
100 | print("{}: No new mail".format(displayName)) |
101 | |
102 | |
103 | MainProgram() |