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
31 * Author: Richard Davey (rich@corephp.co.uk)
32 * License: LGPL, see PHPMailer License
34 * Specifically for PHPMailer to allow POP before SMTP authentication.
35 * Does not yet work with APOP - if you have an APOP account, contact me
36 * and we can test changes to this script.
38 * This class is based on the structure of the SMTP class by Chris Ryan
40 * This class is rfc 1939 compliant and implements all the commands
41 * required for POP3 connection, authentication and disconnection.
44 * @author Richard Davey
59 var $POP3_TIMEOUT = 30;
62 * POP3 Carriage Return + Line Feed
68 * Displaying Debug warnings? (0 = now, 1+ = yes)
108 var $error; // Error log array
112 * Constructor, sets the initial values
119 $this->connected = false;
124 * Combination of public events - connect, login, disconnect
126 * @param string $host
127 * @param integer $port
128 * @param integer $tval
129 * @param string $username
130 * @param string $password
132 function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0)
136 // If no port value is passed, retrieve it
139 $this->port = $this->POP3_PORT;
146 // If no port value is passed, retrieve it
149 $this->tval = $this->POP3_TIMEOUT;
156 $this->do_debug = $debug_level;
157 $this->username = $username;
158 $this->password = $password;
160 // Refresh the error log
164 $result = $this->Connect($this->host, $this->port, $this->tval);
168 $login_result = $this->Login($this->username, $this->password);
179 // We need to disconnect regardless if the login succeeded
186 * Connect to the POP3 server
188 * @param string $host
189 * @param integer $port
190 * @param integer $tval
193 function Connect ($host, $port = false, $tval = 30)
195 // Are we already connected?
196 if ($this->connected)
202 On Windows this will raise a PHP Warning error if the hostname doesn't exist.
203 Rather than supress it with @fsockopen, let's capture it cleanly instead
206 set_error_handler(array(&$this, 'catchWarning'));
208 // Connect to the POP3 server
209 $this->pop_conn = fsockopen($host, // POP3 Host
211 $errno, // Error Number
212 $errstr, // Error Message
213 $tval); // Timeout (seconds)
215 // Restore the error handler
216 restore_error_handler();
218 // Does the Error Log now contain anything?
219 if ($this->error && $this->do_debug >= 1)
221 $this->displayErrors();
225 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",
234 if ($this->do_debug >= 1)
236 $this->displayErrors();
242 // Increase the stream time-out
244 // Check for PHP 4.3.0 or later
245 if (version_compare(phpversion(), '4.3.0', 'ge'))
247 stream_set_timeout($this->pop_conn, $tval, 0);
251 // Does not work on Windows
252 if (substr(PHP_OS, 0, 3) !== 'WIN')
254 socket_set_timeout($this->pop_conn, $tval, 0);
258 // Get the POP3 server response
259 $pop3_response = $this->getResponse();
262 if ($this->checkResponse($pop3_response))
264 // The connection is established and the POP3 server is talking
265 $this->connected = true;
272 * Login to the POP3 server (does not support APOP yet)
274 * @param string $username
275 * @param string $password
278 function Login ($username = "", $password = "")
280 if ($this->connected == false)
282 $this->error = 'Not connected to POP3 server';
284 if ($this->do_debug >= 1)
286 $this->displayErrors();
290 if (empty($username))
292 $username = $this->username;
295 if (empty($password))
297 $password = $this->password;
300 $pop_username = "USER $username" . $this->CRLF;
301 $pop_password = "PASS $password" . $this->CRLF;
304 $this->sendString($pop_username);
305 $pop3_response = $this->getResponse();
307 if ($this->checkResponse($pop3_response))
310 $this->sendString($pop_password);
311 $pop3_response = $this->getResponse();
313 if ($this->checkResponse($pop3_response))
329 * Disconnect from the POP3 server
331 function Disconnect ()
333 $this->sendString('QUIT');
335 fclose($this->pop_conn);
345 * Get the socket response back.
346 * $size is the maximum number of bytes to retrieve
348 * @param integer $size
351 function getResponse ($size = 128)
353 $pop3_response = fgets($this->pop_conn, $size);
355 return $pop3_response;
359 * Send a string down the open socket connection to the POP3 server
361 * @param string $string
364 function sendString ($string)
366 $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
373 * Checks the POP3 server response for +OK or -ERR
375 * @param string $string
378 function checkResponse ($string)
380 if (substr($string, 0, 3) !== '+OK')
382 $this->error = array(
383 'error' => "Server reported an error: $string",
388 if ($this->do_debug >= 1)
390 $this->displayErrors();
403 * If debug is enabled, display the error message array
406 function displayErrors ()
410 foreach ($this->error as $single_error)
412 print_r($single_error);
419 * Takes over from PHP for the socket warning handler
421 * @param integer $errno
422 * @param string $errstr
423 * @param string $errfile
424 * @param integer $errline
426 function catchWarning ($errno, $errstr, $errfile, $errline)
428 $this->error[] = array(
429 'error' => "Connecting to the POP3 server raised a PHP warning: ",