check_gmail

I’ts amazing what you can whip up in just 15 minutes using CPAN (including reading the documentation).

!/usr/bin/env perl

use warnings; use strict;

=head1 INTRODUCTION

Checks if there are new unread messages in your GMail Inbox.

=head1 USAGE

$ perl check_gmail.pl
1       Swaroop C H     Looks like check_gmail.pl works

=cut

######## Configuration

Change this to your correct username.

use constant GMAIL_USERNAME => “username”;

Change this to your correct password.

use constant GMAIL_PASSWORD => “password”;

#### Don’t change anything below this.

use LWP::UserAgent; use XML::Atom::Feed;

my $fetcher = LWP::UserAgent->new(); $fetcher->agent(”check_gmail.pl/0.01″);

my $request = HTTP::Request->new( ‘GET’ => “https://mail.google.com/gmail/feed/atom”, ); $request->authorization_basic(GMAIL_USERNAME, GMAIL_PASSWORD);

my $response = $fetcher->request($request);

if (! $response->is_success()) { die(”Unsuccessful in trying to talk to GMail”); }

my $content = $response->content; my $feed = XML::Atom::Feed->new(\$content); my @new_messages = $feed->entries();

my $i = 1; foreach my $message(@new_messages) { print join(”\t”, $i, $message->author->name, $message->title), “\n”; $i++; }

The End



Update : Baishampayan Ghose quickly jotted down a Python version of this script.

16 Responses to “check_gmail”

  1. Ankur Says:

    Your code reminds me of the book “Spidering Hacks” … old thou … is filled with many such interesting scripts …

  2. PizzaDude Says:

    Did you try it out ?

  3. Philip Says:

    why don’t you just use the pop3 interface?

  4. Venkatesha Murthy Says:

    Useful script; nice work! But I’d be wary of hardcoding my username and password into the script. Besides, I wouldn’t need one script for each GMail account (yes, I have more than one). Also, you can’t deploy this script for all users on your machine.

    You might want to have the script look in your home directory for a read-only-by-owner file for the username and passwords or take the filename as an option.

    V.

  5. t3rmin4t0r Says:

    HTTP Basic Auth is a really really bad idea. Also if you were really into perl, you should’ve

    use GMail::Checker;

    my $gwrapper = new GMail::Checker(USERNAME => “username”, PASSWORD => “password”);

    Anything’s that worth doing …

  6. Swaroop Says:

    @Ankur : Okay

    @PizzaDude : Yes, it works

    @Philip : It was a situation where I didn’t have access to a POP3 client.

    @Venkatesha : Yeah yeah, I did all that (reading from ~/.gmailrc, etc.) in the script that I actually ended up using. What I pasted here was just the basic part.

    @Gopal : Sorry, I’m ignorant here. Is HTTP Basic auth insecure? How is it different from POP3 auth?

    I think I tried GMail::Checker but it didn’t work for me, I remember trying WWW::GMail also.

  7. t3rmin4t0r Says:
    @Gopal : Sorry, I’m ignorant here. Is HTTP Basic auth insecure? How is it different from POP3 auth

    Basic HTTP auth sends your password something like this

    GET / HTTP/1.1 Authorization: Basic dXNlcjpwYXNzd29yZA==

    >>> import base64
    >>> base64.decodestring("dXNlcjpwYXNzd29yZA==") 'user:password'
    

    But I just noticed that feed was on an https:// SSL connection, so there’s really no problem with man in the middle.

    PS: I’m just itching for the day when ‘you know what’ puts up the SOAP api on developer.yahoo.net ;)

  8. Swaroop Says:

    @t3rmin4tor : Okay, basically, there’s no security in basic auth, but at least SSL takes care that no one intercepts it, hmm….

    Yeah, so true, I had fun playing with ‘you know what’ too :)

  9. varun Says:

    Firstly Awesome Wordpres Template !!

    You made it or purchased it ?

    And the script is a cool piece of code.. gonna test it sometime

  10. Swaroop Says:

    @Varun :

    I “purchased it”.

  11. Its My Life …. » Python script to check gmail from ur console !!! Says:

    [...] http://www.swaroopch.info/archives/2006/07/31/check_gmail/ [...]

  12. Umit Says:

    Guys, My gf has access to my gmail. She has changed my passowrd n secret Q. Can u help me out. She mite be accessing it. Mail me at Umitkulshrestha@gmail.com.

  13. srinirao Says:

    It doesn’t work for me. srinirao@srinirao:/tmp> perl gmail.pl Can’t locate XML/Atom/Feed.pm in @INC (@INC contains: /usr/lib/perl5/5.8.6/i586-linux-thread-multi /usr/lib/perl5/5.8.6 /usr/lib/perl5/siteperl/5.8.6/i586-linux-thread-multi /usr/lib/perl5/siteperl/5.8.6 /usr/lib/perl5/siteperl /usr/lib/perl5/vendorperl/5.8.6/i586-linux-thread-multi /usr/lib/perl5/vendorperl/5.8.6 /usr/lib/perl5/vendorperl .) at gmail.pl line 26. BEGIN failed–compilation aborted at gmail.pl line 26.

  14. Swaroop Says:

    @Srinirao : Isn’t it obvious that the module must be installed?

  15. Umit Says:

    can someone help me access my gmail account. i am blocked. have tried the conventional means…dooesnt help..i dont know hacking. pl help

  16. Swaroop Says:

    @Umit : Have you tried contacting Google Customer Care ?

Leave a Reply


Policy on Comments : All comments will be moderated. Any off-topic comments will be deleted without notice.