152 Part II — Getting Inside Gmail
Now you have a reference to an array of hashes containing the contents of the
Inbox. You can loop through this array of hashes, and pull out the details of the
messages with the et_indv_emailfunction. This function can either take the
message ID or, as in this case, take the reference to the specific message, like this:
foreach ( @{ $messages } ) {
my $message = $gmail->get_indv_email( msg => $_ );
print “$message->{ $_->{ ‘id’ } }->{ ‘body’ }\n”;
}
Of course, spinning through your Inbox and printing out all of the bodies might
be fun to do once, but it’s not very useful.
Accessing All the Data of a Message
Mail::Webmail::Gmail can, of course, give you all of the information within a
message. However, relying on addressing the data directly within your script is a
recipe for trouble. Even as I type this sentence, the Gmail UI seems to be chang-
ing and rendering bits of Mail::Webmail::Gmail out of date until either Gmail
changes back or the library is fixed. To make sure that your own code isn’t entirely
broken by such changes, do something like this:
foreach ( @{ $messages } ) {
my $message = $gmail->get_indv_email( msg => $_ );
my $to = $message->{ $_->{ ‘id’ } }->{ ‘to’} || “To
irretrievable”;
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”;
print “$to \n $sender_email \n $sent \n $subject \n
$body”;
}
The double pipe at the end of the variable setting lines basically means, “If this call
to the API returns empty, make it this value instead.” This is a simple catch to make
sure that, at least, your script doesn’t just fail on you.