class.pop3.php 9.8 KB

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