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 …
July 31st, 2006, 11:49 am | #
Did you try it out ?
July 31st, 2006, 3:51 pm | #
why don’t you just use the pop3 interface?
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.
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 …
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.
August 1st, 2006, 6:49 am | #
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
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
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
August 24th, 2006, 8:27 am | #
@Varun :
I “purchased it”.
August 25th, 2006, 7:56 pm | #
[…] http://www.swaroopch.info/archives/2006/07/31/check_gmail/ […]
August 31st, 2006, 9:24 am | #
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.
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.
October 13th, 2006, 6:05 pm | #
@Srinirao : Isn’t it obvious that the module must be installed?
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
October 14th, 2006, 8:31 pm | #
@Umit : Have you tried contacting Google Customer Care ?
October 14th, 2006, 9:41 pm | #
Leave a Reply