1 |
907
|
doc
|
<?php
|
2 |
|
|
/*~ class.pop3.php
|
3 |
|
|
.---------------------------------------------------------------------------.
|
4 |
|
|
| Software: PHPMailer - PHP email class |
|
5 |
|
|
| Version: 2.0.3 |
|
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 |
|
|
| Author: Andy Prevost (project admininistrator) |
|
11 |
|
|
| Author: Brent R. Matzelle (original founder) |
|
12 |
|
|
| Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. |
|
13 |
|
|
| Copyright (c) 2001-2003, Brent R. Matzelle |
|
14 |
|
|
| ------------------------------------------------------------------------- |
|
15 |
|
|
| License: Distributed under the Lesser General Public License (LGPL) |
|
16 |
|
|
| http://www.gnu.org/copyleft/lesser.html |
|
17 |
|
|
| This program is distributed in the hope that it will be useful - WITHOUT |
|
18 |
|
|
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
19 |
|
|
| FITNESS FOR A PARTICULAR PURPOSE. |
|
20 |
|
|
| ------------------------------------------------------------------------- |
|
21 |
|
|
| We offer a number of paid services (www.codeworxtech.com): |
|
22 |
|
|
| - Web Hosting on highly optimized fast and secure servers |
|
23 |
|
|
| - Technology Consulting |
|
24 |
|
|
| - Oursourcing (highly qualified programmers and graphic designers) |
|
25 |
|
|
'---------------------------------------------------------------------------'
|
26 |
|
|
|
27 |
|
|
/**
|
28 |
|
|
* POP Before SMTP Authentication Class
|
29 |
|
|
*
|
30 |
|
|
* Author: Richard Davey (rich@corephp.co.uk)
|
31 |
|
|
* License: LGPL, see PHPMailer License
|
32 |
|
|
*
|
33 |
|
|
* Specifically for PHPMailer to allow POP before SMTP authentication.
|
34 |
|
|
* Does not yet work with APOP - if you have an APOP account, contact me
|
35 |
|
|
* and we can test changes to this script.
|
36 |
|
|
*
|
37 |
|
|
* This class is based on the structure of the SMTP class by Chris Ryan
|
38 |
|
|
*
|
39 |
|
|
* This class is rfc 1939 compliant and implements all the commands
|
40 |
|
|
* required for POP3 connection, authentication and disconnection.
|
41 |
|
|
*
|
42 |
|
|
* @package PHPMailer
|
43 |
|
|
* @author Richard Davey
|
44 |
|
|
*/
|
45 |
|
|
|
46 |
|
|
class POP3
|
47 |
|
|
{
|
48 |
|
|
/**
|
49 |
|
|
* Default POP3 port
|
50 |
|
|
* @var int
|
51 |
|
|
*/
|
52 |
|
|
var $POP3_PORT = 110;
|
53 |
|
|
|
54 |
|
|
/**
|
55 |
|
|
* Default Timeout
|
56 |
|
|
* @var int
|
57 |
|
|
*/
|
58 |
|
|
var $POP3_TIMEOUT = 30;
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* POP3 Carriage Return + Line Feed
|
62 |
|
|
* @var string
|
63 |
|
|
*/
|
64 |
|
|
var $CRLF = "\r\n";
|
65 |
|
|
|
66 |
|
|
/**
|
67 |
|
|
* Displaying Debug warnings? (0 = now, 1+ = yes)
|
68 |
|
|
* @var int
|
69 |
|
|
*/
|
70 |
|
|
var $do_debug = 2;
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* POP3 Mail Server
|
74 |
|
|
* @var string
|
75 |
|
|
*/
|
76 |
|
|
var $host;
|
77 |
|
|
|
78 |
|
|
/**
|
79 |
|
|
* POP3 Port
|
80 |
|
|
* @var int
|
81 |
|
|
*/
|
82 |
|
|
var $port;
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
|
|
* POP3 Timeout Value
|
86 |
|
|
* @var int
|
87 |
|
|
*/
|
88 |
|
|
var $tval;
|
89 |
|
|
|
90 |
|
|
/**
|
91 |
|
|
* POP3 Username
|
92 |
|
|
* @var string
|
93 |
|
|
*/
|
94 |
|
|
var $username;
|
95 |
|
|
|
96 |
|
|
/**
|
97 |
|
|
* POP3 Password
|
98 |
|
|
* @var string
|
99 |
|
|
*/
|
100 |
|
|
var $password;
|
101 |
|
|
|
102 |
|
|
/**#@+
|
103 |
|
|
* @access private
|
104 |
|
|
*/
|
105 |
|
|
var $pop_conn;
|
106 |
|
|
var $connected;
|
107 |
|
|
var $error; // Error log array
|
108 |
|
|
/**#@-*/
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* Constructor, sets the initial values
|
112 |
|
|
*
|
113 |
|
|
* @return POP3
|
114 |
|
|
*/
|
115 |
|
|
function POP3 ()
|
116 |
|
|
{
|
117 |
|
|
$this->pop_conn = 0;
|
118 |
|
|
$this->connected = false;
|
119 |
|
|
$this->error = null;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
/**
|
123 |
|
|
* 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
|
130 |
|
|
*/
|
131 |
|
|
function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0)
|
132 |
|
|
{
|
133 |
|
|
$this->host = $host;
|
134 |
|
|
|
135 |
|
|
// If no port value is passed, retrieve it
|
136 |
|
|
if ($port == false)
|
137 |
|
|
{
|
138 |
|
|
$this->port = $this->POP3_PORT;
|
139 |
|
|
}
|
140 |
|
|
else
|
141 |
|
|
{
|
142 |
|
|
$this->port = $port;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
// If no port value is passed, retrieve it
|
146 |
|
|
if ($tval == false)
|
147 |
|
|
{
|
148 |
|
|
$this->tval = $this->POP3_TIMEOUT;
|
149 |
|
|
}
|
150 |
|
|
else
|
151 |
|
|
{
|
152 |
|
|
$this->tval = $tval;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
$this->do_debug = $debug_level;
|
156 |
|
|
$this->username = $username;
|
157 |
|
|
$this->password = $password;
|
158 |
|
|
|
159 |
|
|
// Refresh the error log
|
160 |
|
|
$this->error = null;
|
161 |
|
|
|
162 |
|
|
// Connect
|
163 |
|
|
$result = $this->Connect($this->host, $this->port, $this->tval);
|
164 |
|
|
|
165 |
|
|
if ($result)
|
166 |
|
|
{
|
167 |
|
|
$login_result = $this->Login($this->username, $this->password);
|
168 |
|
|
|
169 |
|
|
if ($login_result)
|
170 |
|
|
{
|
171 |
|
|
$this->Disconnect();
|
172 |
|
|
|
173 |
|
|
return true;
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
// We need to disconnect regardless if the login succeeded
|
179 |
|
|
$this->Disconnect();
|
180 |
|
|
|
181 |
|
|
return false;
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/**
|
185 |
|
|
* Connect to the POP3 server
|
186 |
|
|
*
|
187 |
|
|
* @param string $host
|
188 |
|
|
* @param integer $port
|
189 |
|
|
* @param integer $tval
|
190 |
|
|
* @return boolean
|
191 |
|
|
*/
|
192 |
|
|
function Connect ($host, $port = false, $tval = 30)
|
193 |
|
|
{
|
194 |
|
|
// Are we already connected?
|
195 |
|
|
if ($this->connected)
|
196 |
|
|
{
|
197 |
|
|
return true;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
/*
|
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
|
203 |
|
|
*/
|
204 |
|
|
|
205 |
|
|
set_error_handler(array(&$this, 'catchWarning'));
|
206 |
|
|
|
207 |
|
|
// Connect to the POP3 server
|
208 |
|
|
$this->pop_conn = fsockopen($host, // POP3 Host
|
209 |
|
|
$port, // Port #
|
210 |
|
|
$errno, // Error Number
|
211 |
|
|
$errstr, // Error Message
|
212 |
|
|
$tval); // Timeout (seconds)
|
213 |
|
|
|
214 |
|
|
// Restore the error handler
|
215 |
|
|
restore_error_handler();
|
216 |
|
|
|
217 |
|
|
// Does the Error Log now contain anything?
|
218 |
|
|
if ($this->error && $this->do_debug >= 1)
|
219 |
|
|
{
|
220 |
|
|
$this->displayErrors();
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
// 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 |
|
|
);
|
232 |
|
|
|
233 |
|
|
if ($this->do_debug >= 1)
|
234 |
|
|
{
|
235 |
|
|
$this->displayErrors();
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
return false;
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
// Increase the stream time-out
|
242 |
|
|
|
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);
|
247 |
|
|
}
|
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 |
|
|
}
|
256 |
|
|
|
257 |
|
|
// Get the POP3 server response
|
258 |
|
|
$pop3_response = $this->getResponse();
|
259 |
|
|
|
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 |
|
|
|
268 |
|
|
}
|
269 |
|
|
|
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';
|
282 |
|
|
|
283 |
|
|
if ($this->do_debug >= 1)
|
284 |
|
|
{
|
285 |
|
|
$this->displayErrors();
|
286 |
|
|
}
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
if (empty($username))
|
290 |
|
|
{
|
291 |
|
|
$username = $this->username;
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
if (empty($password))
|
295 |
|
|
{
|
296 |
|
|
$password = $this->password;
|
297 |
|
|
}
|
298 |
|
|
|
299 |
|
|
$pop_username = "USER $username" . $this->CRLF;
|
300 |
|
|
$pop_password = "PASS $password" . $this->CRLF;
|
301 |
|
|
|
302 |
|
|
// Send the Username
|
303 |
|
|
$this->sendString($pop_username);
|
304 |
|
|
$pop3_response = $this->getResponse();
|
305 |
|
|
|
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 |
|
|
{
|
323 |
|
|
return false;
|
324 |
|
|
}
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
/**
|
328 |
|
|
* Disconnect from the POP3 server
|
329 |
|
|
*/
|
330 |
|
|
function Disconnect ()
|
331 |
|
|
{
|
332 |
|
|
$this->sendString('QUIT');
|
333 |
|
|
|
334 |
|
|
fclose($this->pop_conn);
|
335 |
|
|
}
|
336 |
|
|
|
337 |
|
|
/*
|
338 |
|
|
---------------
|
339 |
|
|
Private Methods
|
340 |
|
|
---------------
|
341 |
|
|
*/
|
342 |
|
|
|
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);
|
353 |
|
|
|
354 |
|
|
return $pop3_response;
|
355 |
|
|
}
|
356 |
|
|
|
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));
|
366 |
|
|
|
367 |
|
|
return $bytes_sent;
|
368 |
|
|
|
369 |
|
|
}
|
370 |
|
|
|
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;
|
393 |
|
|
}
|
394 |
|
|
else
|
395 |
|
|
{
|
396 |
|
|
return true;
|
397 |
|
|
}
|
398 |
|
|
|
399 |
|
|
}
|
400 |
|
|
|
401 |
|
|
/**
|
402 |
|
|
* If debug is enabled, display the error message array
|
403 |
|
|
*
|
404 |
|
|
*/
|
405 |
|
|
function displayErrors ()
|
406 |
|
|
{
|
407 |
|
|
echo '<pre>';
|
408 |
|
|
|
409 |
|
|
foreach ($this->error as $single_error)
|
410 |
|
|
{
|
411 |
|
|
print_r($single_error);
|
412 |
|
|
}
|
413 |
|
|
|
414 |
|
|
echo '</pre>';
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
/**
|
418 |
|
|
* 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
|
424 |
|
|
*/
|
425 |
|
|
function catchWarning ($errno, $errstr, $errfile, $errline)
|
426 |
|
|
{
|
427 |
|
|
$this->error[] = array(
|
428 |
|
|
'error' => "Connecting to the POP3 server raised a PHP warning: ",
|
429 |
|
|
'errno' => $errno,
|
430 |
|
|
'errstr' => $errstr
|
431 |
|
|
);
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
// End of class
|
435 |
|
|
}
|
436 |
|
|
?>
|