Project

General

Profile

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