Hacking Gmail

(Grace) #1

138 Part II — Getting Inside Gmail


my $new_msgs = 0;

if ( defined($messages) ) {
foreach ( @{$messages} ) {
if ( $_->{‘new’} ) {
$new_msgs++;
}
}
}

This leaves you with the variable $new_msgsto give you the number of unread
messages in the Inbox. Listing 8-1 gives an entire working script to display this.

New Mail Count in RSS


#!/usr/bin/perl

use warnings;
use strict;
use Mail::Webmail::Gmail;

my $gmail = Mail::Webmail::Gmail->new(
username => “ben.hammersley\@gmail.com”,
password => “XXXXXXXX”,
);

my $messages =
$gmail->get_messages( label =>
$Mail::Webmail::Gmail::FOLDERS{‘INBOX’} );

my $new_msgs = 0;

if ( defined($messages) ) {
foreach ( @{$messages} ) {
if ( $_->{‘new’} ) {
$new_msgs++;
}
}
}

print “you have $new_msgs new messages in your inbox\n”;

Obviously, from here you can build out to produce all sorts of interesting alerts, as
you shall do later on in this chapter.

An alternative and easier way of doing this can be found in Listing 8-2.
Free download pdf