Small script to verify gmail accounts

We share a small program written in Python whose objective is to verify the amount of unread emails from an account of gmail. I hope you enjoy it…

Another contribution from Luis López makes him one of the winners of our weekly competition: «Share what you know about Linux«. Congratulations Luis!
Attention: it is necessary to install notify-send for the program to work properly.

In case you want to download it, the source code that is displayed below is available on github: https://gist.github.com/3910908

#! / usr / bin / env python

# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
################################################## #########
# Created by Luis Lopez
# GmailPypy v1.0
# note: You must install notify-send.
################################################## #########


from urllib2 import Request, urlopen
from base64 import encodestring
from re-import search
from os import system

# GMail Credentials
user = "your_usuario@gmail.com"
passwd = "your_password :)"

# Create the request object and add the Authorization header
request = Request ("https://mail.google.com/mail/feed/atom")
base64str = encodestring ("% s:% s"% (user, passwd)). replace ("n", "")
request.add_header ("Authorization", "Basic% s"% base64str)

# Get the GMail athom response
Try:
   response = urlopen (request)
except:
   response = None
   msg = "Unexpected error getting the information. Please check your connection or GMail credentials."

# Get the number of unread Emails
if response is None:
   matched = None
else:
   matched = search (r "(? Pd +)", response.read ())

# Create a message to be displayed
if matched is None or int (matched.group ("unreaded")) == 0:
   msg=None
else:
   msg = "Found" + str (matched.group ("unreaded")) + "unread email (s)"

# Show a Pop-up notification with the message
if msg is None:
   pass
else:
   notify = "notify-send -u normal 'GMailn% s'"% msg
   system (notify)

Don't forget to edit the value of the user and passwd variables with your gmail username and password.

In case you want to add it to crontab:

1.- They open the crontab file of their user (my user is lucho)

crontab -u I fight -e

2.- They edit it by adding a line like this and save

* / 10 * * * * env DISPLAY =: 0.0 /path/to/file/pygmail.py

This will cause the program to run once every 10 minutes, and the pop-up will appear at DISPLAY =: 0.0.

Thank you Luis López!
Do you want to participate in our monthly competition and make a contribution to the community?
You just have to send us a mail including the trick or mini-tutorial of your own.

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   tea square said

    The problem with these things is that the password is stored in plain text: S

  2.   Kevin said

    This Well, a question how can I couple it to my conky?

  3.   Anymex. said

    you can add it to your config file this execi 300 python ~ / .scripts / gmail.py

  4.   Anymex. said

    you can edit the code so that the password and user are encrypted, or you can port it to a compiled language

  5.   Anymex. said

    The script is very good, I use it in my conky