Hacking Gmail

(Grace) #1

170 Part III — Conquering Gmail


Listing 11-1: Getting the Existing Labels

use Utils;

$gmail = login();

@labels = $gmail->get_labels(); # simply get all labels
foreach (@labels) { # and iterate through them
print $_. “\n”;
}

Running this will simply print out a list of the labels you are using right now.
That’s useful, but you can extend it a little bit. Listing 11-2 does the same thing,
but allows you to select a label, whereupon it prints all the messages labeled thusly.
Have a look at the listing, and then you’ll walk through the clever bit.

Listing 11-2: Retrieving the Messages from a Certain Label

use Utils;

$gmail = login();

@labels = $gmail->get_labels(); # simply get all labels
$id = 1;
foreach (@labels) { # and iterate through them
print $id. “\t”. $_. “\n”;
$id++;
}

print “\n”;
print “enter label number to retrive labeled messages:\n”;
$num = <>;
print “\n”;

$label = $labels[ $num - 1 ];
if ($label) {
$messages =
$gmail->get_messages( label => $label );
foreach ( @{$messages} ) {
Free download pdf