24.07.2017 Views

Hacking Gmail

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

138 Part II — Getting Inside <strong>Gmail</strong><br />

my $new_msgs = 0;<br />

if ( defined($messages) ) {<br />

foreach ( @{$messages} ) {<br />

if ( $_->{‘new’} ) {<br />

$new_msgs++;<br />

}<br />

}<br />

}<br />

This leaves you with the variable $new_msgs to give you the number of unread<br />

messages in the Inbox. Listing 8-1 gives an entire working script to display this.<br />

Listing 8-1: Checking the New Mail Count in Perl<br />

#!/usr/bin/perl<br />

use warnings;<br />

use strict;<br />

use Mail::Webmail::<strong>Gmail</strong>;<br />

my $gmail = Mail::Webmail::<strong>Gmail</strong>->new(<br />

username => “ben.hammersley\@gmail.com”,<br />

password => “XXXXXXXX”,<br />

);<br />

my $messages =<br />

$gmail->get_messages( label =><br />

$Mail::Webmail::<strong>Gmail</strong>::FOLDERS{‘INBOX’} );<br />

my $new_msgs = 0;<br />

if ( defined($messages) ) {<br />

foreach ( @{$messages} ) {<br />

if ( $_->{‘new’} ) {<br />

$new_msgs++;<br />

}<br />

}<br />

}<br />

print “you have $new_msgs new messages in your inbox\n”;<br />

Obviously, from here you can build out to produce all sorts of interesting alerts, as<br />

you shall do later on in this chapter.<br />

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!