Hacking Gmail

(Grace) #1

Chapter 10 — Sending Mail 163


Listing 10-1: Sending Mail with Perl

use Utils;


$gmail = login();


input data from keyboard


print “to:\n”;
$to = <>;


print “subject:\n”;
$subject = <>;


print “body:\n”;
$body = <>;


$gmail->send_message( to => $to, subject => $subject, msgbody
=> $body ); # and send the message
print “message sent\n”;


That script is, as you can see, remarkably simple. But it does provide the basis for


any number of more complicated scripts. Being able to send mail from a script
isn’t a new thing — it’s pretty easy to do without Gmail — but doing it via Gmail


does give you some advantages. First, it’s easier, but second, the mail is automati-
cally archived. Using Gmail to handle outgoing mail from your applications can


therefore be more resilient, certainly easier, and much more useful than doing it
any other way.


In Chapter 9, you looked at downloading and reading new mail. Listing 10-2
shows a script that combines the techniques you learned there with your new-


found skills at sending mail.


Listing 10-2: Reading Unread Mail and Replying

use Utils;


$gmail = login();


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


Continued
Free download pdf