POP3.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. * PHPMailer POP-Before-SMTP Authentication Class.
  4. * PHP Version 5.5.
  5. *
  6. * @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
  7. *
  8. * @author Marcus Bointon (Synchro/coolbru) <[email protected]>
  9. * @author Jim Jagielski (jimjag) <[email protected]>
  10. * @author Andy Prevost (codeworxtech) <[email protected]>
  11. * @author Brent R. Matzelle (original founder)
  12. * @copyright 2012 - 2017 Marcus Bointon
  13. * @copyright 2010 - 2012 Jim Jagielski
  14. * @copyright 2004 - 2009 Andy Prevost
  15. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  16. * @note This program is distributed in the hope that it will be useful - WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE.
  19. */
  20. namespace PHPMailer\PHPMailer;
  21. /**
  22. * PHPMailer POP-Before-SMTP Authentication Class.
  23. * Specifically for PHPMailer to use for RFC1939 POP-before-SMTP authentication.
  24. * 1) This class does not support APOP authentication.
  25. * 2) Opening and closing lots of POP3 connections can be quite slow. If you need
  26. * to send a batch of emails then just perform the authentication once at the start,
  27. * and then loop through your mail sending script. Providing this process doesn't
  28. * take longer than the verification period lasts on your POP3 server, you should be fine.
  29. * 3) This is really ancient technology; you should only need to use it to talk to very old systems.
  30. * 4) This POP3 class is deliberately lightweight and incomplete, and implements just
  31. * enough to do authentication.
  32. * If you want a more complete class there are other POP3 classes for PHP available.
  33. *
  34. * @author Richard Davey (original author) <[email protected]>
  35. * @author Marcus Bointon (Synchro/coolbru) <[email protected]>
  36. * @author Jim Jagielski (jimjag) <[email protected]>
  37. * @author Andy Prevost (codeworxtech) <[email protected]>
  38. */
  39. class POP3
  40. {
  41. /**
  42. * The POP3 PHPMailer Version number.
  43. *
  44. * @var string
  45. */
  46. const VERSION = '6.0.6';
  47. /**
  48. * Default POP3 port number.
  49. *
  50. * @var int
  51. */
  52. const DEFAULT_PORT = 110;
  53. /**
  54. * Default timeout in seconds.
  55. *
  56. * @var int
  57. */
  58. const DEFAULT_TIMEOUT = 30;
  59. /**
  60. * Debug display level.
  61. * Options: 0 = no, 1+ = yes.
  62. *
  63. * @var int
  64. */
  65. public $do_debug = 0;
  66. /**
  67. * POP3 mail server hostname.
  68. *
  69. * @var string
  70. */
  71. public $host;
  72. /**
  73. * POP3 port number.
  74. *
  75. * @var int
  76. */
  77. public $port;
  78. /**
  79. * POP3 Timeout Value in seconds.
  80. *
  81. * @var int
  82. */
  83. public $tval;
  84. /**
  85. * POP3 username.
  86. *
  87. * @var string
  88. */
  89. public $username;
  90. /**
  91. * POP3 password.
  92. *
  93. * @var string
  94. */
  95. public $password;
  96. /**
  97. * Resource handle for the POP3 connection socket.
  98. *
  99. * @var resource
  100. */
  101. protected $pop_conn;
  102. /**
  103. * Are we connected?
  104. *
  105. * @var bool
  106. */
  107. protected $connected = false;
  108. /**
  109. * Error container.
  110. *
  111. * @var array
  112. */
  113. protected $errors = [];
  114. /**
  115. * Line break constant.
  116. */
  117. const LE = "\r\n";
  118. /**
  119. * Simple static wrapper for all-in-one POP before SMTP.
  120. *
  121. * @param string $host The hostname to connect to
  122. * @param int|bool $port The port number to connect to
  123. * @param int|bool $timeout The timeout value
  124. * @param string $username
  125. * @param string $password
  126. * @param int $debug_level
  127. *
  128. * @return bool
  129. */
  130. public static function popBeforeSmtp(
  131. $host,
  132. $port = false,
  133. $timeout = false,
  134. $username = '',
  135. $password = '',
  136. $debug_level = 0
  137. ) {
  138. $pop = new self();
  139. return $pop->authorise($host, $port, $timeout, $username, $password, $debug_level);
  140. }
  141. /**
  142. * Authenticate with a POP3 server.
  143. * A connect, login, disconnect sequence
  144. * appropriate for POP-before SMTP authorisation.
  145. *
  146. * @param string $host The hostname to connect to
  147. * @param int|bool $port The port number to connect to
  148. * @param int|bool $timeout The timeout value
  149. * @param string $username
  150. * @param string $password
  151. * @param int $debug_level
  152. *
  153. * @return bool
  154. */
  155. public function authorise($host, $port = false, $timeout = false, $username = '', $password = '', $debug_level = 0)
  156. {
  157. $this->host = $host;
  158. // If no port value provided, use default
  159. if (false === $port) {
  160. $this->port = static::DEFAULT_PORT;
  161. } else {
  162. $this->port = (int) $port;
  163. }
  164. // If no timeout value provided, use default
  165. if (false === $timeout) {
  166. $this->tval = static::DEFAULT_TIMEOUT;
  167. } else {
  168. $this->tval = (int) $timeout;
  169. }
  170. $this->do_debug = $debug_level;
  171. $this->username = $username;
  172. $this->password = $password;
  173. // Reset the error log
  174. $this->errors = [];
  175. // connect
  176. $result = $this->connect($this->host, $this->port, $this->tval);
  177. if ($result) {
  178. $login_result = $this->login($this->username, $this->password);
  179. if ($login_result) {
  180. $this->disconnect();
  181. return true;
  182. }
  183. }
  184. // We need to disconnect regardless of whether the login succeeded
  185. $this->disconnect();
  186. return false;
  187. }
  188. /**
  189. * Connect to a POP3 server.
  190. *
  191. * @param string $host
  192. * @param int|bool $port
  193. * @param int $tval
  194. *
  195. * @return bool
  196. */
  197. public function connect($host, $port = false, $tval = 30)
  198. {
  199. // Are we already connected?
  200. if ($this->connected) {
  201. return true;
  202. }
  203. //On Windows this will raise a PHP Warning error if the hostname doesn't exist.
  204. //Rather than suppress it with @fsockopen, capture it cleanly instead
  205. set_error_handler([$this, 'catchWarning']);
  206. if (false === $port) {
  207. $port = static::DEFAULT_PORT;
  208. }
  209. // connect to the POP3 server
  210. $this->pop_conn = fsockopen(
  211. $host, // POP3 Host
  212. $port, // Port #
  213. $errno, // Error Number
  214. $errstr, // Error Message
  215. $tval
  216. ); // Timeout (seconds)
  217. // Restore the error handler
  218. restore_error_handler();
  219. // Did we connect?
  220. if (false === $this->pop_conn) {
  221. // It would appear not...
  222. $this->setError(
  223. "Failed to connect to server $host on port $port. errno: $errno; errstr: $errstr"
  224. );
  225. return false;
  226. }
  227. // Increase the stream time-out
  228. stream_set_timeout($this->pop_conn, $tval, 0);
  229. // Get the POP3 server response
  230. $pop3_response = $this->getResponse();
  231. // Check for the +OK
  232. if ($this->checkResponse($pop3_response)) {
  233. // The connection is established and the POP3 server is talking
  234. $this->connected = true;
  235. return true;
  236. }
  237. return false;
  238. }
  239. /**
  240. * Log in to the POP3 server.
  241. * Does not support APOP (RFC 2828, 4949).
  242. *
  243. * @param string $username
  244. * @param string $password
  245. *
  246. * @return bool
  247. */
  248. public function login($username = '', $password = '')
  249. {
  250. if (!$this->connected) {
  251. $this->setError('Not connected to POP3 server');
  252. }
  253. if (empty($username)) {
  254. $username = $this->username;
  255. }
  256. if (empty($password)) {
  257. $password = $this->password;
  258. }
  259. // Send the Username
  260. $this->sendString("USER $username" . static::LE);
  261. $pop3_response = $this->getResponse();
  262. if ($this->checkResponse($pop3_response)) {
  263. // Send the Password
  264. $this->sendString("PASS $password" . static::LE);
  265. $pop3_response = $this->getResponse();
  266. if ($this->checkResponse($pop3_response)) {
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272. /**
  273. * Disconnect from the POP3 server.
  274. */
  275. public function disconnect()
  276. {
  277. $this->sendString('QUIT');
  278. //The QUIT command may cause the daemon to exit, which will kill our connection
  279. //So ignore errors here
  280. try {
  281. @fclose($this->pop_conn);
  282. } catch (Exception $e) {
  283. //Do nothing
  284. }
  285. }
  286. /**
  287. * Get a response from the POP3 server.
  288. *
  289. * @param int $size The maximum number of bytes to retrieve
  290. *
  291. * @return string
  292. */
  293. protected function getResponse($size = 128)
  294. {
  295. $response = fgets($this->pop_conn, $size);
  296. if ($this->do_debug >= 1) {
  297. echo 'Server -> Client: ', $response;
  298. }
  299. return $response;
  300. }
  301. /**
  302. * Send raw data to the POP3 server.
  303. *
  304. * @param string $string
  305. *
  306. * @return int
  307. */
  308. protected function sendString($string)
  309. {
  310. if ($this->pop_conn) {
  311. if ($this->do_debug >= 2) { //Show client messages when debug >= 2
  312. echo 'Client -> Server: ', $string;
  313. }
  314. return fwrite($this->pop_conn, $string, strlen($string));
  315. }
  316. return 0;
  317. }
  318. /**
  319. * Checks the POP3 server response.
  320. * Looks for for +OK or -ERR.
  321. *
  322. * @param string $string
  323. *
  324. * @return bool
  325. */
  326. protected function checkResponse($string)
  327. {
  328. if (substr($string, 0, 3) !== '+OK') {
  329. $this->setError("Server reported an error: $string");
  330. return false;
  331. }
  332. return true;
  333. }
  334. /**
  335. * Add an error to the internal error store.
  336. * Also display debug output if it's enabled.
  337. *
  338. * @param string $error
  339. */
  340. protected function setError($error)
  341. {
  342. $this->errors[] = $error;
  343. if ($this->do_debug >= 1) {
  344. echo '<pre>';
  345. foreach ($this->errors as $e) {
  346. print_r($e);
  347. }
  348. echo '</pre>';
  349. }
  350. }
  351. /**
  352. * Get an array of error messages, if any.
  353. *
  354. * @return array
  355. */
  356. public function getErrors()
  357. {
  358. return $this->errors;
  359. }
  360. /**
  361. * POP3 connection error handler.
  362. *
  363. * @param int $errno
  364. * @param string $errstr
  365. * @param string $errfile
  366. * @param int $errline
  367. */
  368. protected function catchWarning($errno, $errstr, $errfile, $errline)
  369. {
  370. $this->setError(
  371. 'Connecting to the POP3 server raised a PHP warning:' .
  372. "errno: $errno errstr: $errstr; errfile: $errfile; errline: $errline"
  373. );
  374. }
  375. }