@Snap is right! Screw Python for now and switch to Ruby

First make sure following packages are installed,
ruby
libruby
Gmail by default blocks all non-Google applications from accessing your account. There are two things you can do here to make this script work with Gmail.
First Method (recommended) :-
Generate app password for this script and use that generated password in the script. More information here,
Sign in using App PasswordsSecond Method :-
Change your Goolge account setting to allow non-Google apps access to your account. More information here,
Allowing less secure apps to access your accountNext copy the following script with the name 'check-gmail.rb' somewhere in your $PATH,
#!/usr/bin/ruby
require 'net/imap'
IMAP_USER = "your_gmail_username"
IMAP_PASS = "your_gmail_password"
IMAP_SERVER = "imap.gmail.com"
SSL = true
PORT = 993
IS_EXCHANGE = false
IMAP_FOLDER = "INBOX"
CONKYFIED = true
NEW_COLOR = "green"
SENDER_COLOR = "grey"
SUBJ_COLOR = "white"
def conkify(text,color)
text = "${color #{color}}#{text} $color"
return text
end
def get_messages(conn)
imap = conn
imap.search(["NOT", "DELETED", "UNSEEN"]).each do |message_id|
envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
from, subject = envelope.from[0].name == nil ? envelope.from[0].mailbox : envelope.from[0].name, envelope.subject
CONKYFIED == true ? puts("#{conkify(from,SENDER_COLOR)} $alignr#{conkify(subject,SUBJ_COLOR)}") : puts("#{from} #{subject}")
end
end
imap = Net::IMAP.new(IMAP_SERVER,port = PORT, usessl = SSL)
imap.login(IMAP_USER,IMAP_PASS)
imap.examine(IMAP_FOLDER)
IS_EXCHANGE == true ? counts = imap.search(["NOT", "DELETED", "UNSEEN"]).size : counts = imap.status(IMAP_FOLDER, ["UNSEEN"])["UNSEEN"]
counts > 0 ? ( puts("New Messages: #{CONKYFIED == true ? conkify(counts,NEW_COLOR) : counts}"); get_messages(imap) ) : ( puts("No new messages") )
imap.disconnect
exit
You'll need to modify the following in the script before using it,
IMAP_USER = "your_gmail_username"
IMAP_PASS = "your_gmail_password"
To modify the color output of the script shown in conky, modify the following fields to your liking in the script,
NEW_COLOR = "green"
SENDER_COLOR = "grey"
SUBJ_COLOR = "white"
If you wish to use the script in terminal instead of conky, change the following in the script from,
CONKYFIED = true
to
CONKYFIED = false
Make sure to give the script correct permissions,
chmod 755 /path/to/check-gmail.rb
And call it like following from conky,
execpi 600 /path/to/check-gmail.rb
This is tested okay & satisfaction guaranteed!!!
Cheers!!!