24.07.2017 Views

Hacking Gmail

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10 — Sending Mail<br />

165<br />

You then grab all of the messages in the Inbox and set up some variables you shall<br />

use to keep track of them:<br />

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

messages<br />

$id = 1;<br />

$num = 0;<br />

@nums;<br />

# simply get all<br />

Then you iterate through these messages, adding them to a list if they are marked<br />

as unread. You print the sender’s address and the subject line of the e-mail, with a<br />

number next to it, pushing that number and the message:<br />

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

if ( $_->{“new”} ) {<br />

print $id . “\t”<br />

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

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

. “\n”;<br />

push( @nums, $num );<br />

$id++;<br />

}<br />

$num++;<br />

}<br />

And then you ask the user to enter the number of the message she wants to<br />

reply to:<br />

print “\n”;<br />

print “enter message number to reply to\n”;<br />

$num = ;<br />

print “\n”;<br />

Finally, you retrieve the sender’s e-mail and subject line from the chosen mail and<br />

request some body text from the user. Once you have that, the message is created<br />

and sent:<br />

$message = @{$messages}[ $nums[ $num - 1 ] ];<br />

$msgid = $message->{“id”};<br />

if ($msgid) { # check if message id is OK<br />

print “body:\n”;<br />

$body = ;<br />

$gmail->send_message(<br />

to => $message->{“sender_email”},<br />

subject => “Re: “ . strip_bold( $message->{“subject”}<br />

),<br />

msgbody => $body

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

Saved successfully!

Ooh no, something went wrong!