18.5. Reading Mail with POP3ProblemYou want to fetch mail from a POP3 server. This lets you write a program to summarize your unread mail, move it from a remote server to a local mailbox, or toggle between Internet and local mail systems. SolutionUse the CPAN module Net::POP3: $pop = Net::POP3->new($mail_server) or die "Can't open connection to $mail_server : $!\n"; defined ($pop->login($username, $password)) or die "Can't authenticate: $!\n"; $messages = $pop->list or die "Can't get list of undeleted messages: $!\n"; foreach $msgid (keys %$messages) { $message = $pop->get($msgid); unless (defined $message) { warn "Couldn't fetch $msgid from server: $!\n"; next; } # $message is a reference to an array of lines $pop->delete($msgid); } DiscussionTraditionally, mail has been a three-party system: the MTA (Mail Transport Agent, a system program like sendmail ) delivers mail to the spool , where it is read by the MUA (Mail User Agent, a program like mail ). This dates from the days of big servers holding mail and users reading it through dumb terminals. As PCs and networks entered the picture, the need arose for MUAs like Pine to run on different machines than the one housing the spool. The Post Office Protocol (POP) implements efficient message listing, reading, and deleting over a TCP/IP session.
The CPAN module Net::POP3 is a POP client. That is, it lets your Perl program act as an MUA. The first step in using Net::POP3 is to create a new Net::POP3 object. Pass $pop = Net::POP3->new( "pop.myisp.com" ) or die "Can't connect to pop.myisp.com: $!\n";
All Net::POP3 functions return
You may optionally pass further arguments to $pop = Net::POP3->new( "pop.myisp.com", Timeout => 30 ) or die "Can't connect to pop.myisp.com : $!\n";
Authenticate yourself to the POP3 server with the
defined ($pop->login("gnat", "S33kr1T Pa55w0rD"))
or die "Hey, my username and password didn't work!\n";
defined ($pop->login( "midget" )) # use Net::Netrc to find password
or die "Authentication failed.\n";
defined ($pop->
The $pop->apop( $username, $password ) or die "Couldn't authenticate: $!\n";
Once authenticated, you may then access the spool with
%undeleted = $pop->
To retrieve a message, call print "Retrieving $msgnum : "; $message = $pop->get($msgnum); if ($message) { # succeeded print "\n"; print @$message; # print the message } else { # failed print "failed ($!)\n"; }
The
You have probably noticed there's no way to
send
mail. POP3 only supports reading and deleting existing messages. To send new ones, you still have to use programs like
mail
or
sendmail
, or do The task attempted by POP3 - connecting mail clients and mail servers - is also attempted by the IMAP protocol. IMAP has more features and is more typically seen on very large sites. See AlsoThe documentation for the Net::POP3 module from CPAN; RFC 1734, POP3 AUTHentication command ; RFC 1957, Some Observations on Implementations of the Post Office Protocol Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|