Hacking Gmail

(Grace) #1

Chapter 8 — Checking for Mail 139


Listing 8-2:An Even Easier Way to Check Mail

use Utils;


$gmail = login();


$messages = $gmail->getmessages(); # simply get all
messages
$count = 0;
foreach ( @{$messages} ) { # and iterate through
them
if ( $
->{“new”} ) { # if message is new
$count++;
}
}
print “Number of unread messages: “. $count. “\n”;


This uses the Utils module you created in Chapter 7 — Listing 7-4 to be precise.


That module encapsulates the login process into one simple login()function,


allowing the script to be even simpler than before.


The Basics in Python


PHP, too, provides a simple interface to check for new mail in Gmail. The libg-


mailler library, as you saw in Chapter 6, handles it perfectly well. First, you need
to log in:


$gm->setLoginInfo($name, $pwd, $tz);


if ($gm->connect()) {


Then you fetch the Inbox and create the Snapshotobject:


$gm->fetchBox(GM_STANDARD, “Inbox”, 0);


$snapshot = $gm->getSnapshot(GM_STANDARD);


After that, loop through all of the messages in the Inbox, incrementing a variable


by one for every unread mail you see:


if ($snapshot) {
for ($i = 0;$i < $snapshot->box_total ; $i++ )
{
if ($snapshot->box[$i][“is_read”] == 1)
{ $new++;
}
}

Free download pdf