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

hey perl monks, I am trying to login to a webmail server using the perl LWP::User Agent. This works well but before I can access the INBOX I am redirected back to the login page. Here is what I have so far:

use strict; use LWP; use LWP::Debug qw(+); my $url = 'https://www.a-university.edu/horde/imp/'; my $url_redi = $url . "redirect.php"; # there is a problem with the server's certificate, # but this is not the issue here my $ua = LWP::UserAgent->new( ssl_opts => { SSL_verify_mode => 'SSL_VERIFY_NONE'}); my $form = $ua->get($url_redi) or die "couldn't fetch $url_redi"; $form->is_success() or die $form->message(); # extract cookie my $formcont = $form->content(); my $sessionid; if ($formcont =~ m/Horde=(.*?)" method/s) { $sessionid = $1; } # submit user name and password # all other items required as shown by the web page source code my $response = $ua->post( $url . "?Horde=" . $sessionid, [ 'imapuser' => 'jobro', 'pass' => 'verysecret', 'actionID' => '105', 'url' => '', 'mailbox' => 'INBOX', 'server' => 'hardy.a-university.edu', 'port' => '143', 'namespace' => '', 'maildomain' => 'a-university.edu', 'protocol' => 'imap/notls', 'realm' => 'a-university.edu', 'folders' => 'mail/', 'new_lang' => 'en_US', 'button' => 'Log in' ] ); # at his point the # print $response->as_string(); # is as expected # now the server sends (as part of the http header) # a location for redirection # (I know that I can follow redirections automatically, but this # leads to the same problem) # so I read off the address and try to get this page my $inbox = $ua->get( $url . "mailbox.php?Horde=" . $sessionid, [ 'actionID' => '149', 'mailbox' => 'INBOX' ] ); # but now I am back to the login page :(

(I asked the same question on stack overflow, but no result)

Replies are listed 'Best First'.
Re: perl LWP::User Agent credentials ignored on redirect
by Corion (Patriarch) on Dec 20, 2015 at 10:08 UTC

    You need to be using cookies, like ikegami already told you on Stackoverflow.

    Maybe you want to use WWW::Mechanize instead, which adds many convenience features to LWP::UserAgent?

    Also, have you investigated whether there also is IMAP or POP3 access to that mail server? There are dedicated protocols for transferring mail between computers so scraping a web interface to mail seems a bit convoluted.

      thanks for your reply, Corion, via POP3 I can do it, but wanted to learn LWP and relatives. For the GET request I though it was the right way of using the cookie, but apparently it's not. So next I will look into WWW::Mechanize.
Re: perl LWP::User Agent credentials ignored on redirect
by beech (Parson) on Dec 20, 2015 at 09:52 UTC