class.pop3.php Quellcode

class.pop3.php
gehe zur Dokumentation dieser Datei
1 <?php
30 class POP3
31 {
37  public $Version = '5.2.9';
38 
44  public $POP3_PORT = 110;
45 
51  public $POP3_TIMEOUT = 30;
52 
59  public $CRLF = "\r\n";
60 
67  public $do_debug = 0;
68 
74  public $host;
75 
81  public $port;
82 
88  public $tval;
89 
95  public $username;
96 
102  public $password;
103 
109  private $pop_conn;
110 
116  private $connected = false;
117 
123  private $errors = array();
124 
128  const CRLF = "\r\n";
129 
140  public static function popBeforeSmtp(
141  $host,
142  $port = false,
143  $tval = false,
144  $username = '',
145  $password = '',
146  $debug_level = 0
147  ) {
148  $pop = new POP3;
149  return $pop->authorise($host, $port, $tval, $username, $password, $debug_level);
150  }
151 
165  public function authorise($host, $port = false, $timeout = false, $username = '', $password = '', $debug_level = 0)
166  {
167  $this->host = $host;
168  // If no port value provided, use default
169  if ($port === false) {
170  $this->port = $this->POP3_PORT;
171  } else {
172  $this->port = (integer)$port;
173  }
174  // If no timeout value provided, use default
175  if ($timeout === false) {
176  $this->tval = $this->POP3_TIMEOUT;
177  } else {
178  $this->tval = (integer)$timeout;
179  }
180  $this->do_debug = $debug_level;
181  $this->username = $username;
182  $this->password = $password;
183  // Reset the error log
184  $this->errors = array();
185  // connect
186  $result = $this->connect($this->host, $this->port, $this->tval);
187  if ($result) {
188  $login_result = $this->login($this->username, $this->password);
189  if ($login_result) {
190  $this->disconnect();
191  return true;
192  }
193  }
194  // We need to disconnect regardless of whether the login succeeded
195  $this->disconnect();
196  return false;
197  }
198 
207  public function connect($host, $port = false, $tval = 30)
208  {
209  // Are we already connected?
210  if ($this->connected) {
211  return true;
212  }
213 
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'));
217 
218  if ($port === false) {
220  }
221 
222  // connect to the POP3 server
223  $this->pop_conn = fsockopen(
224  $host, // POP3 Host
225  $port, // Port #
226  $errno, // Error Number
227  $errstr, // Error Message
228  $tval
229  ); // Timeout (seconds)
230  // Restore the error handler
231  restore_error_handler();
232 
233  // Did we connect?
234  if ($this->pop_conn === false) {
235  // It would appear not...
236  $this->setError(array(
237  'error' => "Failed to connect to server $host on port $port",
238  'errno' => $errno,
239  'errstr' => $errstr
240  ));
241  return false;
242  }
243 
244  // Increase the stream time-out
245  stream_set_timeout($this->pop_conn, $tval, 0);
246 
247  // Get the POP3 server response
248  $pop3_response = $this->getResponse();
249  // Check for the +OK
250  if ($this->checkResponse($pop3_response)) {
251  // The connection is established and the POP3 server is talking
252  $this->connected = true;
253  return true;
254  }
255  return false;
256  }
257 
266  public function login($username = '', $password = '')
267  {
268  if (!$this->connected) {
269  $this->setError('Not connected to POP3 server');
270  }
271  if (empty($username)) {
273  }
274  if (empty($password)) {
276  }
277 
278  // Send the Username
279  $this->sendString("USER $username" . self::CRLF);
280  $pop3_response = $this->getResponse();
281  if ($this->checkResponse($pop3_response)) {
282  // Send the Password
283  $this->sendString("PASS $password" . self::CRLF);
284  $pop3_response = $this->getResponse();
285  if ($this->checkResponse($pop3_response)) {
286  return true;
287  }
288  }
289  return false;
290  }
291 
296  public function disconnect()
297  {
298  $this->sendString('QUIT');
299  //The QUIT command may cause the daemon to exit, which will kill our connection
300  //So ignore errors here
301  try {
302  @fclose($this->pop_conn);
303  } catch (Exception $e) {
304  //Do nothing
305  };
306  }
307 
315  private function getResponse($size = 128)
316  {
317  $response = fgets($this->pop_conn, $size);
318  if ($this->do_debug >= 1) {
319  echo "Server -> Client: $response";
320  }
321  return $response;
322  }
323 
330  private function sendString($string)
331  {
332  if ($this->pop_conn) {
333  if ($this->do_debug >= 2) { //Show client messages when debug >= 2
334  echo "Client -> Server: $string";
335  }
336  return fwrite($this->pop_conn, $string, strlen($string));
337  }
338  return 0;
339  }
340 
348  private function checkResponse($string)
349  {
350  if (substr($string, 0, 3) !== '+OK') {
351  $this->setError(array(
352  'error' => "Server reported an error: $string",
353  'errno' => 0,
354  'errstr' => ''
355  ));
356  return false;
357  } else {
358  return true;
359  }
360  }
361 
367  private function setError($error)
368  {
369  $this->errors[] = $error;
370  if ($this->do_debug >= 1) {
371  echo '<pre>';
372  foreach ($this->errors as $error) {
373  print_r($error);
374  }
375  echo '</pre>';
376  }
377  }
378 
387  private function catchWarning($errno, $errstr, $errfile, $errline)
388  {
389  $this->setError(array(
390  'error' => "Connecting to the POP3 server raised a PHP warning: ",
391  'errno' => $errno,
392  'errstr' => $errstr,
393  'errfile' => $errfile,
394  'errline' => $errline
395  ));
396  }
397 }




Korrekturen, Hinweise und Ergänzungen

Bitte scheuen Sie sich nicht und melden Sie, was auf dieser Seite sachlich falsch oder irreführend ist, was ergänzt werden sollte, was fehlt usw. Dazu bitte oben aus dem Menü Seite den Eintrag Support Forum wählen. Es ist eine kostenlose Anmeldung erforderlich, um Anmerkungen zu posten. Unpassende Postings, Spam usw. werden kommentarlos entfernt.