Source for file class.POP3.php
Documentation is available at class.POP3.php
* mail_fetch/class.POP3.php
* Copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
* Modified by Philippe Mingo 2001 mingo@rotedic.com
* An RFC 1939 compliant wrapper class for the POP3 protocol.
* @copyright © 1999-2006 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: class.POP3.php,v 1.27 2006/07/15 12:01:18 tokul Exp $
* This is the POP3 class - DOCUMENT ME
var $ERROR =
''; // Error string.
var $TIMEOUT =
60; // Default timeout before giving up on a
var $COUNT = -
1; // Mailbox msg count
var $BUFFER =
512; // Socket buffer for socket fgets() calls.
// Per RFC 1939 the returned line a POP3
// server can send is 512 bytes.
var $FP =
''; // The connection to the server's
var $MAILSERVER =
''; // Set this to hard code the server name
var $DEBUG =
FALSE; // set to true to echo pop3
// commands and responses to error_log
// this WILL log passwords!
var $BANNER =
''; // Holds the banner returned by the
// pop server - used for apop()
var $ALLOWAPOP =
FALSE; // Allow or disallow apop()
// This must be set to true
function POP3 ( $server =
'', $timeout =
'' ) {
// Do not allow programs to alter MAILSERVER
// if it is already specified. They can get around
// this if they -really- want to, so don't count on it.
function connect ($server, $port =
110) {
// Opens a socket to the specified server. Unless overridden,
// port defaults to 110. Returns true on success, false on fail
// If MAILSERVER is set, override $server with it's value
if (!isset
($port) ||
!$port) {$port =
110;}
$this->ERROR =
_("POP3 connect:") .
' ' .
_("No server specified");
$fp =
@fsockopen("$server", $port, $errno, $errstr);
$this->ERROR =
_("POP3 connect:") .
' ' .
_("Error") .
' ' .
"[$errno] [$errstr]";
error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
if(!$this->is_ok($reply)) {
$this->ERROR =
_("POP3 connect:") .
' ' .
_("Error") .
' ' .
"[$reply]";
function user ($user =
"") {
// Sends the USER command, returns true or false
$this->ERROR =
_("POP3 user:") .
' ' .
_("no login ID submitted");
} elseif(!isset
($this->FP)) {
$this->ERROR =
_("POP3 user:") .
' ' .
_("connection not established");
if(!$this->is_ok($reply)) {
$this->ERROR =
_("POP3 user:") .
' ' .
_("Error") .
' ' .
"[$reply]";
function pass ($pass =
"") {
// Sends the PASS command, returns # of msgs in mailbox,
// returns false (undef) on Auth failure
$this->ERROR =
_("POP3 pass:") .
' ' .
_("No password submitted");
} elseif(!isset
($this->FP)) {
$this->ERROR =
_("POP3 pass:") .
' ' .
_("connection not established");
if(!$this->is_ok($reply)) {
$this->ERROR =
_("POP3 pass:") .
' ' .
_("Authentication failed") .
" [$reply]";
$count =
$this->last("count");
function apop ($login,$pass) {
// Attempts an APOP login. If this fails, it'll
// try a standard login. YOUR SERVER MUST SUPPORT
// THE USE OF THE APOP COMMAND!
// (apop is optional per rfc1939)
$this->ERROR =
_("POP3 apop:") .
' ' .
_("No connection to server");
$retVal =
$this->login($login,$pass);
} elseif(empty($login)) {
$this->ERROR =
_("POP3 apop:") .
' ' .
_("No login ID submitted");
$this->ERROR =
_("POP3 apop:") .
' ' .
_("No password submitted");
if( (!$banner) or (empty($banner)) ) {
$this->ERROR =
_("POP3 apop:") .
' ' .
_("No server banner") .
' - ' .
_("abort");
$retVal =
$this->login($login,$pass);
$APOPString =
md5($AuthString);
$cmd =
"APOP $login $APOPString";
if(!$this->is_ok($reply)) {
$this->ERROR =
_("POP3 apop:") .
' ' .
_("apop authentication failed") .
' - ' .
_("abort");
$retVal =
$this->login($login,$pass);
$count =
$this->last("count");
function login ($login =
"", $pass =
"") {
// Sends both user and pass. Returns # of msgs in mailbox or
// false on failure (or -1, if the error occurs while getting
// the number of messages.)
if( !isset
($this->FP) ) {
$this->ERROR =
_("POP3 login:") .
' ' .
_("No connection to server");
if( !$this->user( $login ) ) {
// Preserve the error generated by user()
$count =
$this->pass($pass);
if( (!$count) ||
($count == -
1) ) {
// Preserve the error generated by last() and pass()
function top ($msgNum, $numLines =
"0") {
// Gets the header and first $numLines of the msg body
// returns data in an array with each returned line being
// an array element. If $numLines is empty, returns
// only the header information, and none of the body.
$this->ERROR =
_("POP3 top:") .
' ' .
_("No connection to server");
$cmd =
"TOP $msgNum $numLines";
fwrite($fp, "TOP $msgNum $numLines\r\n");
$reply =
fgets($fp, $buffer);
@error_log("POP3 SEND [$cmd] GOT [$reply]",0);
if(!$this->is_ok($reply))
$this->ERROR =
_("POP3 top:") .
' ' .
_("Error") .
' ' .
"[$reply]";
$line =
fgets($fp,$buffer);
while ( !ereg("^\.\r\n",$line))
$MsgArray[$count] =
$line;
$line =
fgets($fp,$buffer);
if(empty($line)) { break; }
// If called with an argument, returns that msgs' size in octets
// No argument returns an associative array of undeleted
// msg numbers and their sizes in octets
$this->ERROR =
_("POP3 pop_list:") .
' ' .
_("No connection to server");
if( (!$Total) or ($Total == -
1) )
// return -1; // mailbox empty
@error_log("POP3 SEND [$cmd] GOT [$reply]",0);
if(!$this->is_ok($reply))
$this->ERROR =
_("POP3 pop_list:") .
' ' .
_("Error") .
' ' .
"[$reply]";
list
($junk,$num,$size) =
preg_split('/\s+/',$reply);
if(!$this->is_ok($reply))
$this->ERROR =
_("POP3 pop_list:") .
' ' .
_("Error") .
' ' .
"[$reply]";
for($msgC=
1;$msgC <=
$Total; $msgC++
)
if($msgC >
$Total) { break; }
$this->ERROR =
_("POP3 pop_list:") .
' ' .
_("Premature end of list");
list
($thisMsg,$msgSize) =
preg_split('/\s+/',$line);
$MsgArray[$msgC] =
"deleted";
$MsgArray[$msgC] =
$msgSize;
// Retrieve the specified msg number. Returns an array
// where each line of the msg is an array element.
$this->ERROR =
_("POP3 get:") .
' ' .
_("No connection to server");
if(!$this->is_ok($reply))
$this->ERROR =
_("POP3 get:") .
' ' .
_("Error") .
' ' .
"[$reply]";
$line =
fgets($fp,$buffer);
while ( !ereg("^\.\r\n",$line))
$MsgArray[$count] =
$line;
$line =
fgets($fp,$buffer);
if(empty($line)) { break; }
function last ( $type =
"count" ) {
// Returns the highest msg number in the mailbox.
// returns -1 on error, 0+ on success, if type != count
// results in a popstat() call (2 element array returned)
$this->ERROR =
_("POP3 last:") .
' ' .
_("No connection to server");
if(!$this->is_ok($reply))
$this->ERROR =
_("POP3 last:") .
' ' .
_("Error") .
' ' .
"[$reply]";
return array($count,$size);
// Resets the status of the remote server. This includes
// resetting the status of ALL msgs to not be deleted.
// This method automatically closes the connection to the server.
$this->ERROR =
_("POP3 reset:") .
' ' .
_("No connection to server");
if(!$this->is_ok($reply))
// The POP3 RSET command -never- gives a -ERR
// response - if it ever does, something truely
$this->ERROR =
_("POP3 reset:") .
' ' .
_("Error") .
' ' .
"