3 minute read

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;


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

=head1 USAGE

$ perl 1 Swaroop C H Looks like works


############## 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("");

my $request = HTTP::Request->new( 'GET' => "", ); $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.


Ankur says:

Your code reminds me of the book "Spidering Hacks" ... old thou ... is filled with many such interesting scripts ...

PizzaDude says:

Did you try it out ?

Philip says:

why don't you just use the pop3 interface?

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.


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

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.

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

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

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

Swaroop says:

@Varun :

I "purchased it".

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

[...] [...]

Umit says:

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

srinirao says:

It doesn't work for me.
srinirao@srinirao:/tmp> perl
Can't locate XML/Atom/ 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 line 26.
BEGIN failed--compilation aborted at line 26.

Swaroop says:

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

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

Swaroop says:

@Umit : Have you tried contacting Google Customer Care ?