ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
POP3.php
Go to the documentation of this file.
1 <?php
21 namespace PHPMailer\PHPMailer;
22 
41 class POP3
42 {
48  const VERSION = '6.1.6';
49 
55  const DEFAULT_PORT = 110;
56 
62  const DEFAULT_TIMEOUT = 30;
63 
70  public $do_debug = 0;
71 
77  public $host;
78 
84  public $port;
85 
91  public $tval;
92 
98  public $username;
99 
105  public $password;
106 
112  protected $pop_conn;
113 
119  protected $connected = false;
120 
126  protected $errors = [];
127 
131  const LE = "\r\n";
132 
145  public static function popBeforeSmtp(
146  $host,
147  $port = false,
148  $timeout = false,
149  $username = '',
150  $password = '',
151  $debug_level = 0
152  ) {
153  $pop = new self();
154 
155  return $pop->authorise($host, $port, $timeout, $username, $password, $debug_level);
156  }
157 
172  public function authorise($host, $port = false, $timeout = false, $username = '', $password = '', $debug_level = 0)
173  {
174  $this->host = $host;
175  // If no port value provided, use default
176  if (false === $port) {
177  $this->port = static::DEFAULT_PORT;
178  } else {
179  $this->port = (int) $port;
180  }
181  // If no timeout value provided, use default
182  if (false === $timeout) {
183  $this->tval = static::DEFAULT_TIMEOUT;
184  } else {
185  $this->tval = (int) $timeout;
186  }
187  $this->do_debug = $debug_level;
188  $this->username = $username;
189  $this->password = $password;
190  // Reset the error log
191  $this->errors = [];
192  // connect
193  $result = $this->connect($this->host, $this->port, $this->tval);
194  if ($result) {
195  $login_result = $this->login($this->username, $this->password);
196  if ($login_result) {
197  $this->disconnect();
198 
199  return true;
200  }
201  }
202  // We need to disconnect regardless of whether the login succeeded
203  $this->disconnect();
204 
205  return false;
206  }
207 
217  public function connect($host, $port = false, $tval = 30)
218  {
219  // Are we already connected?
220  if ($this->connected) {
221  return true;
222  }
223 
224  //On Windows this will raise a PHP Warning error if the hostname doesn't exist.
225  //Rather than suppress it with @fsockopen, capture it cleanly instead
226  set_error_handler([$this, 'catchWarning']);
227 
228  if (false === $port) {
229  $port = static::DEFAULT_PORT;
230  }
231 
232  // connect to the POP3 server
233  $errno = 0;
234  $errstr = '';
235  $this->pop_conn = fsockopen(
236  $host, // POP3 Host
237  $port, // Port #
238  $errno, // Error Number
239  $errstr, // Error Message
240  $tval
241  ); // Timeout (seconds)
242  // Restore the error handler
243  restore_error_handler();
244 
245  // Did we connect?
246  if (false === $this->pop_conn) {
247  // It would appear not...
248  $this->setError(
249  "Failed to connect to server $host on port $port. errno: $errno; errstr: $errstr"
250  );
251 
252  return false;
253  }
254 
255  // Increase the stream time-out
256  stream_set_timeout($this->pop_conn, $tval, 0);
257 
258  // Get the POP3 server response
259  $pop3_response = $this->getResponse();
260  // Check for the +OK
261  if ($this->checkResponse($pop3_response)) {
262  // The connection is established and the POP3 server is talking
263  $this->connected = true;
264 
265  return true;
266  }
267 
268  return false;
269  }
270 
280  public function login($username = '', $password = '')
281  {
282  if (!$this->connected) {
283  $this->setError('Not connected to POP3 server');
284  }
285  if (empty($username)) {
287  }
288  if (empty($password)) {
290  }
291 
292  // Send the Username
293  $this->sendString("USER $username" . static::LE);
294  $pop3_response = $this->getResponse();
295  if ($this->checkResponse($pop3_response)) {
296  // Send the Password
297  $this->sendString("PASS $password" . static::LE);
298  $pop3_response = $this->getResponse();
299  if ($this->checkResponse($pop3_response)) {
300  return true;
301  }
302  }
303 
304  return false;
305  }
306 
310  public function disconnect()
311  {
312  $this->sendString('QUIT');
313  //The QUIT command may cause the daemon to exit, which will kill our connection
314  //So ignore errors here
315  try {
316  @fclose($this->pop_conn);
317  } catch (Exception $e) {
318  //Do nothing
319  }
320  }
321 
329  protected function getResponse($size = 128)
330  {
331  $response = fgets($this->pop_conn, $size);
332  if ($this->do_debug >= 1) {
333  echo 'Server -> Client: ', $response;
334  }
335 
336  return $response;
337  }
338 
346  protected function sendString($string)
347  {
348  if ($this->pop_conn) {
349  if ($this->do_debug >= 2) { //Show client messages when debug >= 2
350  echo 'Client -> Server: ', $string;
351  }
352 
353  return fwrite($this->pop_conn, $string, strlen($string));
354  }
355 
356  return 0;
357  }
358 
367  protected function checkResponse($string)
368  {
369  if (strpos($string, '+OK') !== 0) {
370  $this->setError("Server reported an error: $string");
371 
372  return false;
373  }
374 
375  return true;
376  }
377 
384  protected function setError($error)
385  {
386  $this->errors[] = $error;
387  if ($this->do_debug >= 1) {
388  echo '<pre>';
389  foreach ($this->errors as $e) {
390  print_r($e);
391  }
392  echo '</pre>';
393  }
394  }
395 
401  public function getErrors()
402  {
403  return $this->errors;
404  }
405 
414  protected function catchWarning($errno, $errstr, $errfile, $errline)
415  {
416  $this->setError(
417  'Connecting to the POP3 server raised a PHP warning:' .
418  "errno: $errno errstr: $errstr; errfile: $errfile; errline: $errline"
419  );
420  }
421 }
getResponse($size=128)
Get a response from the POP3 server.
Definition: POP3.php:329
$size
Definition: RandomTest.php:84
$result
connect($host, $port=false, $tval=30)
Connect to a POP3 server.
Definition: POP3.php:217
static popBeforeSmtp( $host, $port=false, $timeout=false, $username='', $password='', $debug_level=0)
Simple static wrapper for all-in-one POP before SMTP.
Definition: POP3.php:145
const LE
Line break constant.
Definition: POP3.php:131
authorise($host, $port=false, $timeout=false, $username='', $password='', $debug_level=0)
Authenticate with a POP3 server.
Definition: POP3.php:172
checkResponse($string)
Checks the POP3 server response.
Definition: POP3.php:367
sendString($string)
Send raw data to the POP3 server.
Definition: POP3.php:346
catchWarning($errno, $errstr, $errfile, $errline)
POP3 connection error handler.
Definition: POP3.php:414
setError($error)
Add an error to the internal error store.
Definition: POP3.php:384
PHPMailer POP-Before-SMTP Authentication Class.
Definition: POP3.php:41
getErrors()
Get an array of error messages, if any.
Definition: POP3.php:401
login($username='', $password='')
Log in to the POP3 server.
Definition: POP3.php:280
$response
disconnect()
Disconnect from the POP3 server.
Definition: POP3.php:310
Get an OAuth2 token from an OAuth2 provider.