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 11 — Dealing with Labels<br />

171<br />

}<br />

}<br />

print $_->{“sender_email”} . “\t”<br />

. strip_bold( $_->{“subject”} )<br />

. “\n”;<br />

The important section to note here is the code that follows:<br />

if ($label) {<br />

$messages =<br />

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

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

print $_->{“sender_email”} . “\t”<br />

. strip_bold( $_->{“subject”} )<br />

. “\n”;<br />

}<br />

}<br />

By this section of the script, you’ve printed out the labels you know about, and<br />

asked the user to choose one. So now you test to see if the number the user enters<br />

is actually a value option, and if it is, you retrieve all of the messages with the pertinent<br />

label. That’s done, as ever, with the get_messages() function, which can<br />

be modified by passing the name of a label with it:<br />

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

And this returns messages in the same way as you dealt with in Chapter 8.<br />

In Chapter 9, you requested new mail and gave the option to reply to it. Here, in<br />

Listing 11-3, you can do a similar thing: request mail for a certain label and give<br />

the option to reply to it.<br />

Listing 11-3: Retrieving a Labeled Message and Replying<br />

use Utils;<br />

$gmail = login();<br />

@labels = $gmail->get_labels();<br />

$id = 1;<br />

foreach (@labels) {<br />

print $id . “ “ . $_ . “\n”;<br />

$id++;<br />

# simply get all labels<br />

# and iterate through them<br />

Continued

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

Saved successfully!

Ooh no, something went wrong!