Hacking Gmail

(Grace) #1

Chapter 9 — Reading Mail 155


attachments - if we need them as well - we need to use
get_attachment method)
print “sender: “. $full_message->{$id}->{“sender”}.
“\n”;
print “sent: “. $full_message->{$id}->{“sent”}. “\n”;
print “to: “. $full_message->{$id}->{“to”}. “\n”;
print “subject: “. strip_bold($full_message->{$id}-



{“subject”}). “\n”;
print $full_message->{$id}->{“body”}. “\n\n”;
}



Now, as you can see from the in-code comments, this code can’t deal with attach-


ments. It’s time you learned how. Oh. Look...


Dealing with Attachments.


Gmail’s enormous storage capacity gives you the opportunity to use it for very


large attachments. There are many possibilities for this feature, but first you need
to know how to retrieve the attachments themselves.


You retrieve an attachment in a way very closely connected to the method you
used in the RSS script in Listing 9-1. First, retrieve the list of messages and then


loop through them, pulling out the data on each message. Here you differ —
you’re looking for an attachment, so you test to see if one is present, and if so you


go on to do something about it. The first part of a script after logging in, there-
fore, is:


my $messages = $gmail->get_messages();


foreach ( @{$messages} ) {
my $email = $gmail->get_indvemail( msg => $ );
if ( defined( $email->{ $->{‘id’} }->{‘attachments’} ) )
{
foreach ( @{ $email->{ $
->{‘id’} }->{‘attachments’} }
) {


# Here do something with each attachment

}
}
}


Making an RSS Feed of Your Inbox


So now you know how to gather the mail from a specific folder and print it out.
Let’s do something more useful with it, as an exercise. How about an RSS feed of

Free download pdf