Source for file class.pop3.php

Documentation is available at class.pop3.php

  1. <?php
  2. /*~ class.pop3.php
  3. .---------------------------------------------------------------------------.
  4. |  Software: PHPMailer - PHP email class                                    |
  5. |   Version: 5.1                                                            |
  6. |   Contact: via sourceforge.net support pages (also www.codeworxtech.com)  |
  7. |      Info: http://phpmailer.sourceforge.net                               |
  8. |   Support: http://sourceforge.net/projects/phpmailer/                     |
  9. | ------------------------------------------------------------------------- |
  10. |     Admin: Andy Prevost (project admininistrator)                         |
  11. |   Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
  12. |          : Marcus Bointon (coolbru) coolbru@users.sourceforge.net         |
  13. |   Founder: Brent R. Matzelle (original founder)                           |
  14. | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved.               |
  15. | Copyright (c) 2001-2003, Brent R. Matzelle                                |
  16. | ------------------------------------------------------------------------- |
  17. |   License: Distributed under the Lesser General Public License (LGPL)     |
  18. |            http://www.gnu.org/copyleft/lesser.html                        |
  19. | This program is distributed in the hope that it will be useful - WITHOUT  |
  20. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
  21. | FITNESS FOR A PARTICULAR PURPOSE.                                         |
  22. | ------------------------------------------------------------------------- |
  23. | We offer a number of paid services (www.codeworxtech.com):                |
  24. | - Web Hosting on highly optimized fast and secure servers                 |
  25. | - Technology Consulting                                                   |
  26. | - Oursourcing (highly qualified programmers and graphic designers)        |
  27. '---------------------------------------------------------------------------'
  28. */
  29.  
  30. /**
  31.  * PHPMailer - PHP POP Before SMTP Authentication Class
  32.  * NOTE: Designed for use with PHP version 5 and up
  33.  * @package PHPMailer
  34.  * @author Andy Prevost
  35.  * @author Marcus Bointon
  36.  * @copyright 2004 - 2009 Andy Prevost
  37.  * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
  38.  * @version $Id: class.pop3.php 444 2009-05-05 11:22:26Z coolbru $
  39.  */
  40.  
  41. /**
  42.  * POP Before SMTP Authentication Class
  43.  * Version 5.0.0
  44.  *
  45.  * Author: Richard Davey (rich@corephp.co.uk)
  46.  * Modifications: Andy Prevost
  47.  * License: LGPL, see PHPMailer License
  48.  *
  49.  * Specifically for PHPMailer to allow POP before SMTP authentication.
  50.  * Does not yet work with APOP - if you have an APOP account, contact Richard Davey
  51.  * and we can test changes to this script.
  52.  *
  53.  * This class is based on the structure of the SMTP class originally authored by Chris Ryan
  54.  *
  55.  * This class is rfc 1939 compliant and implements all the commands
  56.  * required for POP3 connection, authentication and disconnection.
  57.  *
  58.  * @package PHPMailer
  59.  * @author Richard Davey
  60.  */
  61.  
  62. class POP3 {
  63.   /**
  64.    * Default POP3 port
  65.    * @var int 
  66.    */
  67.   public $POP3_PORT = 110;
  68.  
  69.   /**
  70.    * Default Timeout
  71.    * @var int 
  72.    */
  73.   public $POP3_TIMEOUT = 30;
  74.  
  75.   /**
  76.    * POP3 Carriage Return + Line Feed
  77.    * @var string 
  78.    */
  79.   public $CRLF = "\r\n";
  80.  
  81.   /**
  82.    * Displaying Debug warnings? (0 = now, 1+ = yes)
  83.    * @var int 
  84.    */
  85.   public $do_debug = 2;
  86.  
  87.   /**
  88.    * POP3 Mail Server
  89.    * @var string 
  90.    */
  91.   public $host;
  92.  
  93.   /**
  94.    * POP3 Port
  95.    * @var int 
  96.    */
  97.   public $port;
  98.  
  99.   /**
  100.    * POP3 Timeout Value
  101.    * @var int 
  102.    */
  103.   public $tval;
  104.  
  105.   /**
  106.    * POP3 Username
  107.    * @var string 
  108.    */
  109.   public $username;
  110.  
  111.   /**
  112.    * POP3 Password
  113.    * @var string 
  114.    */
  115.   public $password;
  116.  
  117.   /////////////////////////////////////////////////
  118.   // PROPERTIES, PRIVATE AND PROTECTED
  119.   /////////////////////////////////////////////////
  120.  
  121.   private $pop_conn;
  122.   private $connected;
  123.   private $error;     //  Error log array
  124.  
  125.   /**
  126.    * Constructor, sets the initial values
  127.    * @access public
  128.    * @return POP3 
  129.    */
  130.   public function __construct({
  131.     $this->pop_conn  0;
  132.     $this->connected false;
  133.     $this->error     null;
  134.   }
  135.  
  136.   /**
  137.    * Combination of public events - connect, login, disconnect
  138.    * @access public
  139.    * @param string $host 
  140.    * @param integer $port 
  141.    * @param integer $tval 
  142.    * @param string $username 
  143.    * @param string $password 
  144.    */
  145.   public function Authorise ($host$port false$tval false$username$password$debug_level 0{
  146.     $this->host = $host;
  147.  
  148.     //  If no port value is passed, retrieve it
  149.     if ($port == false{
  150.       $this->port = $this->POP3_PORT;
  151.     else {
  152.       $this->port = $port;
  153.     }
  154.  
  155.     //  If no port value is passed, retrieve it
  156.     if ($tval == false{
  157.       $this->tval = $this->POP3_TIMEOUT;
  158.     else {
  159.       $this->tval = $tval;
  160.     }
  161.  
  162.     $this->do_debug = $debug_level;
  163.     $this->username = $username;
  164.     $this->password = $password;
  165.  
  166.     //  Refresh the error log
  167.     $this->error null;
  168.  
  169.     //  Connect
  170.     $result $this->Connect($this->host$this->port$this->tval);
  171.  
  172.     if ($result{
  173.       $login_result $this->Login($this->username$this->password);
  174.  
  175.       if ($login_result{
  176.         $this->Disconnect();
  177.  
  178.         return true;
  179.       }
  180.  
  181.     }
  182.  
  183.     //  We need to disconnect regardless if the login succeeded
  184.     $this->Disconnect();
  185.  
  186.     return false;
  187.   }
  188.  
  189.   /**
  190.    * Connect to the POP3 server
  191.    * @access public
  192.    * @param string $host 
  193.    * @param integer $port 
  194.    * @param integer $tval 
  195.    * @return boolean 
  196.    */
  197.   public function Connect ($host$port false$tval 30{
  198.     //  Are we already connected?
  199.     if ($this->connected{
  200.       return true;
  201.     }
  202.  
  203.     /*
  204.     On Windows this will raise a PHP Warning error if the hostname doesn't exist.
  205.     Rather than supress it with @fsockopen, let's capture it cleanly instead
  206.     */
  207.  
  208.     set_error_handler(array(&$this'catchWarning'));
  209.  
  210.     //  Connect to the POP3 server
  211.     $this->pop_conn fsockopen($host,    //  POP3 Host
  212.                   $port,    //  Port #
  213.                   $errno,   //  Error Number
  214.                   $errstr,  //  Error Message
  215.                   $tval);   //  Timeout (seconds)
  216.  
  217.     //  Restore the error handler
  218.  
  219.     //  Does the Error Log now contain anything?
  220.     if ($this->error && $this->do_debug >= 1{
  221.       $this->displayErrors();
  222.     }
  223.  
  224.     //  Did we connect?
  225.     if ($this->pop_conn == false{
  226.       //  It would appear not...
  227.       $this->error array(
  228.         'error' => "Failed to connect to server $host on port $port",
  229.         'errno' => $errno,
  230.         'errstr' => $errstr
  231.       );
  232.  
  233.       if ($this->do_debug >= 1{
  234.         $this->displayErrors();
  235.       }
  236.  
  237.       return false;
  238.     }
  239.  
  240.     //  Increase the stream time-out
  241.  
  242.     //  Check for PHP 4.3.0 or later
  243.     if (version_compare(phpversion()'5.0.0''ge')) {
  244.       stream_set_timeout($this->pop_conn$tval0);
  245.     else {
  246.       //  Does not work on Windows
  247.       if (substr(PHP_OS03!== 'WIN'{
  248.         socket_set_timeout($this->pop_conn$tval0);
  249.       }
  250.     }
  251.  
  252.     //  Get the POP3 server response
  253.     $pop3_response $this->getResponse();
  254.  
  255.     //  Check for the +OK
  256.     if ($this->checkResponse($pop3_response)) {
  257.     //  The connection is established and the POP3 server is talking
  258.     $this->connected true;
  259.       return true;
  260.     }
  261.  
  262.   }
  263.  
  264.   /**
  265.    * Login to the POP3 server (does not support APOP yet)
  266.    * @access public
  267.    * @param string $username 
  268.    * @param string $password 
  269.    * @return boolean 
  270.    */
  271.   public function Login ($username ''$password ''{
  272.     if ($this->connected == false{
  273.       $this->error 'Not connected to POP3 server';
  274.  
  275.       if ($this->do_debug >= 1{
  276.         $this->displayErrors();
  277.       }
  278.     }
  279.  
  280.     if (empty($username)) {
  281.       $username $this->username;
  282.     }
  283.  
  284.     if (empty($password)) {
  285.       $password $this->password;
  286.     }
  287.  
  288.     $pop_username "USER $username$this->CRLF;
  289.     $pop_password "PASS $password$this->CRLF;
  290.  
  291.     //  Send the Username
  292.     $this->sendString($pop_username);
  293.     $pop3_response $this->getResponse();
  294.  
  295.     if ($this->checkResponse($pop3_response)) {
  296.       //  Send the Password
  297.       $this->sendString($pop_password);
  298.       $pop3_response $this->getResponse();
  299.  
  300.       if ($this->checkResponse($pop3_response)) {
  301.         return true;
  302.       else {
  303.         return false;
  304.       }
  305.     else {
  306.       return false;
  307.     }
  308.   }
  309.  
  310.   /**
  311.    * Disconnect from the POP3 server
  312.    * @access public
  313.    */
  314.   public function Disconnect ({
  315.     $this->sendString('QUIT');
  316.  
  317.     fclose($this->pop_conn);
  318.   }
  319.  
  320.   /////////////////////////////////////////////////
  321.   //  Private Methods
  322.   /////////////////////////////////////////////////
  323.  
  324.   /**
  325.    * Get the socket response back.
  326.    * $size is the maximum number of bytes to retrieve
  327.    * @access private
  328.    * @param integer $size 
  329.    * @return string 
  330.    */
  331.   private function getResponse ($size 128{
  332.     $pop3_response fgets($this->pop_conn$size);
  333.  
  334.     return $pop3_response;
  335.   }
  336.  
  337.   /**
  338.    * Send a string down the open socket connection to the POP3 server
  339.    * @access private
  340.    * @param string $string 
  341.    * @return integer 
  342.    */
  343.   private function sendString ($string{
  344.     $bytes_sent fwrite($this->pop_conn$stringstrlen($string));
  345.  
  346.     return $bytes_sent;
  347.   }
  348.  
  349.   /**
  350.    * Checks the POP3 server response for +OK or -ERR
  351.    * @access private
  352.    * @param string $string 
  353.    * @return boolean 
  354.    */
  355.   private function checkResponse ($string{
  356.     if (substr($string03!== '+OK'{
  357.       $this->error array(
  358.         'error' => "Server reported an error: $string",
  359.         'errno' => 0,
  360.         'errstr' => ''
  361.       );
  362.  
  363.       if ($this->do_debug >= 1{
  364.         $this->displayErrors();
  365.       }
  366.  
  367.       return false;
  368.     else {
  369.       return true;
  370.     }
  371.  
  372.   }
  373.  
  374.   /**
  375.    * If debug is enabled, display the error message array
  376.    * @access private
  377.    */
  378.   private function displayErrors ({
  379.     echo '<pre>';
  380.  
  381.     foreach ($this->error as $single_error{
  382.       print_r($single_error);
  383.     }
  384.  
  385.     echo '</pre>';
  386.   }
  387.  
  388.   /**
  389.    * Takes over from PHP for the socket warning handler
  390.    * @access private
  391.    * @param integer $errno 
  392.    * @param string $errstr 
  393.    * @param string $errfile 
  394.    * @param integer $errline 
  395.    */
  396.   private function catchWarning ($errno$errstr$errfile$errline{
  397.     $this->error[array(
  398.       'error' => "Connecting to the POP3 server raised a PHP warning: ",
  399.       'errno' => $errno,
  400.       'errstr' => $errstr
  401.     );
  402.   }
  403.  
  404.   //  End of class
  405. }
  406. ?>

Documentation generated on Sun, 04 Apr 2010 22:43:37 +0200 by phpDocumentor 1.4.1