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 * Modifications: Andy Prevost
33 * License: LGPL, see PHPMailer License
35 * Specifically for PHPMailer to allow POP before SMTP authentication.
36 * Does not yet work with APOP - if you have an APOP account, contact me
37 * and we can test changes to this script.
39 * This class is based on the structure of the SMTP class by Chris Ryan
41 * This class is rfc 1939 compliant and implements all the commands
42 * required for POP3 connection, authentication and disconnection.
45 * @author Richard Davey
53 public $POP3_PORT = 110;
59 public $POP3_TIMEOUT = 30;
62 * POP3 Carriage Return + Line Feed
65 public $CRLF = "\r\n";
68 * Displaying Debug warnings? (0 = now, 1+ = yes)
108 private $error; // Error log array
112 * Constructor, sets the initial values
116 public function __construct() {
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 public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
134 // If no port value is passed, retrieve it
135 if ($port == false) {
136 $this->port = $this->POP3_PORT;
141 // If no port value is passed, retrieve it
142 if ($tval == false) {
143 $this->tval = $this->POP3_TIMEOUT;
148 $this->do_debug = $debug_level;
149 $this->username = $username;
150 $this->password = $password;
152 // Refresh the error log
156 $result = $this->Connect($this->host, $this->port, $this->tval);
159 $login_result = $this->Login($this->username, $this->password);
169 // We need to disconnect regardless if the login succeeded
176 * Connect to the POP3 server
178 * @param string $host
179 * @param integer $port
180 * @param integer $tval
183 public function Connect ($host, $port = false, $tval = 30) {
184 // Are we already connected?
185 if ($this->connected) {
190 On Windows this will raise a PHP Warning error if the hostname doesn't exist.
191 Rather than supress it with @fsockopen, let's capture it cleanly instead
194 set_error_handler(array(&$this, 'catchWarning'));
196 // Connect to the POP3 server
197 $this->pop_conn = fsockopen($host, // POP3 Host
199 $errno, // Error Number
200 $errstr, // Error Message
201 $tval); // Timeout (seconds)
203 // Restore the error handler
204 restore_error_handler();
206 // Does the Error Log now contain anything?
207 if ($this->error && $this->do_debug >= 1) {
208 $this->displayErrors();
212 if ($this->pop_conn == false) {
213 // It would appear not...
214 $this->error = array(
215 'error' => "Failed to connect to server $host on port $port",
220 if ($this->do_debug >= 1) {
221 $this->displayErrors();
227 // Increase the stream time-out
229 // Check for PHP 4.3.0 or later
230 if (version_compare(phpversion(), '4.3.0', 'ge')) {
231 stream_set_timeout($this->pop_conn, $tval, 0);
233 // Does not work on Windows
234 if (substr(PHP_OS, 0, 3) !== 'WIN') {
235 socket_set_timeout($this->pop_conn, $tval, 0);
239 // Get the POP3 server response
240 $pop3_response = $this->getResponse();
243 if ($this->checkResponse($pop3_response)) {
244 // The connection is established and the POP3 server is talking
245 $this->connected = true;
252 * Login to the POP3 server (does not support APOP yet)
254 * @param string $username
255 * @param string $password
258 public function Login ($username = '', $password = '') {
259 if ($this->connected == false) {
260 $this->error = 'Not connected to POP3 server';
262 if ($this->do_debug >= 1) {
263 $this->displayErrors();
267 if (empty($username)) {
268 $username = $this->username;
271 if (empty($password)) {
272 $password = $this->password;
275 $pop_username = "USER $username" . $this->CRLF;
276 $pop_password = "PASS $password" . $this->CRLF;
279 $this->sendString($pop_username);
280 $pop3_response = $this->getResponse();
282 if ($this->checkResponse($pop3_response)) {
284 $this->sendString($pop_password);
285 $pop3_response = $this->getResponse();
287 if ($this->checkResponse($pop3_response)) {
298 * Disconnect from the POP3 server
301 public function Disconnect () {
302 $this->sendString('QUIT');
304 fclose($this->pop_conn);
307 /////////////////////////////////////////////////
309 /////////////////////////////////////////////////
312 * Get the socket response back.
313 * $size is the maximum number of bytes to retrieve
315 * @param integer $size
318 private public function getResponse ($size = 128) {
319 $pop3_response = fgets($this->pop_conn, $size);
321 return $pop3_response;
325 * Send a string down the open socket connection to the POP3 server
327 * @param string $string
330 private function sendString ($string) {
331 $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
337 * Checks the POP3 server response for +OK or -ERR
339 * @param string $string
342 private function checkResponse ($string) {
343 if (substr($string, 0, 3) !== '+OK') {
344 $this->error = array(
345 'error' => "Server reported an error: $string",
350 if ($this->do_debug >= 1) {
351 $this->displayErrors();
362 * If debug is enabled, display the error message array
365 private function displayErrors () {
368 foreach ($this->error as $single_error) {
369 print_r($single_error);
376 * Takes over from PHP for the socket warning handler
378 * @param integer $errno
379 * @param string $errstr
380 * @param string $errfile
381 * @param integer $errline
383 private function catchWarning ($errno, $errstr, $errfile, $errline) {
384 $this->error[] = array(
385 'error' => "Connecting to the POP3 server raised a PHP warning: ",