template
[mailer.git] / 0.2.1 / inc / phpmailer / class.pop3.php
1 <?php
2 /*~ class.pop3.php
3 .---------------------------------------------------------------------------.
4 |  Software: PHPMailer - PHP email class                                    |
5 |   Version: 2.0.0 rc2                                                      |
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 '---------------------------------------------------------------------------'
26
27 /**
28  * POP Before SMTP Authentication Class
29  * Version 1.0
30  *
31  * Author: Richard Davey (rich@corephp.co.uk)
32  * License: LGPL, see PHPMailer License
33  *
34  * Specifically for PHPMailer to allow POP before SMTP authentication.
35  * Does not yet work with APOP - if you have an APOP account, contact me
36  * and we can test changes to this script.
37  *
38  * This class is based on the structure of the SMTP class by Chris Ryan
39  *
40  * This class is rfc 1939 compliant and implements all the commands
41  * required for POP3 connection, authentication and disconnection.
42  *
43  * @package PHPMailer
44  * @author Richard Davey
45  */
46
47 class POP3
48 {
49   /**
50    * Default POP3 port
51    * @var int
52    */
53   var $POP3_PORT = 110;
54
55   /**
56    * Default Timeout
57    * @var int
58    */
59   var $POP3_TIMEOUT = 30;
60
61   /**
62    * POP3 Carriage Return + Line Feed
63    * @var string
64    */
65   var $CRLF = "\r\n";
66
67   /**
68    * Displaying Debug warnings? (0 = now, 1+ = yes)
69    * @var int
70    */
71   var $do_debug = 2;
72
73   /**
74    * POP3 Mail Server
75    * @var string
76    */
77   var $host;
78
79   /**
80    * POP3 Port
81    * @var int
82    */
83   var $port;
84
85   /**
86    * POP3 Timeout Value
87    * @var int
88    */
89   var $tval;
90
91   /**
92    * POP3 Username
93    * @var string
94    */
95   var $username;
96
97   /**
98    * POP3 Password
99    * @var string
100    */
101   var $password;
102
103   /**#@+
104    * @access private
105    */
106   var $pop_conn;
107   var $connected;
108   var $error;     //  Error log array
109   /**#@-*/
110
111   /**
112    * Constructor, sets the initial values
113    *
114    * @return POP3
115    */
116   function POP3 ()
117     {
118       $this->pop_conn = 0;
119       $this->connected = false;
120       $this->error = null;
121     }
122
123   /**
124    * Combination of public events - connect, login, disconnect
125    *
126    * @param string $host
127    * @param integer $port
128    * @param integer $tval
129    * @param string $username
130    * @param string $password
131    */
132   function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0)
133   {
134     $this->host = $host;
135
136     //  If no port value is passed, retrieve it
137     if ($port == false)
138     {
139       $this->port = $this->POP3_PORT;
140     }
141     else
142     {
143       $this->port = $port;
144     }
145
146     //  If no port value is passed, retrieve it
147     if ($tval == false)
148     {
149       $this->tval = $this->POP3_TIMEOUT;
150     }
151     else
152     {
153       $this->tval = $tval;
154     }
155
156     $this->do_debug = $debug_level;
157     $this->username = $username;
158     $this->password = $password;
159
160     //  Refresh the error log
161       $this->error = null;
162
163       //  Connect
164     $result = $this->Connect($this->host, $this->port, $this->tval);
165
166     if ($result)
167     {
168       $login_result = $this->Login($this->username, $this->password);
169
170       if ($login_result)
171       {
172         $this->Disconnect();
173
174         return true;
175       }
176
177     }
178
179     //  We need to disconnect regardless if the login succeeded
180     $this->Disconnect();
181
182     return false;
183   }
184
185   /**
186    * Connect to the POP3 server
187    *
188    * @param string $host
189    * @param integer $port
190    * @param integer $tval
191    * @return boolean
192    */
193   function Connect ($host, $port = false, $tval = 30)
194     {
195     //  Are we already connected?
196     if ($this->connected)
197     {
198       return true;
199     }
200
201     /*
202       On Windows this will raise a PHP Warning error if the hostname doesn't exist.
203       Rather than supress it with @fsockopen, let's capture it cleanly instead
204     */
205
206     set_error_handler(array(&$this, 'catchWarning'));
207
208     //  Connect to the POP3 server
209     $this->pop_conn = fsockopen($host,    //  POP3 Host
210                   $port,    //  Port #
211                   $errno,   //  Error Number
212                   $errstr,  //  Error Message
213                   $tval);   //  Timeout (seconds)
214
215     //  Restore the error handler
216     restore_error_handler();
217
218     //  Does the Error Log now contain anything?
219     if ($this->error && $this->do_debug >= 1)
220     {
221         $this->displayErrors();
222     }
223
224     //  Did we connect?
225       if ($this->pop_conn == false)
226       {
227         //  It would appear not...
228         $this->error = array(
229           'error' => "Failed to connect to server $host on port $port",
230           'errno' => $errno,
231           'errstr' => $errstr
232         );
233
234         if ($this->do_debug >= 1)
235         {
236           $this->displayErrors();
237         }
238
239         return false;
240       }
241
242       //  Increase the stream time-out
243
244       //  Check for PHP 4.3.0 or later
245       if (version_compare(phpversion(), '4.3.0', 'ge'))
246       {
247         stream_set_timeout($this->pop_conn, $tval, 0);
248       }
249       else
250       {
251         //  Does not work on Windows
252         if (substr(PHP_OS, 0, 3) !== 'WIN')
253         {
254           socket_set_timeout($this->pop_conn, $tval, 0);
255         }
256       }
257
258     //  Get the POP3 server response
259       $pop3_response = $this->getResponse();
260
261       //  Check for the +OK
262       if ($this->checkResponse($pop3_response))
263       {
264       //  The connection is established and the POP3 server is talking
265       $this->connected = true;
266         return true;
267       }
268
269     }
270
271     /**
272      * Login to the POP3 server (does not support APOP yet)
273      *
274      * @param string $username
275      * @param string $password
276      * @return boolean
277      */
278     function Login ($username = '', $password = '')
279     {
280       if ($this->connected == false)
281       {
282         $this->error = 'Not connected to POP3 server';
283
284         if ($this->do_debug >= 1)
285         {
286           $this->displayErrors();
287         }
288       }
289
290       if (empty($username))
291       {
292         $username = $this->username;
293       }
294
295       if (empty($password))
296       {
297         $password = $this->password;
298       }
299
300     $pop_username = "USER $username" . $this->CRLF;
301     $pop_password = "PASS $password" . $this->CRLF;
302
303       //  Send the Username
304       $this->sendString($pop_username);
305       $pop3_response = $this->getResponse();
306
307       if ($this->checkResponse($pop3_response))
308       {
309         //  Send the Password
310         $this->sendString($pop_password);
311         $pop3_response = $this->getResponse();
312
313         if ($this->checkResponse($pop3_response))
314         {
315           return true;
316         }
317         else
318         {
319           return false;
320         }
321       }
322       else
323       {
324         return false;
325       }
326     }
327
328     /**
329      * Disconnect from the POP3 server
330      */
331     function Disconnect ()
332     {
333       $this->sendString('QUIT');
334
335       fclose($this->pop_conn);
336     }
337
338     /*
339       ---------------
340       Private Methods
341       ---------------
342     */
343
344     /**
345      * Get the socket response back.
346      * $size is the maximum number of bytes to retrieve
347      *
348      * @param integer $size
349      * @return string
350      */
351     function getResponse ($size = 128)
352     {
353       $pop3_response = fgets($this->pop_conn, $size);
354
355       return $pop3_response;
356     }
357
358     /**
359      * Send a string down the open socket connection to the POP3 server
360      *
361      * @param string $string
362      * @return integer
363      */
364     function sendString ($string)
365     {
366       $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
367
368       return $bytes_sent;
369
370     }
371
372     /**
373      * Checks the POP3 server response for +OK or -ERR
374      *
375      * @param string $string
376      * @return boolean
377      */
378     function checkResponse ($string)
379     {
380       if (substr($string, 0, 3) !== '+OK')
381       {
382         $this->error = array(
383           'error' => "Server reported an error: $string",
384           'errno' => 0,
385           'errstr' => ''
386         );
387
388         if ($this->do_debug >= 1)
389         {
390           $this->displayErrors();
391         }
392
393         return false;
394       }
395       else
396       {
397         return true;
398       }
399
400     }
401
402     /**
403      * If debug is enabled, display the error message array
404      *
405      */
406     function displayErrors ()
407     {
408       echo '<pre>';
409
410       foreach ($this->error as $single_error)
411     {
412         print_r($single_error);
413     }
414
415       echo '</pre>';
416     }
417
418   /**
419    * Takes over from PHP for the socket warning handler
420    *
421    * @param integer $errno
422    * @param string $errstr
423    * @param string $errfile
424    * @param integer $errline
425    */
426   function catchWarning ($errno, $errstr, $errfile, $errline)
427   {
428     $this->error[] = array(
429       'error' => "Connecting to the POP3 server raised a PHP warning: ",
430       'errno' => $errno,
431       'errstr' => $errstr
432     );
433   }
434
435   //  End of class
436 }
437 ?>