3 .---------------------------------------------------------------------------.
4 | Software: PHPMailer - PHP email class |
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 '---------------------------------------------------------------------------'
28 * POP Before SMTP Authentication Class
30 * Author: Richard Davey (rich@corephp.co.uk)
31 * License: LGPL, see PHPMailer License
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.
37 * This class is based on the structure of the SMTP class by Chris Ryan
39 * This class is rfc 1939 compliant and implements all the commands
40 * required for POP3 connection, authentication and disconnection.
43 * @author Richard Davey
58 var $POP3_TIMEOUT = 30;
61 * POP3 Carriage Return + Line Feed
67 * Displaying Debug warnings? (0 = now, 1+ = yes)
107 var $error; // Error log array
111 * Constructor, sets the initial values
118 $this->connected = false;
123 * Combination of public events - connect, login, disconnect
125 * @param string $host
126 * @param integer $port
127 * @param integer $tval
128 * @param string $username
129 * @param string $password
131 function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0)
135 // If no port value is passed, retrieve it
138 $this->port = $this->POP3_PORT;
145 // If no port value is passed, retrieve it
148 $this->tval = $this->POP3_TIMEOUT;
155 $this->do_debug = $debug_level;
156 $this->username = $username;
157 $this->password = $password;
159 // Refresh the error log
163 $result = $this->Connect($this->host, $this->port, $this->tval);
167 $login_result = $this->Login($this->username, $this->password);
178 // We need to disconnect regardless if the login succeeded
185 * Connect to the POP3 server
187 * @param string $host
188 * @param integer $port
189 * @param integer $tval
192 function Connect ($host, $port = false, $tval = 30)
194 // Are we already connected?
195 if ($this->connected)
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
205 set_error_handler(array(&$this, 'catchWarning'));
207 // Connect to the POP3 server
208 $this->pop_conn = fsockopen($host, // POP3 Host
210 $errno, // Error Number
211 $errstr, // Error Message
212 $tval); // Timeout (seconds)
214 // Restore the error handler
215 restore_error_handler();
217 // Does the Error Log now contain anything?
218 if ($this->error && $this->do_debug >= 1)
220 $this->displayErrors();
224 if ($this->pop_conn == false)
226 // It would appear not...
227 $this->error = array(
228 'error' => "Failed to connect to server $host on port $port",
233 if ($this->do_debug >= 1)
235 $this->displayErrors();
241 // Increase the stream time-out
243 // Check for PHP 4.3.0 or later
244 if (version_compare(phpversion(), '4.3.0', 'ge'))
246 stream_set_timeout($this->pop_conn, $tval, 0);
250 // Does not work on Windows
251 if (substr(PHP_OS, 0, 3) !== 'WIN')
253 socket_set_timeout($this->pop_conn, $tval, 0);
257 // Get the POP3 server response
258 $pop3_response = $this->getResponse();
261 if ($this->checkResponse($pop3_response))
263 // The connection is established and the POP3 server is talking
264 $this->connected = true;
271 * Login to the POP3 server (does not support APOP yet)
273 * @param string $username
274 * @param string $password
277 function Login ($username = '', $password = '')
279 if ($this->connected == false)
281 $this->error = 'Not connected to POP3 server';
283 if ($this->do_debug >= 1)
285 $this->displayErrors();
289 if (empty($username))
291 $username = $this->username;
294 if (empty($password))
296 $password = $this->password;
299 $pop_username = "USER $username" . $this->CRLF;
300 $pop_password = "PASS $password" . $this->CRLF;
303 $this->sendString($pop_username);
304 $pop3_response = $this->getResponse();
306 if ($this->checkResponse($pop3_response))
309 $this->sendString($pop_password);
310 $pop3_response = $this->getResponse();
312 if ($this->checkResponse($pop3_response))
328 * Disconnect from the POP3 server
330 function Disconnect ()
332 $this->sendString('QUIT');
334 fclose($this->pop_conn);
344 * Get the socket response back.
345 * $size is the maximum number of bytes to retrieve
347 * @param integer $size
350 function getResponse ($size = 128)
352 $pop3_response = fgets($this->pop_conn, $size);
354 return $pop3_response;
358 * Send a string down the open socket connection to the POP3 server
360 * @param string $string
363 function sendString ($string)
365 $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
372 * Checks the POP3 server response for +OK or -ERR
374 * @param string $string
377 function checkResponse ($string)
379 if (substr($string, 0, 3) !== '+OK')
381 $this->error = array(
382 'error' => "Server reported an error: $string",
387 if ($this->do_debug >= 1)
389 $this->displayErrors();
402 * If debug is enabled, display the error message array
405 function displayErrors ()
409 foreach ($this->error as $single_error)
411 print_r($single_error);
418 * Takes over from PHP for the socket warning handler
420 * @param integer $errno
421 * @param string $errstr
422 * @param string $errfile
423 * @param integer $errline
425 function catchWarning ($errno, $errstr, $errfile, $errline)
427 $this->error[] = array(
428 'error' => "Connecting to the POP3 server raised a PHP warning: ",