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