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