PHP Classes
Icontem

File: pop3


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of tieto  >  POP3  >  pop3  
File: pop3
Role: Class source
Content type: text/plain
Description: pop3 class
Class: POP3
Retrieve and delete messages from a POP3 mailbox
 

Contents

Class file image Download
class POP3
{
    public $host = '192.168.1.1';
    public $port = 110;
    public $timeout = 10;
    public $error = null;
    private $fd = null;
    
    public function __construct()
    {
        $this->fd = fsockopen($this->host,$this->port,&$errno,&$errstr,$this->timeout);
        stream_set_blocking($this->fd,true);
        if( $errno )
        {
            echo 'Error: '.$errno.': '.$errstr;
            exit(1);
        }
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            $this->_write('QUIT');
        }
    }
    
    private function _write($cmd)
    {
        fwrite($this->fd,$cmd."\r\n");
    }
    
    private function _read($stream=false)
    {
        $retval = null;
        if( ! $stream )
        {
            $retval = fgets($this->fd,1024);
            //$retval = preg_replace("/\r?\n/","\r\n",$retval);
        } else {
            while( ! feof($this->fd) )
            {
                $tmp = fgets($this->fd,1024);
                //$tmp = preg_replace("/\r?\n/","\r\n",$tmp);
                if( chop($tmp) == '.') break;
                $retval .= $tmp;
            }
        }
        return $retval;
    }
    
    private function _check($msg)
    {
        $stat = substr($msg,0,strpos($msg,' '));
        if($stat == '-ERR') return false;
        //if($stat == '+OK') return true;
        return true;
    }

    //login to server
    public function pUSERPASS($user, $password)
    {
        $this->_write('USER '.$user);
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            $this->_write('QUIT');
            return false;
        }
        $this->_write('PASS '.$password);
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            $this->_write('QUIT');
            return false;
        }
        return true;
    }
    
    public function pSTAT()
    {
        $retval = null;
        $this->_write('STAT');
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            $this->_write('QUIT');
            return false;
        }
        list(,$retval['number'],$retval['size']) = split(' ',$msg);
        return $retval;
    }
    
    //list messages on server
    public function pLIST()
    {
        $this->_write('LIST');
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            $this->_write('QUIT');
            return false;
        }
        $msg = split("\n",$this->_read(true));
        for($x = 0; $x < sizeof($msg); $x++ )
        {
            $tmp = split(' ',$msg[$x]);
            $retval[$tmp[0]] = $tmp[1];
        }
        unset($retval['']);
        return $retval;
    }
    
    //retrive a message from server
    public function pRETR($num)
    {
        $this->_write('RETR '.$num);
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            $this->_write('QUIT');
            return false;
        }
        $msg = $this->_read(true);
        return $msg;
    }
    
    //marks a message deletion from the server
    //it is not actually deleted until the QUIT command is issued.
    //If you lose the connection to the mail server before issuing
    //the QUIT command, the server should not delete any messages
    public function pDELE($num)
    {
        $this->_write('DELE '.$num);
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            $this->_write('QUIT');
            return false;
        }
    }
    
    //This resets (unmarks) any messages previously marked for deletion in this session
    //so that the QUIT command will not delete them
    public function pRSET()
    {
        $this->_write('RSET');
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            return false;
        }
    }
    
    //This deletes any messages marked for deletion, and then logs you off of the mail server.
    //This is the last command to use. This does not disconnect you from the ISP, just the mailbox.
    public function pQUIT()
    {
        $this->_write('QUIT');
        $msg = $this->_read();
        if( ! $this->_check($msg) )
        {
            $this->error = $msg;
            return false;
        }        
    }
    
    public function __destruct()
    {
        fclose($this->fd);
    }
}

//EXAMPLE:
$pop3 = new POP3();

$pop3->pUSERPASS('tieto','123456');
if($pop3->error){echo 'ERROR: '.$pop3->error; exit(0); }

$messages = $pop3->pLIST();

echo $pop3->pRETR(1);

$pop3->pQUIT();

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products