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 )
  221. {
  222. if ($this->connected == false)
  223. {
  224. $this->error = 'Not connected to POP3 server';
  225.  
  226. if ($this->do_debug >= 1)
  227. {
  228. $this->displayErrors();
  229. }
  230. }
  231.  
  232. if (empty($username))
  233. {
  234. $username = $this->username;
  235. }
  236.  
  237. if (empty($password))
  238. {
  239. $password = $this->password;
  240. }
  241.  
  242. $pop_username = "USER $username" . $this->CRLF;
  243. $pop_password = "PASS $password" . $this->CRLF;
  244.  
  245. // Send the Username
  246. $this->sendString($pop_username);
  247. $pop3_response = $this->getResponse();
  248.  
  249. if ($this->checkResponse($pop3_response))
  250. {
  251. // Send the Password
  252. $this->sendString($pop_password);
  253. $pop3_response = $this->getResponse();
  254.  
  255. if ($this->checkResponse($pop3_response))
  256. {
  257. return true;
  258. }
  259. else
  260. {
  261. return false;
  262. }
  263. }
  264. else
  265. {
  266. return false;
  267. }
  268. }
  269.  
  270. /**
  271. * Disconnect from the POP3 server
  272. */
  273. function Disconnect ()
  274. {
  275. $this->sendString('QUIT');
  276.  
  277. fclose($this->pop_conn);
  278. }
  279.  
  280. /*
  281. ---------------
  282. Private Methods
  283. ---------------
  284. */
  285.  
  286. /**
  287. * Get the socket response back.
  288. * $size is the maximum numbe-sym">($this->error && $this->do_debug >= 1)
  289. {
  290. $this->displayErrors();
  291. }
  292.  
  293. // Did we connect?
  294. if ($this->pop_conn == false)
  295. {
  296. // It would appear not...
  297. $this->error = array(
  298. 'error' => "Failed to connect to server $host on port $port",
  299. 'errno' => $errno,
  300. 'errstr' => $errstr
  301. );
  302.  
  303. if ($this->do_debug >= 1)
  304. {
  305. $this->displayErrors();
  306. }
  307.  
  308. return false;
  309. }
  310.  
  311. // Increase the stream time-out
  312.  
  313. // Check for PHP 4.3.0 or later
  314. if (version_compare(phpversion(), '4.3.0', 'ge'))
  315. {
  316. stream_set_timeout($this->pop_conn, $tval, 0);
  317. }
  318. else
  319. {
  320. // Does not work on Windows
  321. if (substr(PHP_OS, 0, 3) !== 'WIN')
  322. {
  323. socket_set_timeout($this->pop_conn, $tval, 0);
  324. }
  325. }
  326.  
  327. // Get the POP3 server response
  328. $pop3_response = $this->getResponse();
  329.  
  330. // Check for the +OK
  331. if ($this->checkResponse($pop3_response))
  332. {
  333. // The connection is established and the POP3 server is talking
  334. $this->connected = true;
  335. return true;
  336. }
  337.  
  338. }
  339.  
  340. /**
  341. * Login to the POP3 server (does not support APOP yet)
  342. *
  343. * @param string $username
  344. * @param string $password
  345. * @return boolean
  346. */
  347. function Login ($username = '', $password = '' *
  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. }
  438. ?>

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