#!/opt/local/dev/perl/bin/perl -w use strict; use warnings; use WWW::Mechanize; use Crypt::SSLeay; # required for https:// use HTML::Entities; my %ref; $ref{to} = 'recpipent@example.com'; $ref{user} = 'yahoo_username'; $ref{pass} = 'yahoo_userpassword'; $ref{subject} = 'Greetings'; $ref{body} = "This email is sent through WWW::Mechanize."; my $status; $status = send_email(\%ref); if ($status) { print "Email sent out successfully\n"; } else { print "Some problem has happened.\n"; } sub send_email { my ($ref) = (@_); my $agentstr = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12'; my $agent = WWW::Mechanize->new(autocheck => 1, agent => $agentstr); my $mail_server = "http://mail.yahoo.com"; my $user; my $pass; my $cookie_file; $user = $ref->{user}; $cookie_file = "./yahoo_cookie_$user.dat"; $agent->cookie_jar->{ignore_discard} = 1; if (-e $cookie_file) { $agent->cookie_jar->load($cookie_file); } $agent->get($mail_server); if ( ($agent->content =~ /https:\/\/login.yahoo.com\/config\/login/) and ($agent->content =~ /login_form/)) { $agent->form_name("login_form"); unless ($agent->content =~ /$user/) { $agent->field("login", $user); } $agent->field("passwd", $ref->{pass}); $agent->submit(); my $mail_url; if ($agent->content =~ /url=([^"]+)/s) { $mail_url = $1; } if (!$mail_url) { return 0; # Cannot login } $agent->cookie_jar->save($cookie_file); $agent->get($mail_url); } $agent->form_number(4); $agent->submit(); $agent->form_name("Compose"); $agent->field("to", $ref->{to}); $agent->field("Subj", $ref->{subject}); $agent->field("Content", $ref->{body}); $agent->click_button(value => "Send"); return 1; }