Source for file class.pop3.php

Documentation is available at class.pop3.php

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

Documentation generated on Thu, 02 Apr 2009 21:19:49 -0400 by phpDocumentor 1.3.0RC3