24.07.2017 Views

Hacking Gmail

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 — Reading Mail<br />

155<br />

attachments - if we need them as well - we need to use<br />

get_attachment method)<br />

print “sender: “ . $full_message->{$id}->{“sender”} .<br />

“\n”;<br />

print “sent: “ . $full_message->{$id}->{“sent”} . “\n”;<br />

print “to: “ . $full_message->{$id}->{“to”} . “\n”;<br />

print “subject: “ . strip_bold($full_message->{$id}-<br />

>{“subject”}) . “\n”;<br />

print $full_message->{$id}->{“body”} . “\n\n”;<br />

}<br />

Now, as you can see from the in-code comments, this code can’t deal with attachments.<br />

It’s time you learned how. Oh. Look . . .<br />

Dealing with Attachments<br />

<strong>Gmail</strong>’s enormous storage capacity gives you the opportunity to use it for very<br />

large attachments. There are many possibilities for this feature, but first you need<br />

to know how to retrieve the attachments themselves.<br />

You retrieve an attachment in a way very closely connected to the method you<br />

used in the RSS script in Listing 9-1. First, retrieve the list of messages and then<br />

loop through them, pulling out the data on each message. Here you differ —<br />

you’re looking for an attachment, so you test to see if one is present, and if so you<br />

go on to do something about it. The first part of a script after logging in, therefore,<br />

is:<br />

my $messages = $gmail->get_messages();<br />

foreach ( @{$messages} ) {<br />

my $email = $gmail->get_indv_email( msg => $_ );<br />

if ( defined( $email->{ $_->{‘id’} }->{‘attachments’} ) )<br />

{<br />

foreach ( @{ $email->{ $_->{‘id’} }->{‘attachments’} }<br />

) {<br />

# Here do something with each attachment<br />

}<br />

}<br />

}<br />

Making an RSS Feed of Your Inbox<br />

So now you know how to gather the mail from a specific folder and print it out.<br />

Let’s do something more useful with it, as an exercise. How about an RSS feed of

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!