Hacking Gmail

(Grace) #1

162 Part II — Getting Inside Gmail


One other advantage of using the Gmail SMTP client is that any mail sent via the
SMTP gateway is automatically stored within your Gmail account.

Using the SMTP Server Programmatically
If you want to talk directly to the SMTP server instead of using the APIs featured
in the rest of this chapter, then you will need to use a library that can deal with
TLS encryption. There is no standard module to do this within Perl or PHP at
the time of this writing, but Python users can use the standard smtplib, which
comes with the Python distribution.

Chapter 10: Sending Mail


The Mail::Webmail::Gmail module encapsulates mail sending in one single func-
tion,send_message. The basic method to send a message is:
$gmail->send_message(
to => ‘[email protected]’,
subject => ‘Test Message’,
msgbody => ‘This is a test.’
);

To send to multiple addresses, you can use an arrayref containing all of the
addresses:
my $email_addrs = [
[email protected]’,
[email protected]’,
[email protected]’, ];
$gmail->send_message(
to => $email_addrs,
subject => ‘Test Message’,
msgbody => ‘This is a test.’
);

You may also send mail using cc: and bcc:
$gmail->send_message(
to => $email_addrs,
cc=> $cc_email_addrs,
subject => ‘Test Message’,
msgbody => ‘This is a test.’
);

Listing 10-1 shows a small script, using the Mail::Webmail::Gmail module and
the Utils.pm code introduced in Chapter 7. It takes input from the keyboard,
and sends the mail directly. It’s exceptionally easy to understand, so no walk-
through is necessary.
Free download pdf