Use WWW::Mechanize to send email through Yahoo! Mail
In this article, we provide an example on how to use
the
WWW::Mechanize perl package
to send email through a Web-based email provider:
Yahoo! Mail. You can extend it to send email
through
Google Mail too.
Introduction
When you do development with your local box, it doesn't necessarily
have all the required setup for sending email.
The ubiquitous
Sendmail program is not easy to set up. For example
your box may have a dynamic IP address assigned by your ISP and the
recipient's mail server won't accept unauthenticated sender. If you
configure sendmail to use your ISP's email relay server, you are
limited to the email address you sign up with the ISP if there is any.
This limits your flexibility because you may want to send email
from another email address, such as Yahoo! mail or Google mail.
In this case, you want to have a module or a script that your
web site can call to send email through Yahoo! or Google without
human intervention. Following we describe how to implement such
a script with Perl for Yahoo! mail.
Email script implementation in Perl
Most web sites use
cookies
to track user sessions.
With WWW::Mechanize, you just need to know to
load and save cookies without the need to do any parsing.
You first need to create an
WWW::Mechanize
object (referenced as the $agent variable)
and then load a cookie file if it exists. This
helps to avoid signing in unnecessarily and thus to reduce
the time to send an email. This is especially useful
when your script sends many emails a day.
The code in essence looks like this:
$agent = WWW::Mechanize->new();
if (-e $cookie_file) {
$agent->cookie_jar->load($cookie_file);
}
Then you can call $agent->get($ymail_url)
to visit Yahoo! mail's home page. Another nice thing
about WWW::Mechanize is that it handles
almost all the redirects for you automatically. If
you don't have the cookies file or if the cookies
expire, then you will be required to sign into Yahoo!.
You can check whether you are in the Yahoo! login page
or in the Yahoo! Mail overview page by checking the returned
content:
If it has the Yahoo! login URL and contains a
form with the name login_form, then you
need to submit your user name and password through
the $agent. The code looks like the
following.
$agent->form_name("login_form");
$agent->field("login", $username);
$agent->field("passwd", $password);
$agent->submit();
After logging in, you are landing in the Yahoo! Mail's overview page.
The Compose button on this page is actually
the fourth form. Since it doesn't have any name, you have to
ask $agent to select and submit that form. The code looks
like this:
$agent->form_number(4);
$agent->submit();
Now you come to the actual email composition page and you can check details about the form through the Web developer toolbar assuming that you have the Web developer Firefox extension installed. Just choose "Forms" and then "Display Form Details" from the toolbar. Once you know the form's details, you can easily construct the request to send email. The code looks like the following:
$agent->form_name("Compose");
$agent->field("to", $to);
$agent->field("Subj", $subject);
$agent->field("Content", $content);
$agent->click_button(value => "Send");
You can wrap the above code into a function in Perl which accepts a reference to a hash that contains the sender's Yahoo! ID and password, the recipient's email address, subject and body of the email and then send out the email.
There is one more note here. Sometimes even though the cookies expire, Yahoo! may still recognizes the user. In that case, the user name has been prefilled for you and you just need to provide the password. Your script should be able to handle that case.
For your convenience, you can download
the sample script that sends email through
Yahoo! Mail with WWW::Mechanize
and adapt it to your need.
Possible email script implementation in PHP
According to our best knowledge,
it is not easy to write such script in PHP.
It is not just that Perl is more powerful than PHP in scripting
capabilities, it is also that the
WWW::Mechanize
perl package
depends on a lot of many other perl packages that are readily
available from
CPAN, the Comprehensive Perl Archive Network.
In comparison,
the PHP curl extension
is often used to automate sending HTTP requests in the PHP world.
However, it is at a great disadvantage because it mostly implements the
HTTP protocol only and doesn't have native integration with other
PHP modules for HTML parsing such as URL extraction, form handling
and etc. Even if those modules exist, you may still have to evaluate
them, assemble them together and then apply them to your script.
This is not so easy as writing script with
WWW::Mechanize. However,
please do let us know if you know how to do it.
References
-
Kevin Hemenway and Tara Calishain's book "Spidering Hacks":
This book covers how to writes scripts to collect information
automatically from the Internet. It contains a few recipes on
using
WWW::Mechanize.