Hacking Gmail

(Grace) #1

164 Part II — Getting Inside Gmail


Listing 10-2 (continued)

foreach ( @{$messages} ) {
if ( $_->{“new”} ) {
print $id. “\t”

. $_->{“sender_email”}. “\t”
. stripbold( $->{“subject”} )
. “\n”;
push( @nums, $num );
$id++;
}
$num++;
}


print “\n”;
print “enter message number to reply to\n”;
$num = <>;
print “\n”;

$message = @{$messages}[ $nums[ $num - 1 ] ];
$msgid = $message->{“id”};
if ($msgid) { # check if message id is OK
print “body:\n”;
$body = <>;
$gmail->send_message(
to => $message->{“sender_email”},
subject => “Re: “. strip_bold( $message->{“subject”}
),
msgbody => $body
); # we are using sender and subject from the
original message
print “message sent\n”;
}

Running this script produces a list of the new messages and gives you the option
to choose one and reply to it. You should see how this works from the code, but
let’s walk through it.

The start is simple enough. You’re using the Utils.pm module you created in
Chapter 7, and you just want to log in. Logging in creates the Gmail object
used in the rest of the script:
use Utils;

$gmail = login();
Free download pdf