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

Documentation generated on Thu, 02 Apr 2009 17:51:27 -0400 by phpDocumentor 1.3.0RC3