1
0

class.pop3.php 10 KB

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