Perl 5 module to talk to a pop3 (rfc1939) server
use Mail::POP3Client; $pop = new Mail::POP3Client( USER => "me", PASSWORD => "mypassword", HOST => "pop3.do.main" ); for( $i = 1; $i <= $pop->Count(); $i++ ) { foreach( $pop->Head( $i ) ) { /^(From|Subject):\s+/i && print $_, "\n"; } } $pop->Close(); # OR with SSL $pop = new Mail::POP3Client( USER => "me", PASSWORD => "mypassword", HOST => "pop3.do.main", USESSL => true, ); # OR $pop2 = new Mail::POP3Client( HOST => "pop3.otherdo.main" ); $pop2->User( "somebody" ); $pop2->Pass( "doublesecret" ); $pop2->Connect() >= 0 || die $pop2->Message(); $pop2->Close(); # OR to use your own SSL socket... my $socket = IO::Socket::SSL->new( PeerAddr => 'pop.securedo.main', PeerPort => 993, Proto => 'tcp') || die "No socket!"; my $pop = Mail::POP3Client->new(); $pop->User('somebody'); $pop->Pass('doublesecret'); $pop->Socket($socket); $pop->Connect();
This module implements an Object-Oriented interface to a \s-1POP3\s0 server. It implements \s-1RFC1939\s0 (http://www.faqs.org/rfcs/rfc1939.html)
Here is a simple example to list out the From: and Subject: headers in your remote mailbox:
#!/usr/local/bin/perl
use Mail::POP3Client;
$pop = new Mail::POP3Client( USER => "me", PASSWORD => "mypassword", HOST => "pop3.do.main" ); for ($i = 1; $i <= $pop->Count(); $i++) { foreach ( $pop->Head( $i ) ) { /^(From|Subject):\s+/i and print $_, "\n"; } print "\n"; }
Old style (deprecated):
new Mail::POP3Client( \s-1USER\s0, \s-1PASSWORD\s0 [, \s-1HOST\s0, \s-1PORT\s0, \s-1DEBUG\s0, \s-1AUTH_MODE\s0] );
New style (shown with defaults):
new Mail::POP3Client( \s-1USER\s0 => "\*(L", \s-1PASSWORD\s0 => \*(R"\*(L", \s-1HOST\s0 => \*(R"pop3", \s-1PORT\s0 => 110, \s-1AUTH_MODE\s0 => '\s-1BEST\s0', \s-1DEBUG\s0 => 0, \s-1TIMEOUT\s0 => 60, \s-1LOCALADDR\s0 => 'xxx.xxx.xxx.xxx[:xx]', \s-1SOCKET\s0 => undef, \s-1USESSL\s0 => 0, );
\s-1USER\s0 is the userID of the account on the \s-1POP\s0 server
\s-1PASSWORD\s0 is the cleartext password for the userID
\s-1HOST\s0 is the \s-1POP\s0 server name or \s-1IP\s0 address (default = 'pop3')
\s-1PORT\s0 is the \s-1POP\s0 server port (default = 110)
\s-1DEBUG\s0 - any non-null, non-zero value turns on debugging (default = 0)
\s-1AUTH_MODE\s0 - pass '\s-1APOP\s0' to force \s-1APOP\s0 (\s-1MD5\s0) authorization. (default is '\s-1BEST\s0')
\s-1TIMEOUT\s0 - set a timeout value for socket operations (default = 60)
\s-1LOCALADDR\s0 - allow selecting a local inet address to use
These commands are intended to make writing a \s-1POP3\s0 client easier. They do not necessarily map directly to \s-1POP3\s0 commands defined in \s-1RFC1081\s0 or \s-1RFC1939\s0, although all commands should be supported. Some commands return multiple lines as an array in an array context.
Construct a new \s-1POP3\s0 connection with this. You should use the hash-style constructor. The old positional constructor is deprecated and will be removed in a future release. It is strongly recommended that you convert your code to the new version. You should give it at least 2 arguments: \s-1USER\s0 and \s-1PASSWORD\s0. The default \s-1HOST\s0 is 'pop3' which may or may not work for you. You can specify a different \s-1PORT\s0 (be careful here). new will attempt to Connect to and Login to the \s-1POP3\s0 server if you supply a \s-1USER\s0 and \s-1PASSWORD\s0. If you do not supply them in the constructor, you will need to call Connect yourself. The valid values for \s-1AUTH_MODE\s0 are '\s-1BEST\s0', '\s-1PASS\s0', '\s-1APOP\s0' and '\s-1CRAM-MD5\s0'. \s-1BEST\s0 says to try \s-1APOP\s0 if the server appears to support it and it can be used to successfully log on, next try similarly with \s-1CRAM-MD5\s0, and finally revert to \s-1PASS\s0. \s-1APOP\s0 and \s-1CRAM-MD5\s0 imply that an \s-1MD5\s0 checksum will be used instead of sending your password in cleartext. However, if the server does not claim to support \s-1APOP\s0 or \s-1CRAM-MD5\s0, the cleartext method will be used. Be careful. There are a few servers that will send a timestamp in the banner greeting, but \s-1APOP\s0 will not work with them (for instance if the server does not know your password in cleartext). If you think your authentication information is correct, run in \s-1DEBUG\s0 mode and look for errors regarding authorization. If so, then you may have to use '\s-1PASS\s0' for that server. The same applies to \s-1CRAM-MD5\s0, too. If you enable debugging with \s-1DEBUG\s0 => 1, socket traffic will be echoed to \s-1STDERR\s0. Another warning, it's impossible to differentiate between a timeout and a failure. If you pass a true value for \s-1USESSL\s0, the port will be changed to 995 if it is not set or is 110. Otherwise, it will use your port. If \s-1USESSL\s0 is true, IO::Socket::SSL will be loaded. If it is not in your perl, the call to connect will fail. new returns a valid Mail::POP3Client object in all cases. To test for a connection failure, you will need to check the number of messages: -1 indicates a connection error. This will likely change sometime in the future to return undef on an error, setting $! as a side effect. This change will not happen in any 2.x version.
Get the headers of the specified message, either as an array or as a string, depending on context. You can also specify a number of preview lines which will be returned with the headers. This may not be supported by all \s-1POP3\s0 server implementations as it is marked as optional in the \s-1RFC\s0. Submitted by Dennis Moroney <[email protected]>.
Get the body of the specified message, either as an array of lines or as a string, depending on context.
Get the body of the specified message and write it to the given file handle. my $fh = new IO::Handle(); $fh->fdopen( fileno( \s-1STDOUT\s0 ), \*(L"w\*(R" ); $pop->BodyToFile( $fh, 1 ); Does no stripping of \s-1NL\s0 or \s-1CR\s0.
Get the head and body of the specified message, either as an array of lines or as a string, depending on context.
foreach ( $pop->HeadAndBody( 1 ) )
print $_, \*(L"\n\*(R";
prints out the complete text of message 1.
Get the head and body of the specified message and write it to the given file handle. my $fh = new IO::Handle(); $fh->fdopen( fileno( \s-1STDOUT\s0 ), \*(L"w\*(R" ); $pop->HeadAndBodyToFile( $fh, 1 ); Does no stripping of \s-1NL\s0 or \s-1CR\s0.
Same as HeadAndBody.
Same as HeadAndBodyToFile.
Mark the specified message number as \s-1DELETED\s0. Becomes effective upon \s-1QUIT\s0 (invoking the Close method). Can be reset with a Reset message.
Start the connection to the \s-1POP3\s0 server. You can pass in the host and port. Returns 1 if the connection succeeds, or 0 if it fails (Message will contain a reason). The constructor always returns a blessed reference to a Mail::POP3Client obhect. This may change in a version 3.x release, but never in a 2.x release.
Close the connection gracefully. \s-1POP3\s0 says this will perform any pending deletes on the server.
Return true or false on whether the connection is active.
Return the file descriptor for the socket, or set if supplied.
Set/Return the size of the remote mailbox. Set by POPStat.
Set/Return the number of remote messages. Set during Login.
The last status message received from the server or a message describing any problem encountered.
The internal state of the connection: \s-1DEAD\s0, \s-1AUTHORIZATION\s0, \s-1TRANSACTION\s0.
Return the results of a \s-1POP3\s0 \s-1STAT\s0 command. Sets the size of the mailbox.
Returns the size of the given message number when called with an argument using the following format: <message_number> <size_in_bytes> If message_number is omitted, List behaves the same as ListArray, returning an indexed array of the sizes of each message in the same format. You can parse the size in bytes using split: ($msgnum, $size) = split('\s+', $pop -> List( n ));
Return a list of sizes of each message. This returns an indexed array, with each message number as an index (starting from 1) and the value as the next entry on the line. Beware that some servers send additional info for each message for the list command. That info may be lost.
Return the unique \s-1ID\s0 for the given message (or all of them). Returns an indexed array with an entry for each valid message number. Indexing begins at 1 to coincide with the server's indexing.
Query server capabilities, as described in \s-1RFC\s0 2449. Returns the capabilities in an array. Valid in all states.
Optional extended commands. Transaction state only.
Return the number of the last message, retrieved from the server.
Tell the server to unmark any message marked for deletion.
Set/Return the current user name.
Set/Return the current user name.
Attempt to login to the server connection.
Set/Return the current host.
Set/Return the current port number.
Basic Mail::IMAPClient method calls are also supported: close, connect, login, message_string, Password, and unseen. Also, empty stubs are provided for Folder, folders, Peek, select, and Uid.
This module does not have mandatory requirements for modules that are not part of the standard Perl distribution. However, \s-1APOP\s0 needs need Digest::MD5 and \s-1CRAM-MD5\s0 needs Digest::HMAC_MD5 and MIME::Base64.
Sean Dowd <[email protected]>
Based loosely on News::NNTPClient by Rodger Anderson <[email protected]>.
perl\|(1)
the Digest::MD5 manpage, the Digest::HMAC_MD5 manpage, the MIME::Base64 manpage
\s-1RFC\s0 1939: Post Office Protocol - Version 3
\s-1RFC\s0 2195: \s-1IMAP/POP\s0 AUTHorize Extension for Simple Challenge/Response
\s-1RFC\s0 2449: \s-1POP3\s0 Extension Mechanism