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

I want to use the Mail::POP3Client module to connect to an email server and check for and retrieve mail using a perl script. I have tried many many sample scripts for connecting and retrieving email, however my current problem is that I cannot connect to a mail server. I have tried using my email server, as well as creating a gmail account, and neither have worked. Can anyone offer suggestions of what email host they have used that has worked for this module? And possibly the code you used (although I think the code I have will work if I just have the right email program that will let me connect using POP)? Thanks.

Replies are listed 'Best First'.
Re: Mail::POP3Client email server to use
by socketdave (Curate) on Jul 14, 2005 at 19:45 UTC
    Could you post your code and the output you're receiving?

    UPDATE:

    This works for me (straight from the docs):
    #!/usr/bin/perl use Mail::POP3Client; $pop = new Mail::POP3Client( USER => "user", PASSWORD => "pass", HOST => "server.domain.tld" ); for ( $i = 1 ; $i <= $pop->Count() ; $i++ ) { foreach ( $pop->Head($i) ) { /^(From|Subject):\s+/i and print $_, "\n"; } print "\n"; }
      Thanks for the replies. Here is the code I am using. I'm not too familiar with Perl so I really have no idea where I am going wrong. The output I get is just a warning about "main::trigger" only being used once. I've tried using the sample code you've posted and it doesn't work. I try to use the print $pop->Count(); in that sample code and it spits out -1, meaning it's not seeing any email. Any further suggestions? Thanks for the help.
      $continue = "false"; until ($continue = "true") { use Mail::POP3Client; $pop = new Mail::POP3Client( USER => "me", PASSWORD => "password", HOST => "pop.google.com", AUTH_MODE => "PASS", DEBUG => 1 ); $MailState = $pop->State; if($MailState eq 'AUTHORIZATION') { die "\n\nBad user name or password!\n" } elsif($MailState eq 'DEAD') { die "\n\nMail server unreachable or unavailable!\n" } $messages = $pop->Count(); print $messages; if ($messages > 0) { # Read mail headers and get useful info for ($m = 1; $m<=$pop->Count; $m++) { $Headers = $pop->Head($m); if($Headers =~ /(\bReturn-Path.*)/g){ $RTNP = $1}else{ $RTNP = 'Return-Path:'} if($Headers =~ /(\bDate.*)/g){ $Date = $1}else{ $Date = 'Date:'} if($Headers =~ /(\bFrom.*)/g){ $From = $1}else{$From = 'From:'} if($Headers =~ /(\bTo.*)/g){ $To = $1}else{ $To = 'To:'} if($Headers =~ /(\bSubject.*)/g){ $Subj = $1}else{ $Subj = 'Subject:'} # Now Print it print "Message: ", $m, "\n", #$Date,"\n", #$To,"\n", #$From,"\n", #$RTNP, "\n", $Subj,"\n\n"; if ($Subj =~ /\bswift\sburst\alert.*/i) { $trigger = $m; $continue = "true"; } } } } #Look at the body of that email and do whatever with it... #$Headers = $mail->Head($trigger); #@_ = split(/\n/, $Headers); #$Body = $mail->Body($trigger); #print "\n\nMessage: ", $trigger, "\n\n"; #print $Body; #$pop->Close();

      Edit g0n - added code tags

        First, you should probably edit your code to remove the username and pass (unless this is an account set up specifically for testing this script).

        It seems to me that you should move your initialization of your pop object to outside the until loop:
        use Mail::POP3Client; $pop = new Mail::POP3Client( USER => "username", PASSWORD => "password", HOST => "server.domain.tld", AUTH_MODE => "PASS", DEBUG => 1 ); $MailState = $pop->State; if ( $MailState eq 'AUTHORIZATION' ) { die "\n\nBad user name or password!\n"; } elsif ( $MailState eq 'DEAD' ) { die "\n\nMail server unreachable or unavailable!\n"; } $continue = "false"; until ( $continue = "true" ) { ...


        This won't get your whole script working, but with this change it does connect and check my messages.

        Hope this helps...
Re: Mail::POP3Client email server to use
by atcroft (Abbot) on Jul 14, 2005 at 19:25 UTC

    Two things you may wish to try, to determine the cause of the issue. First of all, turn on debugging (by adding the DEBUG => 1 parameter to the new() function call), and two, check the AUTH_MODE your mail server uses. In attempting to use the first sample from the Mail::POP3Client docs, I too could not log in. When I turned on debugging, I was able to see that it was attempting to use APOP against a server that was not speaking it, and was thus failing. Setting 'AUTH_MODE' to 'PASS' (also in the new() call) resolved the issue, and the sample code worked as expected.

    Hope that helps.