Hacking Gmail

(Grace) #1

158 Part II — Getting Inside Gmail


By this point in the script, you have logged in. Now to retrieve the messages in
the Inbox:
my $messages =
$gmail->get_messages( label =>
$Mail::Webmail::Gmail::FOLDERS{‘INBOX’} );

This places the contents of the Inbox into $messagesas a reference to an array of
hashes, which contains the messages within the Inbox. Before looping through
this array and creating an RSS item from each one, first you need to create the
object that creates the RSS feed. Do that with this line:
my $rss = new XML::RSS( version => ‘2.0’ );

Now for the real workings. You have an array where each member is a hash, con-
taining a single message and all its details. To get to these details, you need to be
able to address them with the hash’s key. So, loop through the array, take the name
of the hash, use that as its key, and grab out the values:

foreach ( @{$messages} ) {

my $message = $gmail->get_indv_email( msg => $_ );

my $messageid = $_->{‘id’};

my $sender_email = $message->{ $_->{‘id’} }-
>{‘sender_email’}
|| “Sender_email irretrievable”;

my $sent = $message->{ $_->{‘id’} }->{‘sent’}
|| “To irretrievable”;

my $subject = $message->{ $_->{‘id’} }->{‘subject’}
|| “Subject irretrievable”;

my $body = $message->{ $_->{‘id’} }->{‘body’}
|| “Body irretrievable”;

Noting, again, the double pipe in the statement that gives the variable a value even if
the Mail::Webmail::Gmail module cannot. This protects you a little from Gmail’s
evolution breaking the module and hence your scripts.

Next, create the RSS item for the message:

$rss->add_item(
title => “$subject”,
link =>
“http://gmail.google.com/gmail/h/abcde12345/?th=$messageid&v=c
”,
Free download pdf