I use the following function that I wrote to log into yahoo mail and retrieve the number of messages waiting:
sub loginyahoomail( $ $ )
{
# arguments
my ( $user, $password ) = @_;
my $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
my $mech = WWW::Mechanize->new( agent => $agent,
quiet => 1,
onwarn => \&warnfunc,
onerror => \&warnfunc,
);
$mech->get( "https://mail.yahoo.com/" );
return( undef ) if ( ! $mech->success() );
$debug && print( STDERR "-At->" . $mech->uri() . "\n" );
if ( $mech->response->content =~ m/Yahoo! Mail.*$user/ )
{
$mech->follow_link( text => 'Sign Out' );
$debug && print( STDERR "-At->" . $mech->uri() . "\n" );
}
return( undef ) if ( ! $mech->success() );
if ( $mech->response->content =~ m/Sign out completely/i )
{
$mech->follow_link( text => 'Sign out completely' );
$debug && print( STDERR "-At->" . $mech->uri() . "\n" );
}
return( undef ) if ( ! $mech->success() );
my $signinasdiff = 'Sign in as a different user';
if ( $mech->response->content =~ m/$signinasdiff/i )
{
$mech->follow_link( text => 'Sign in as a different user' );
$debug && print( STDERR "-At->" . $mech->uri() . "\n" );
}
return( undef ) if ( ! $mech->success() );
if ( $mech->response->content =~ m/Return to Yahoo! Mail/ )
{
$mech->follow_link( text => 'Return to Yahoo! Mail' );
$debug && print( STDERR "-At->" . $mech->uri() . "\n" );
}
return( undef ) if ( ! $mech->success() );
$mech->get( "https://mail.yahoo.com/" );
return( undef ) if ( ! $mech->success() );
if ( $mech->response->content =~ m/login_form/ )
{
$mech->form_name( 'login_form' );
$mech->set_fields( login => $user,
passwd => $password );
$mech->submit();
}
return( undef ) if ( ! $mech->success() );
$debug && print( STDERR "-At->" . $mech->uri() . "\n" );
if ( $mech->response->content =~
m/http-equiv.*refresh.*url=[\'\"]?([^\'\">]+)/i )
{
$mech->get( $1 );
$debug && print( STDERR "-At->" . $mech->uri() . "\n" );
}
return( undef ) if ( ! $mech->success() );
if ( $mech->response->content =~
m/gWelcomePage\s*=\s*[\"]?([^\r\n\" ]+)/i ) {
$mech->get( $1 );
$debug && print( STDERR "-At->" . $mech->uri() . "\n" );
}
return( undef ) if ( ! $mech->success() );
if ( $mech->response->content =~
m/You have (?:<b>)?(\d+)\s*unread/i )
{
print( STDERR "$1 messages waiting\n" );
}
elsif ( $mech->response->content =~
m/>Inbox(\s*\((\d+)\))?</ )
{
my $count = $2 || 0;
print( STDERR "$count messages waiting\n" );
}
# sign out like the good boys we are
$mech->follow_link( text => 'Sign Out' );
return( undef ) if ( ! $mech->success() );
if ( $mech->response->content =~
m/(You have successfully closed|You have signed out)/ )
{
print( STDERR "Signed out\n" );
}
return undef;
}