Source for file pop3.php

Documentation is available at pop3.php

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

Documentation generated on Tue, 19 Nov 2013 15:10:54 +0100 by phpDocumentor 1.4.3