You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

421 lines
11 KiB

  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) <phpmailer@synchromedia.co.uk>
  9. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  10. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  11. * @author Brent R. Matzelle (original founder)
  12. * @copyright 2012 - 2019 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, implementing 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) <rich@corephp.co.uk>
  35. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  36. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  37. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  38. */
  39. class POP3
  40. {
  41. /**
  42. * The POP3 PHPMailer Version number.
  43. *
  44. * @var string
  45. */
  46. const VERSION = '6.1.4';
  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. $errno = 0;
  211. $errstr = '';
  212. $this->pop_conn = fsockopen(
  213. $host, // POP3 Host
  214. $port, // Port #
  215. $errno, // Error Number
  216. $errstr, // Error Message
  217. $tval
  218. ); // Timeout (seconds)
  219. // Restore the error handler
  220. restore_error_handler();
  221. // Did we connect?
  222. if (false === $this->pop_conn) {
  223. // It would appear not...
  224. $this->setError(
  225. "Failed to connect to server $host on port $port. errno: $errno; errstr: $errstr"
  226. );
  227. return false;
  228. }
  229. // Increase the stream time-out
  230. stream_set_timeout($this->pop_conn, $tval, 0);
  231. // Get the POP3 server response
  232. $pop3_response = $this->getResponse();
  233. // Check for the +OK
  234. if ($this->checkResponse($pop3_response)) {
  235. // The connection is established and the POP3 server is talking
  236. $this->connected = true;
  237. return true;
  238. }
  239. return false;
  240. }
  241. /**
  242. * Log in to the POP3 server.
  243. * Does not support APOP (RFC 2828, 4949).
  244. *
  245. * @param string $username
  246. * @param string $password
  247. *
  248. * @return bool
  249. */
  250. public function login($username = '', $password = '')
  251. {
  252. if (!$this->connected) {
  253. $this->setError('Not connected to POP3 server');
  254. }
  255. if (empty($username)) {
  256. $username = $this->username;
  257. }
  258. if (empty($password)) {
  259. $password = $this->password;
  260. }
  261. // Send the Username
  262. $this->sendString("USER $username" . static::LE);
  263. $pop3_response = $this->getResponse();
  264. if ($this->checkResponse($pop3_response)) {
  265. // Send the Password
  266. $this->sendString("PASS $password" . static::LE);
  267. $pop3_response = $this->getResponse();
  268. if ($this->checkResponse($pop3_response)) {
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. /**
  275. * Disconnect from the POP3 server.
  276. */
  277. public function disconnect()
  278. {
  279. $this->sendString('QUIT');
  280. //The QUIT command may cause the daemon to exit, which will kill our connection
  281. //So ignore errors here
  282. try {
  283. @fclose($this->pop_conn);
  284. } catch (Exception $e) {
  285. //Do nothing
  286. }
  287. }
  288. /**
  289. * Get a response from the POP3 server.
  290. *
  291. * @param int $size The maximum number of bytes to retrieve
  292. *
  293. * @return string
  294. */
  295. protected function getResponse($size = 128)
  296. {
  297. $response = fgets($this->pop_conn, $size);
  298. if ($this->do_debug >= 1) {
  299. echo 'Server -> Client: ', $response;
  300. }
  301. return $response;
  302. }
  303. /**
  304. * Send raw data to the POP3 server.
  305. *
  306. * @param string $string
  307. *
  308. * @return int
  309. */
  310. protected function sendString($string)
  311. {
  312. if ($this->pop_conn) {
  313. if ($this->do_debug >= 2) { //Show client messages when debug >= 2
  314. echo 'Client -> Server: ', $string;
  315. }
  316. return fwrite($this->pop_conn, $string, strlen($string));
  317. }
  318. return 0;
  319. }
  320. /**
  321. * Checks the POP3 server response.
  322. * Looks for for +OK or -ERR.
  323. *
  324. * @param string $string
  325. *
  326. * @return bool
  327. */
  328. protected function checkResponse($string)
  329. {
  330. if (strpos($string, '+OK') !== 0) {
  331. $this->setError("Server reported an error: $string");
  332. return false;
  333. }
  334. return true;
  335. }
  336. /**
  337. * Add an error to the internal error store.
  338. * Also display debug output if it's enabled.
  339. *
  340. * @param string $error
  341. */
  342. protected function setError($error)
  343. {
  344. $this->errors[] = $error;
  345. if ($this->do_debug >= 1) {
  346. echo '<pre>';
  347. foreach ($this->errors as $e) {
  348. print_r($e);
  349. }
  350. echo '</pre>';
  351. }
  352. }
  353. /**
  354. * Get an array of error messages, if any.
  355. *
  356. * @return array
  357. */
  358. public function getErrors()
  359. {
  360. return $this->errors;
  361. }
  362. /**
  363. * POP3 connection error handler.
  364. *
  365. * @param int $errno
  366. * @param string $errstr
  367. * @param string $errfile
  368. * @param int $errline
  369. */
  370. protected function catchWarning($errno, $errstr, $errfile, $errline)
  371. {
  372. $this->setError(
  373. 'Connecting to the POP3 server raised a PHP warning:' .
  374. "errno: $errno errstr: $errstr; errfile: $errfile; errline: $errline"
  375. );
  376. }
  377. }