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.

15 Comments

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

  2. 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.

  3. 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 …

  4. @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.

  5. @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 ;)

  6. @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 :)

  7. Firstly Awesome Wordpres Template !!

    You made it or purchased it ?

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

  8. 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.

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

Comments are closed.