plink has asked for the wisdom of the Perl Monks concerning the following question:

hi Monks, I am trying to code a script that permits me to login into my webmail, catch the last top news and then disconnect. I am trying to code using LWP::UserAgent but whitout success. In a few words:
1.)use LWP::UserAgent; 2.)use strict; 3.)my $user = ''; 4.)my $pass = ''; 5.)my $ua = LWP::UserAgent->new(); 6.)my $resp = $ua->get("http://www.webmail.com/login?u=".$user."&passw +ord=".$pass ); 7.)die if !$resp->is_success; #the rest of code here ----------->
Problem seems to be with the code at line 6. I am not able to login, successfully to the site. I suspect the path "http:://www/.." is not the right one. How can i find the right one where to pass all the params necessary to login into the mail? Thanks

Replies are listed 'Best First'.
Re: WebMail authentication
by spazm (Monk) on Aug 02, 2009 at 21:26 UTC
    To find the correct URL to pass your login parameters, you could go look at the page you use to login to your webmail. Then either watch the headers go by or parse the source, your call.

    Personally, I'd use WWW::Mechanize to handle this all automatically.

    Quick plan:

    1. get the url of the page you eventually want within your webmail setup.
    2. logout of webmail, and try opening that page. Do you get sent to a login form? Good. What does the login form look like? A form with the words "LOGIN" or somesuch?
    3. login to the form. Does it take you back to the page your requested? Yes is good. No is ok too.
    4. Now do it again via WWW::Mechanize
    5. ...
    6. profit
    #!/usr/bin/perl -w use strict; use WWW::Mechanize; my $target_url = "https://webmail.com/the/page/I/want"; my $user = "myusername"; my $pass = "<=6chars"; my $ua = WWW::Mechanize->new(); my $resp = $ua->get( $target_url ); #Dear student, check here that resp is valid... #check if page looks like the login page, by looking for a field named + "login" if ( $ua->form_name("login") ) { $ua->field( "username", $user ); #look for a field named "username" $ua->field( "password", $pass ); #look for a field named "password" $ua->submit(); #submit the form } #$ua should now have the comment you were hoping for.
Re: WebMail authentication
by Marshall (Canon) on Aug 02, 2009 at 16:49 UTC
    Most of these things use SSL (secure socket layer) for the communication or at least for the authentication. This can be a hassle to setup and get working. But once you get it going, it works pretty much transparently to you. I looked at one of my LWP gizmos:

    use LWP::UserAgent; use Crypt::SSLeay; use HTTP::Cookies;
    I think you will need this Crypt::SSLeay module.
        Yes. I looked again at the code I snipped from and I don't see any explicit things in it about SSleay. LWP and I presume Mechanize will "just know" about this module and use it if necessary. Of course posts, etc will go to a https URL instead of just http. You may need a cookie_jar also. And some consideration as to who you are going to "mimic". Here I am pretending to be something pretty stupid. This can affect what the server sends you.
        my $ua = LWP::UserAgent->new or die "Problem with the new UserAgent\n" +; $ua->cookie_jar(HTTP::Cookies->new); $ua->agent("Mozilla/4.76 [en] (Windows NT 5.0; U)"); print "And now I'm calling myself ", $ua->agent( ), "!\n";