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.


Comments

16 comments

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

Gravatar image Ankur
July 31st, 2006, 11:49 am | #

Did you try it out ?

Gravatar image PizzaDude
July 31st, 2006, 3:51 pm | #

why don’t you just use the pop3 interface?

Gravatar image Philip
July 31st, 2006, 8:08 pm | #

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.

Gravatar image Venkatesha Murthy
July 31st, 2006, 8:15 pm | #

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 …

Gravatar image t3rmin4t0r
July 31st, 2006, 9:17 pm | #

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

Gravatar image Swaroop
August 1st, 2006, 6:49 am | #

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

Gravatar image t3rmin4t0r
August 2nd, 2006, 1:06 am | #

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

Gravatar image Swaroop
August 2nd, 2006, 2:06 pm | #

Firstly Awesome Wordpres Template !!

You made it or purchased it ?

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

Gravatar image varun
August 24th, 2006, 8:27 am | #

@Varun :

I “purchased it”.

Gravatar image Swaroop
August 25th, 2006, 7:56 pm | #

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.

Gravatar image Umit
October 12th, 2006, 1:24 am | #

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.

Gravatar image srinirao
October 13th, 2006, 6:05 pm | #

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

Gravatar image Swaroop
October 13th, 2006, 8:04 pm | #

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

Gravatar image Umit
October 14th, 2006, 8:31 pm | #

@Umit : Have you tried contacting Google Customer Care ?

Gravatar image Swaroop
October 14th, 2006, 9:41 pm | #


Leave a Reply

Note: Comments can use Markdown syntax.

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

Subscribe without commenting

About

Swaroop C H is 25 years of age. He graduated in B.E. (Computer Science) from PES Institute of Technology, Bangalore, India. He has previously worked at Yahoo! and Adobe.

More about

Books

  • A Byte of Python

    This book will help you to learn the Python programming language, even if all you know about computers is how to save text files.

    More info

Subscribe

If you want to know when new stories appear on this website, you can subscribe to the RSS feed or have them emailed to you:

RSS

Email

Enter your email address:

Microblogging

Twitter

del.icio.us articles

Waiting for next proto.in!

I'm attending Proto.in 2008 Delhi!

Advertisements

Photos