3 * PHPMailer POP-Before-SMTP Authentication Class.
6 * @link https://github.com/PHPMailer/PHPMailer/
7 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
8 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
9 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
10 * @author Brent R. Matzelle (original founder)
11 * @copyright 2012 - 2014 Marcus Bointon
12 * @copyright 2010 - 2012 Jim Jagielski
13 * @copyright 2004 - 2009 Andy Prevost
14 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
15 * @note This program is distributed in the hope that it will be useful - WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE.
21 * PHPMailer POP-Before-SMTP Authentication Class.
22 * Specifically for PHPMailer to use for RFC1939 POP-before-SMTP authentication.
23 * Does not support APOP.
25 * @author Richard Davey (original author) <rich@corephp.co.uk>
26 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
27 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
28 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
33 * The POP3 PHPMailer Version number.
37 public $Version = '5.2.21';
40 * Default POP3 port number.
44 public $POP3_PORT = 110;
47 * Default timeout in seconds.
51 public $POP3_TIMEOUT = 30;
54 * POP3 Carriage Return + Line Feed.
57 * @deprecated Use the constant instead
59 public $CRLF = "\r\n";
62 * Debug display level.
63 * Options: 0 = no, 1+ = yes
70 * POP3 mail server hostname.
84 * POP3 Timeout Value in seconds.
105 * Resource handle for the POP3 connection socket.
116 protected $connected = false;
123 protected $errors = array();
126 * Line break constant
131 * Simple static wrapper for all-in-one POP before SMTP
133 * @param integer|boolean $port The port number to connect to
134 * @param integer|boolean $timeout The timeout value
135 * @param string $username
136 * @param string $password
137 * @param integer $debug_level
140 public static function popBeforeSmtp(
149 return $pop->authorise($host, $port, $timeout, $username, $password, $debug_level);
153 * Authenticate with a POP3 server.
154 * A connect, login, disconnect sequence
155 * appropriate for POP-before SMTP authorisation.
157 * @param string $host The hostname to connect to
158 * @param integer|boolean $port The port number to connect to
159 * @param integer|boolean $timeout The timeout value
160 * @param string $username
161 * @param string $password
162 * @param integer $debug_level
165 public function authorise($host, $port = false, $timeout = false, $username = '', $password = '', $debug_level = 0)
168 // If no port value provided, use default
169 if (false === $port) {
170 $this->port
= $this->POP3_PORT
;
172 $this->port
= (integer)$port;
174 // If no timeout value provided, use default
175 if (false === $timeout) {
176 $this->tval
= $this->POP3_TIMEOUT
;
178 $this->tval
= (integer)$timeout;
180 $this->do_debug
= $debug_level;
181 $this->username
= $username;
182 $this->password
= $password;
183 // Reset the error log
184 $this->errors
= array();
186 $result = $this->connect($this->host
, $this->port
, $this->tval
);
188 $login_result = $this->login($this->username
, $this->password
);
194 // We need to disconnect regardless of whether the login succeeded
200 * Connect to a POP3 server.
202 * @param string $host
203 * @param integer|boolean $port
204 * @param integer $tval
207 public function connect($host, $port = false, $tval = 30)
209 // Are we already connected?
210 if ($this->connected
) {
214 //On Windows this will raise a PHP Warning error if the hostname doesn't exist.
215 //Rather than suppress it with @fsockopen, capture it cleanly instead
216 set_error_handler(array($this, 'catchWarning'));
218 if (false === $port) {
219 $port = $this->POP3_PORT
;
222 // connect to the POP3 server
223 $this->pop_conn
= fsockopen(
226 $errno, // Error Number
227 $errstr, // Error Message
229 ); // Timeout (seconds)
230 // Restore the error handler
231 restore_error_handler();
234 if (false === $this->pop_conn
) {
235 // It would appear not...
236 $this->setError(array(
237 'error' => "Failed to connect to server $host on port $port",
244 // Increase the stream time-out
245 stream_set_timeout($this->pop_conn
, $tval, 0);
247 // Get the POP3 server response
248 $pop3_response = $this->getResponse();
250 if ($this->checkResponse($pop3_response)) {
251 // The connection is established and the POP3 server is talking
252 $this->connected
= true;
259 * Log in to the POP3 server.
260 * Does not support APOP (RFC 2828, 4949).
262 * @param string $username
263 * @param string $password
266 public function login($username = '', $password = '')
268 if (!$this->connected
) {
269 $this->setError('Not connected to POP3 server');
271 if (empty($username)) {
272 $username = $this->username
;
274 if (empty($password)) {
275 $password = $this->password
;
279 $this->sendString("USER $username" . self
::CRLF
);
280 $pop3_response = $this->getResponse();
281 if ($this->checkResponse($pop3_response)) {
283 $this->sendString("PASS $password" . self
::CRLF
);
284 $pop3_response = $this->getResponse();
285 if ($this->checkResponse($pop3_response)) {
293 * Disconnect from the POP3 server.
296 public function disconnect()
298 $this->sendString('QUIT');
299 //The QUIT command may cause the daemon to exit, which will kill our connection
300 //So ignore errors here
302 @fclose
($this->pop_conn
);
303 } catch (Exception
$e) {
309 * Get a response from the POP3 server.
310 * $size is the maximum number of bytes to retrieve
311 * @param integer $size
315 protected function getResponse($size = 128)
317 $response = fgets($this->pop_conn
, $size);
318 if ($this->do_debug
>= 1) {
319 echo "Server -> Client: $response";
325 * Send raw data to the POP3 server.
326 * @param string $string
330 protected function sendString($string)
332 if ($this->pop_conn
) {
333 if ($this->do_debug
>= 2) { //Show client messages when debug >= 2
334 echo "Client -> Server: $string";
336 return fwrite($this->pop_conn
, $string, strlen($string));
342 * Checks the POP3 server response.
343 * Looks for for +OK or -ERR.
344 * @param string $string
348 protected function checkResponse($string)
350 if (substr($string, 0, 3) !== '+OK') {
351 $this->setError(array(
352 'error' => "Server reported an error: $string",
363 * Add an error to the internal error store.
364 * Also display debug output if it's enabled.
368 protected function setError($error)
370 $this->errors
[] = $error;
371 if ($this->do_debug
>= 1) {
373 foreach ($this->errors
as $error) {
381 * Get an array of error messages, if any.
384 public function getErrors()
386 return $this->errors
;
390 * POP3 connection error handler.
391 * @param integer $errno
392 * @param string $errstr
393 * @param string $errfile
394 * @param integer $errline
397 protected function catchWarning($errno, $errstr, $errfile, $errline)
399 $this->setError(array(
400 'error' => "Connecting to the POP3 server raised a PHP warning: ",
403 'errfile' => $errfile,
404 'errline' => $errline