3 .---------------------------------------------------------------------------.
 
   4 |  Software: PHPMailer - PHP email class                                    |
 
   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 '---------------------------------------------------------------------------'
 
  27  * PHPMailer - PHP POP Before SMTP Authentication Class
 
  28  * NOTE: Designed for use with PHP version 5 and up
 
  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)
 
  39  * PHP POP-Before-SMTP Authentication Class
 
  43  * @license: LGPL, see PHPMailer License
 
  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.
 
  49  * This class is based on the structure of the SMTP class originally authored by Chris Ryan
 
  51  * This class is rfc 1939 compliant and implements all the commands
 
  52  * required for POP3 connection, authentication and disconnection.
 
  55  * @author Richard Davey (orig) <rich@corephp.co.uk>
 
  56  * @author Andy Prevost
 
  57  * @author Jim Jagielski
 
  65   public $POP3_PORT = 110;
 
  71   public $POP3_TIMEOUT = 30;
 
  74    * POP3 Carriage Return + Line Feed
 
  77   public $CRLF = "\r\n";
 
  80    * Displaying Debug warnings? (0 = now, 1+ = yes)
 
 116    * Sets the POP3 PHPMailer Version number
 
 119   public $Version         = '5.2.6';
 
 121   /////////////////////////////////////////////////
 
 122   // PROPERTIES, PRIVATE AND PROTECTED
 
 123   /////////////////////////////////////////////////
 
 126    * @var resource Resource handle for the POP connection socket
 
 130    * @var boolean Are we connected?
 
 134    * @var array Error container
 
 136   private $error;     //  Error log array
 
 139    * Constructor, sets the initial values
 
 143   public function __construct() {
 
 145     $this->connected = false;
 
 150    * Combination of public events - connect, login, disconnect
 
 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
 
 160   public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
 
 163     //  If no port value is passed, retrieve it
 
 164     if ($port == false) {
 
 165       $this->port = $this->POP3_PORT;
 
 170     //  If no port value is passed, retrieve it
 
 171     if ($tval == false) {
 
 172       $this->tval = $this->POP3_TIMEOUT;
 
 177     $this->do_debug = $debug_level;
 
 178     $this->username = $username;
 
 179     $this->password = $password;
 
 181     //  Refresh the error log
 
 185     $result = $this->Connect($this->host, $this->port, $this->tval);
 
 188       $login_result = $this->Login($this->username, $this->password);
 
 198     //  We need to disconnect regardless if the login succeeded
 
 205    * Connect to the POP3 server
 
 207    * @param string $host
 
 208    * @param bool|int $port
 
 209    * @param integer $tval
 
 212   public function Connect ($host, $port = false, $tval = 30) {
 
 213     //  Are we already connected?
 
 214     if ($this->connected) {
 
 219     On Windows this will raise a PHP Warning error if the hostname doesn't exist.
 
 220     Rather than supress it with @fsockopen, let's capture it cleanly instead
 
 223     set_error_handler(array(&$this, 'catchWarning'));
 
 225     //  Connect to the POP3 server
 
 226     $this->pop_conn = fsockopen($host,    //  POP3 Host
 
 228                   $errno,   //  Error Number
 
 229                   $errstr,  //  Error Message
 
 230                   $tval);   //  Timeout (seconds)
 
 232     //  Restore the error handler
 
 233     restore_error_handler();
 
 235     //  Does the Error Log now contain anything?
 
 236     if ($this->error && $this->do_debug >= 1) {
 
 237       $this->displayErrors();
 
 241     if ($this->pop_conn == false) {
 
 242       //  It would appear not...
 
 243       $this->error = array(
 
 244         'error' => "Failed to connect to server $host on port $port",
 
 249       if ($this->do_debug >= 1) {
 
 250         $this->displayErrors();
 
 256     //  Increase the stream time-out
 
 258     //  Check for PHP 4.3.0 or later
 
 259     if (version_compare(phpversion(), '5.0.0', 'ge')) {
 
 260       stream_set_timeout($this->pop_conn, $tval, 0);
 
 262       //  Does not work on Windows
 
 263       if (substr(PHP_OS, 0, 3) !== 'WIN') {
 
 264         socket_set_timeout($this->pop_conn, $tval, 0);
 
 268     //  Get the POP3 server response
 
 269     $pop3_response = $this->getResponse();
 
 272     if ($this->checkResponse($pop3_response)) {
 
 273       //  The connection is established and the POP3 server is talking
 
 274       $this->connected = true;
 
 281    * Login to the POP3 server (does not support APOP yet)
 
 283    * @param string $username
 
 284    * @param string $password
 
 287   public function Login ($username = '', $password = '') {
 
 288     if ($this->connected == false) {
 
 289       $this->error = 'Not connected to POP3 server';
 
 291       if ($this->do_debug >= 1) {
 
 292         $this->displayErrors();
 
 296     if (empty($username)) {
 
 297       $username = $this->username;
 
 300     if (empty($password)) {
 
 301       $password = $this->password;
 
 304     $pop_username = "USER $username" . $this->CRLF;
 
 305     $pop_password = "PASS $password" . $this->CRLF;
 
 308     $this->sendString($pop_username);
 
 309     $pop3_response = $this->getResponse();
 
 311     if ($this->checkResponse($pop3_response)) {
 
 313       $this->sendString($pop_password);
 
 314       $pop3_response = $this->getResponse();
 
 316       if ($this->checkResponse($pop3_response)) {
 
 324    * Disconnect from the POP3 server
 
 327   public function Disconnect () {
 
 328     $this->sendString('QUIT');
 
 330     fclose($this->pop_conn);
 
 333   /////////////////////////////////////////////////
 
 335   /////////////////////////////////////////////////
 
 338    * Get the socket response back.
 
 339    * $size is the maximum number of bytes to retrieve
 
 341    * @param integer $size
 
 344   private function getResponse ($size = 128) {
 
 345     $pop3_response = fgets($this->pop_conn, $size);
 
 347     return $pop3_response;
 
 351    * Send a string down the open socket connection to the POP3 server
 
 353    * @param string $string
 
 356   private function sendString ($string) {
 
 357     $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
 
 363    * Checks the POP3 server response for +OK or -ERR
 
 365    * @param string $string
 
 368   private function checkResponse ($string) {
 
 369     if (substr($string, 0, 3) !== '+OK') {
 
 370       $this->error = array(
 
 371         'error' => "Server reported an error: $string",
 
 376       if ($this->do_debug >= 1) {
 
 377         $this->displayErrors();
 
 388    * If debug is enabled, display the error message array
 
 391   private function displayErrors () {
 
 394     foreach ($this->error as $single_error) {
 
 395       print_r($single_error);
 
 402    * Takes over from PHP for the socket warning handler
 
 404    * @param integer $errno
 
 405    * @param string $errstr
 
 406    * @param string $errfile
 
 407    * @param integer $errline
 
 409   private function catchWarning ($errno, $errstr, $errfile, $errline) {
 
 410     $this->error[] = array(
 
 411       'error' => "Connecting to the POP3 server raised a PHP warning: ",