Checking for Mail
N
ow that you’ve been introduced to the Gmail libraries, you
can them to use with a simple script to tell you when you
have new mail. In this chapter, you go through the first
stage of this code in all of the languages and then build on it in
Perl to make a standalone application.
As previously discussed, the APIs upon which this code
is based may cease to work every so often, as Google
changes the way that Gmail operates. If that’s the case,
your knowledge gained in Chapter 5 should help you
help the API’s author to fix things.
The Basics in PHP
Using the Mail::Webmail::Gmail module to check for mail is
simplicity itself. You need to set up the modules and then log in:
use Mail::Webmail::Gmail;
my $gmail = Mail::Webmail::Gmail->new(
username => “ben.hammersley\@gmail.com”,
password => “XXXXXXXX”,
);
After that, retrieve the Inbox and step through the list of mes-
sages in it. Within the Perl library, using the get_messages
method gives you an array of hashes, with the value of ‘new’
being the read/unread flag. So all you need to do is count the
number of messages with a true value in that reference, like so:
̨Checking for new
Reading Mail with Perl.
And Now
̨Instant Messenger
alerts
̨Alerts to your
mobile phone
chapter
in this chapter