Torrential Web Dev

Gmail contact list grabber

Gmail contact grabber

Compared to the MSN and AIM contact grabbing classes interacting with Gmail was an absolute pleasure thanks to the hard work of at least two people truly deserving of thanks.

Y. H. Gan and Neerav Modi have produced an immensely powerful script for interacting with Gmail. It does far more than just grabbing the contact list. Using it feels a lot like using a swiss army knife or maybe one of those swanky Leathermans when a screwdriver will do just fine.

Grab the Script

The libgmailer script you'll need can be downloaded from sourceforge. Grab that and then return for instructions on how to grab the contact list.

Grabbing the contact list

The code you need to retrieve the Gmail contact list with this script is as follows.

<?php
require_once("libgmailer.php");
   
$gmail_acc = "username";
$gmail_pwd = "acc_pass";
$my_timezone = 0;
 
$gmailer = new GMailer();
if ($gmailer->created) {
$gmailer->setLoginInfo($gmail_acc, $gmail_pwd, $my_timezone);

//uncomment if you need it
//$gmailer->setProxy("proxy.company.com");

if ($gmailer->connect()) {
// GMailer connected to Gmail successfully.
// Do something with it.

//Get the contacts
// For "Inbox"

$gmailer->fetchBox(GM_CONTACT, "all", "");

$snapshot = $gmailer->getSnapshot(GM_CONTACT);


//Outputs an array of the contacts
var_dump($snapshot->contacts);

//Outputs the number of contacts
var_dump($snapshot->contacts_total);



} else {
die("Fail to connect because: ".$gmailer->lastActionStatus());
}
} else {
die("Failed to create GMailer because: ".$gmailer->lastActionStatus());
}

?>

That's it! It's a bit more complex than for some of the other scripts but that's what happens when you use such a powerful package for something simple. The only drawback with this script is that it requires CURL but I think most hosts have this installed so it's unlikely to be a problem.

If you do have any problems though or want to give any other sort of feedback please post it in the comments of this thread.