Pop3.class.php 10 KB

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