140 Part II — Getting Inside Gmail
Listing 8-3 gives you a complete script, printing to the screen a count of the new
mail in your account.
Listing 8-3:Checking for New Mail in PHP
<?PHP
require(“libgmailer.php”);
$gm = new GMailer();
$name = “USERNAME”;
$pwd = “PASSWORD”;
$tz = “0”;
$new = 0;
$gm->setLoginInfo($name, $pwd, $tz);
if ($gm->connect()) {
$gm->fetchBox(GM_STANDARD, “Inbox”, 0);
$snapshot = $gm->getSnapshot(GM_STANDARD);
if ($snapshot) {
for ($i = 0;$i < $snapshot->box_total ; $i++ )
{
if ($snapshot->box[$i][“is_read”] == 1)
{ $new++;
}
}
echo “You have”. $new. “new messages”;
}
}
?>
Building on the Basics
Python’s libgmail provides the simplest method to get a new mail count: There’s a
specific function that you can use.
So, as usual, you first need to log in and check for errors there: