Hacking Gmail

(Grace) #1

Chapter 10 — Sending Mail 165


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


use to keep track of them:


$messages = $gmail->get_messages(); # simply get all
messages
$id = 1;
$num = 0;


@nums;


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


as unread. You print the sender’s address and the subject line of the e-mail, with a
number next to it, pushing that number and the message:


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


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


And then you ask the user to enter the number of the message she wants to


reply to:


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


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


request some body text from the user. Once you have that, the message is created
and sent:


$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

Free download pdf