class.pop3.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00048
00053
00054
00059
00060
00065
00066
00071
00072
00077
00078
00083
00084
00089
00090
00095
00096
00101
00102
00106
00107
00108
00116
00117
00118
00119
00120
00121
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 set_error_handler(array(&$this, 'catchWarning'));
00195
00196
00197 $this->pop_conn = fsockopen($host,
00198 $port,
00199 $errno,
00200 $errstr,
00201 $tval);
00202
00203
00204 restore_error_handler();
00205
00206
00207 if ($this->error && $this->do_debug >= 1) {
00208 $this->displayErrors();
00209 }
00210
00211
00212 if ($this->pop_conn == false) {
00213
00214 $this->error = array(
00215 'error' => "Failed to connect to server $host on port $port",
00216 'errno' => $errno,
00217 'errstr' => $errstr
00218 );
00219
00220 if ($this->do_debug >= 1) {
00221 $this->displayErrors();
00222 }
00223
00224 return false;
00225 }
00226
00227
00228
00229
00230 if (version_compare(phpversion(), '4.3.0', 'ge')) {
00231 stream_set_timeout($this->pop_conn, $tval, 0);
00232 } else {
00233
00234 if (substr(PHP_OS, 0, 3) !== 'WIN') {
00235 socket_set_timeout($this->pop_conn, $tval, 0);
00236 }
00237 }
00238
00239
00240 $pop3_response = $this->getResponse();
00241
00242
00243 if ($this->checkResponse($pop3_response)) {
00244
00245 $this->connected = true;
00246 return true;
00247 }
00248
00249 }
00250
00258 public function Login ($username = '', $password = '') {
00259 if ($this->connected == false) {
00260 $this->error = 'Not connected to POP3 server';
00261
00262 if ($this->do_debug >= 1) {
00263 $this->displayErrors();
00264 }
00265 }
00266
00267 if (empty($username)) {
00268 $username = $this->username;
00269 }
00270
00271 if (empty($password)) {
00272 $password = $this->password;
00273 }
00274
00275 $pop_username = "USER $username" . $this->CRLF;
00276 $pop_password = "PASS $password" . $this->CRLF;
00277
00278
00279 $this->sendString($pop_username);
00280 $pop3_response = $this->getResponse();
00281
00282 if ($this->checkResponse($pop3_response)) {
00283
00284 $this->sendString($pop_password);
00285 $pop3_response = $this->getResponse();
00286
00287 if ($this->checkResponse($pop3_response)) {
00288 return true;
00289 } else {
00290 return false;
00291 }
00292 } else {
00293 return false;
00294 }
00295 }
00296
00301 public function Disconnect () {
00302 $this->sendString('QUIT');
00303
00304 fclose($this->pop_conn);
00305 }
00306
00308
00310
00318 private public function getResponse ($size = 128) {
00319 $pop3_response = fgets($this->pop_conn, $size);
00320
00321 return $pop3_response;
00322 }
00323
00330 private function sendString ($string) {
00331 $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
00332
00333 return $bytes_sent;
00334 }
00335
00342 private function checkResponse ($string) {
00343 if (substr($string, 0, 3) !== '+OK') {
00344 $this->error = array(
00345 'error' => "Server reported an error: $string",
00346 'errno' => 0,
00347 'errstr' => ''
00348 );
00349
00350 if ($this->do_debug >= 1) {
00351 $this->displayErrors();
00352 }
00353
00354 return false;
00355 } else {
00356 return true;
00357 }
00358
00359 }
00360
00365 private function displayErrors () {
00366 echo '<pre>';
00367
00368 foreach ($this->error as $single_error) {
00369 print_r($single_error);
00370 }
00371
00372 echo '</pre>';
00373 }
00374
00383 private function catchWarning ($errno, $errstr, $errfile, $errline) {
00384 $this->error[] = array(
00385 'error' => "Connecting to the POP3 server raised a PHP warning: ",
00386 'errno' => $errno,
00387 'errstr' => $errstr
00388 );
00389 }
00390
00391
00392 }
00393 ?>