За последние 24 часа нас посетили 23667 программистов и 1598 роботов. Сейчас ищет 831 программист ...

Класс работы с pop3

Тема в разделе "Прочие вопросы по PHP", создана пользователем georgela, 9 мар 2012.

  1. georgela

    georgela Активный пользователь

    С нами с:
    11 май 2011
    Сообщения:
    395
    Симпатии:
    0
    Дайте пож-та ссылку на такой класс,если кто с ним работал.
    Только почту свою читать,особо много не надо
     
  2. Ganzal

    Ganzal Суперстар
    Команда форума Модератор

    С нами с:
    15 мар 2007
    Сообщения:
    9.893
    Симпатии:
    965
    самому написать?)))) протокол легкий)))
     
  3. georgela

    georgela Активный пользователь

    С нами с:
    11 май 2011
    Сообщения:
    395
    Симпатии:
    0
    Код (Text):
    1. <?php
    2.  
    3. class POP3
    4. {
    5.     public $host ;
    6.     public $port ;
    7.     public $timeout = 10;
    8.     public $error = null;
    9.     private $fd = null;
    10.    
    11.     public function __construct($host,$port=110)
    12.     {
    13.             $this->host=$host;$this->port=$port;
    14.         $this->fd = fsockopen($this->host,$this->port,&$errno,&$errstr,$this->timeout);
    15.         stream_set_blocking($this->fd,true);
    16.         if( $errno )
    17.         {
    18.             echo 'Error: '.$errno.': '.$errstr;
    19.             exit(1);
    20.         }
    21.         $msg = $this->_read();
    22.         if( ! $this->_check($msg) )
    23.         {
    24.             $this->error = $msg;
    25.             $this->_write('QUIT');
    26.         }
    27.     }
    28.    
    29.     private function _write($cmd)
    30.     {
    31.         fwrite($this->fd,$cmd."\r\n");
    32.     }
    33.    
    34.     private function _read($stream=false)
    35.     {
    36.         $retval = null;
    37.         if( ! $stream )
    38.         {
    39.             $retval = fgets($this->fd,1024);
    40.             //$retval = preg_replace("/\r?\n/","\r\n",$retval);
    41.         } else {
    42.             while( ! feof($this->fd) )
    43.             {
    44.                 $tmp = fgets($this->fd,1024);
    45.                 //$tmp = preg_replace("/\r?\n/","\r\n",$tmp);
    46.                 if( chop($tmp) == '.') break;
    47.                 $retval .= $tmp;
    48.             }
    49.         }
    50.         return $retval;
    51.     }
    52.    
    53.     private function _check($msg)
    54.     {
    55.         $stat = substr($msg,0,strpos($msg,' '));
    56.         if($stat == '-ERR') return false;
    57.         //if($stat == '+OK') return true;
    58.         return true;
    59.     }
    60.  
    61.     //login to server
    62.     public function pUSERPASS($user, $password)
    63.     {
    64.         $this->_write('USER '.$user);
    65.         $msg = $this->_read();
    66.         if( ! $this->_check($msg) )
    67.         {
    68.             $this->error = $msg;
    69.             $this->_write('QUIT');
    70.             return false;
    71.         }
    72.         $this->_write('PASS '.$password);
    73.         $msg = $this->_read();
    74.         if( ! $this->_check($msg) )
    75.         {
    76.             $this->error = $msg;
    77.             $this->_write('QUIT');
    78.             return false;
    79.         }
    80.         return true;
    81.     }
    82.    
    83.     public function pSTAT()
    84.     {
    85.         $retval = null;
    86.         $this->_write('STAT');
    87.         $msg = $this->_read();
    88.         if( ! $this->_check($msg) )
    89.         {
    90.             $this->error = $msg;
    91.             $this->_write('QUIT');
    92.             return false;
    93.         }
    94.         list(,$retval['number'],$retval['size']) = split(' ',$msg);
    95.         return $retval;
    96.     }
    97.    
    98.     //list messages on server
    99.     public function pLIST()
    100.     {
    101.         $this->_write('LIST');
    102.         $msg = $this->_read();
    103.         if( ! $this->_check($msg) )
    104.         {
    105.             $this->error = $msg;
    106.             $this->_write('QUIT');
    107.             return false;
    108.         }
    109.         $msg = split("\n",$this->_read(true));
    110.         for($x = 0; $x < sizeof($msg); $x++ )
    111.         {
    112.             $tmp = split(' ',$msg[$x]);
    113.             $retval[$tmp[0]] = $tmp[1];
    114.         }
    115.         unset($retval['']);
    116.         return $retval;
    117.     }
    118.    
    119.     //retrive a message from server
    120.     public function pRETR($num)
    121.     {
    122.         $this->_write('RETR '.$num);
    123.         $msg = $this->_read();
    124.         if( ! $this->_check($msg) )
    125.         {
    126.             $this->error = $msg;
    127.             $this->_write('QUIT');
    128.             return false;
    129.         }
    130.         $msg = $this->_read(true);
    131.         return $msg;
    132.     }
    133.    
    134.     //marks a message deletion from the server
    135.     //it is not actually deleted until the QUIT command is issued.
    136.     //If you lose the connection to the mail server before issuing
    137.     //the QUIT command, the server should not delete any messages
    138.     public function pDELE($num)
    139.     {
    140.         $this->_write('DELE '.$num);
    141.         $msg = $this->_read();
    142.         if( ! $this->_check($msg) )
    143.         {
    144.             $this->error = $msg;
    145.             $this->_write('QUIT');
    146.             return false;
    147.         }
    148.     }
    149.    
    150.     //This resets (unmarks) any messages previously marked for deletion in this session
    151.     //so that the QUIT command will not delete them
    152.     public function pRSET()
    153.     {
    154.         $this->_write('RSET');
    155.         $msg = $this->_read();
    156.         if( ! $this->_check($msg) )
    157.         {
    158.             $this->error = $msg;
    159.             return false;
    160.         }
    161.     }
    162.    
    163.     //This deletes any messages marked for deletion, and then logs you off of the mail server.
    164.     //This is the last command to use. This does not disconnect you from the ISP, just the mailbox.
    165.     public function pQUIT()
    166.     {
    167.         $this->_write('QUIT');
    168.         $msg = $this->_read();
    169.         if( ! $this->_check($msg) )
    170.         {
    171.             $this->error = $msg;
    172.             return false;
    173.         }      
    174.     }
    175.    
    176.     public function __destruct()
    177.     {
    178.         fclose($this->fd);
    179.     }
    180. }
    181.  
    182. //EXAMPLE:
    183.  
    184.  
    185. $pop3 = new POP3();
    186.  
    187. $pop3->pUSERPASS('tieto','123456');
    188. if($pop3->error){echo 'ERROR: '.$pop3->error; exit(0); }
    189.  
    190. $messages = $pop3->pLIST();
    191.  
    192. echo $pop3->pRETR(1);
    193.  
    194. $pop3->pQUIT();
    195. ?>
    Качал с phpclasses.org

    Добавлено спустя 3 минуты 8 секунд:
    Есть какой нить класс интересный на php?Набираю библиотеку программиста
     
  4. sobachnik

    sobachnik Старожил

    С нами с:
    20 апр 2007
    Сообщения:
    3.380
    Симпатии:
    13
    Адрес:
    Дмитров, МО
    PHPMailer :)
     
  5. georgela

    georgela Активный пользователь

    С нами с:
    11 май 2011
    Сообщения:
    395
    Симпатии:
    0
    sobachnik
    Ага,им походу Джумла пользуется.