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 | 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. |
15 | 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 | We offer a number of paid services (www.codeworxtech.com): |
24 | - Web Hosting on highly optimized fast and secure servers |
25 | - Technology Consulting |
26 | - Oursourcing (highly qualified programmers and graphic designers) |
27 '---------------------------------------------------------------------------'
31 * PHPMailer - PHP POP Before SMTP Authentication Class
32 * NOTE: Designed for use with PHP version 5 and up
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 $
42 * POP Before SMTP Authentication Class
45 * Author: Richard Davey (rich@corephp.co.uk)
46 * Modifications: Andy Prevost
47 * License: LGPL, see PHPMailer License
49 * Specifically for PHPMailer to allow POP before SMTP authentication.
50 * Does not yet work with APOP - if you have an APOP account, contact Richard Davey
51 * and we can test changes to this script.
53 * This class is based on the structure of the SMTP class originally authored by Chris Ryan
55 * This class is rfc 1939 compliant and implements all the commands
56 * required for POP3 connection, authentication and disconnection.
59 * @author Richard Davey
67 public $POP3_PORT = 110;
73 public $POP3_TIMEOUT = 30;
76 * POP3 Carriage Return + Line Feed
79 public $CRLF = "\r\n";
82 * Displaying Debug warnings? (0 = now, 1+ = yes)
117 /////////////////////////////////////////////////
118 // PROPERTIES, PRIVATE AND PROTECTED
119 /////////////////////////////////////////////////
123 private $error; // Error log array
126 * Constructor, sets the initial values
130 public function __construct() {
132 $this->connected
= false;
137 * Combination of public events - connect, login, disconnect
139 * @param string $host
140 * @param integer $port
141 * @param integer $tval
142 * @param string $username
143 * @param string $password
145 public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
148 // If no port value is passed, retrieve it
149 if ($port == false) {
150 $this->port
= $this->POP3_PORT
;
155 // If no port value is passed, retrieve it
156 if ($tval == false) {
157 $this->tval
= $this->POP3_TIMEOUT
;
162 $this->do_debug
= $debug_level;
163 $this->username
= $username;
164 $this->password
= $password;
166 // Refresh the error log
170 $result = $this->Connect($this->host
, $this->port
, $this->tval
);
173 $login_result = $this->Login($this->username
, $this->password
);
183 // We need to disconnect regardless if the login succeeded
190 * Connect to the POP3 server
192 * @param string $host
193 * @param integer $port
194 * @param integer $tval
197 public function Connect ($host, $port = false, $tval = 30) {
198 // Are we already connected?
199 if ($this->connected
) {
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
208 set_error_handler(array(&$this, 'catchWarning'));
210 // Connect to the POP3 server
211 $this->pop_conn
= fsockopen($host, // POP3 Host
213 $errno, // Error Number
214 $errstr, // Error Message
215 $tval); // Timeout (seconds)
217 // Restore the error handler
218 restore_error_handler();
220 // Does the Error Log now contain anything?
221 if ($this->error
&& $this->do_debug
>= 1) {
222 $this->displayErrors();
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",
234 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(), '5.0.0', 'ge')) {
245 stream_set_timeout($this->pop_conn
, $tval, 0);
247 // Does not work on Windows
248 if (substr(PHP_OS
, 0, 3) !== 'WIN') {
249 socket_set_timeout($this->pop_conn
, $tval, 0);
253 // Get the POP3 server response
254 $pop3_response = $this->getResponse();
257 if ($this->checkResponse($pop3_response)) {
258 // The connection is established and the POP3 server is talking
259 $this->connected
= true;
266 * Login to the POP3 server (does not support APOP yet)
268 * @param string $username
269 * @param string $password
272 public function Login ($username = '', $password = '') {
273 if ($this->connected
== false) {
274 $this->error
= 'Not connected to POP3 server';
276 if ($this->do_debug
>= 1) {
277 $this->displayErrors();
281 if (empty($username)) {
282 $username = $this->username
;
285 if (empty($password)) {
286 $password = $this->password
;
289 $pop_username = "USER $username" . $this->CRLF
;
290 $pop_password = "PASS $password" . $this->CRLF
;
293 $this->sendString($pop_username);
294 $pop3_response = $this->getResponse();
296 if ($this->checkResponse($pop3_response)) {
298 $this->sendString($pop_password);
299 $pop3_response = $this->getResponse();
301 if ($this->checkResponse($pop3_response)) {
312 * Disconnect from the POP3 server
315 public function Disconnect () {
316 $this->sendString('QUIT');
318 fclose($this->pop_conn
);
321 /////////////////////////////////////////////////
323 /////////////////////////////////////////////////
326 * Get the socket response back.
327 * $size is the maximum number of bytes to retrieve
329 * @param integer $size
332 private function getResponse ($size = 128) {
333 $pop3_response = fgets($this->pop_conn
, $size);
335 return $pop3_response;
339 * Send a string down the open socket connection to the POP3 server
341 * @param string $string
344 private function sendString ($string) {
345 $bytes_sent = fwrite($this->pop_conn
, $string, strlen($string));
351 * Checks the POP3 server response for +OK or -ERR
353 * @param string $string
356 private function checkResponse ($string) {
357 if (substr($string, 0, 3) !== '+OK') {
358 $this->error
= array(
359 'error' => "Server reported an error: $string",
364 if ($this->do_debug
>= 1) {
365 $this->displayErrors();
376 * If debug is enabled, display the error message array
379 private function displayErrors () {
382 foreach ($this->error
as $single_error) {
383 print_r($single_error);
390 * Takes over from PHP for the socket warning handler
392 * @param integer $errno
393 * @param string $errstr
394 * @param string $errfile
395 * @param integer $errline
397 private function catchWarning ($errno, $errstr, $errfile, $errline) {
398 $this->error
[] = array(
399 'error' => "Connecting to the POP3 server raised a PHP warning: ",