PageRenderTime 28ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/api/vendor/phpmailer/phpmailer/class.smtp.php

https://gitlab.com/x33n/respond
PHP | 935 lines | 469 code | 65 blank | 401 comment | 63 complexity | 921778c45350c7a45288eb1a674bfc2a MD5 | raw file
  1. <?php
  2. /**
  3. * PHPMailer RFC821 SMTP email transport class.
  4. * PHP Version 5
  5. * @package PHPMailer
  6. * @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
  7. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  8. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  9. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  10. * @author Brent R. Matzelle (original founder)
  11. * @copyright 2014 Marcus Bointon
  12. * @copyright 2010 - 2012 Jim Jagielski
  13. * @copyright 2004 - 2009 Andy Prevost
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. * @note This program is distributed in the hope that it will be useful - WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. /**
  20. * PHPMailer RFC821 SMTP email transport class.
  21. * Implements RFC 821 SMTP commands and provides some utility methods for sending mail to an SMTP server.
  22. * @package PHPMailer
  23. * @author Chris Ryan <unknown@example.com>
  24. * @author Marcus Bointon <phpmailer@synchromedia.co.uk>
  25. */
  26. class SMTP
  27. {
  28. /**
  29. * The PHPMailer SMTP version number.
  30. * @type string
  31. */
  32. const VERSION = '5.2.8';
  33. /**
  34. * SMTP line break constant.
  35. * @type string
  36. */
  37. const CRLF = "\r\n";
  38. /**
  39. * The SMTP port to use if one is not specified.
  40. * @type int
  41. */
  42. const DEFAULT_SMTP_PORT = 25;
  43. /**
  44. * The maximum line length allowed by RFC 2822 section 2.1.1
  45. * @type int
  46. */
  47. const MAX_LINE_LENGTH = 998;
  48. /**
  49. * The PHPMailer SMTP Version number.
  50. * @type string
  51. * @deprecated Use the constant instead
  52. * @see SMTP::VERSION
  53. */
  54. public $Version = '5.2.8';
  55. /**
  56. * SMTP server port number.
  57. * @type int
  58. * @deprecated This is only ever used as a default value, so use the constant instead
  59. * @see SMTP::DEFAULT_SMTP_PORT
  60. */
  61. public $SMTP_PORT = 25;
  62. /**
  63. * SMTP reply line ending.
  64. * @type string
  65. * @deprecated Use the constant instead
  66. * @see SMTP::CRLF
  67. */
  68. public $CRLF = "\r\n";
  69. /**
  70. * Debug output level.
  71. * Options:
  72. * * `0` No output
  73. * * `1` Commands
  74. * * `2` Data and commands
  75. * * `3` As 2 plus connection status
  76. * * `4` Low-level data output
  77. * @type int
  78. */
  79. public $do_debug = 0;
  80. /**
  81. * How to handle debug output.
  82. * Options:
  83. * * `echo` Output plain-text as-is, appropriate for CLI
  84. * * `html` Output escaped, line breaks converted to <br>, appropriate for browser output
  85. * * `error_log` Output to error log as configured in php.ini
  86. * @type string
  87. */
  88. public $Debugoutput = 'echo';
  89. /**
  90. * Whether to use VERP.
  91. * @link http://en.wikipedia.org/wiki/Variable_envelope_return_path
  92. * @link http://www.postfix.org/VERP_README.html Info on VERP
  93. * @type bool
  94. */
  95. public $do_verp = false;
  96. /**
  97. * The timeout value for connection, in seconds.
  98. * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2
  99. * This needs to be quite high to function correctly with hosts using greetdelay as an anti-spam measure.
  100. * @link http://tools.ietf.org/html/rfc2821#section-4.5.3.2
  101. * @type int
  102. */
  103. public $Timeout = 300;
  104. /**
  105. * The SMTP timelimit value for reads, in seconds.
  106. * @type int
  107. */
  108. public $Timelimit = 30;
  109. /**
  110. * The socket for the server connection.
  111. * @type resource
  112. */
  113. protected $smtp_conn;
  114. /**
  115. * Error message, if any, for the last call.
  116. * @type string
  117. */
  118. protected $error = '';
  119. /**
  120. * The reply the server sent to us for HELO.
  121. * @type string
  122. */
  123. protected $helo_rply = '';
  124. /**
  125. * The most recent reply received from the server.
  126. * @type string
  127. */
  128. protected $last_reply = '';
  129. /**
  130. * Constructor.
  131. * @access public
  132. */
  133. public function __construct()
  134. {
  135. $this->smtp_conn = 0;
  136. $this->error = null;
  137. $this->helo_rply = null;
  138. $this->do_debug = 0;
  139. }
  140. /**
  141. * Output debugging info via a user-selected method.
  142. * @param string $str Debug string to output
  143. * @return void
  144. */
  145. protected function edebug($str)
  146. {
  147. switch ($this->Debugoutput) {
  148. case 'error_log':
  149. //Don't output, just log
  150. error_log($str);
  151. break;
  152. case 'html':
  153. //Cleans up output a bit for a better looking, HTML-safe output
  154. echo htmlentities(
  155. preg_replace('/[\r\n]+/', '', $str),
  156. ENT_QUOTES,
  157. 'UTF-8'
  158. )
  159. . "<br>\n";
  160. break;
  161. case 'echo':
  162. default:
  163. echo gmdate('Y-m-d H:i:s')."\t".trim($str)."\n";
  164. }
  165. }
  166. /**
  167. * Connect to an SMTP server.
  168. * @param string $host SMTP server IP or host name
  169. * @param int $port The port number to connect to
  170. * @param int $timeout How long to wait for the connection to open
  171. * @param array $options An array of options for stream_context_create()
  172. * @access public
  173. * @return bool
  174. */
  175. public function connect($host, $port = null, $timeout = 30, $options = array())
  176. {
  177. // Clear errors to avoid confusion
  178. $this->error = null;
  179. // Make sure we are __not__ connected
  180. if ($this->connected()) {
  181. // Already connected, generate error
  182. $this->error = array('error' => 'Already connected to a server');
  183. return false;
  184. }
  185. if (empty($port)) {
  186. $port = self::DEFAULT_SMTP_PORT;
  187. }
  188. // Connect to the SMTP server
  189. if ($this->do_debug >= 3) {
  190. $this->edebug('Connection: opening');
  191. }
  192. $errno = 0;
  193. $errstr = '';
  194. $socket_context = stream_context_create($options);
  195. //Suppress errors; connection failures are handled at a higher level
  196. $this->smtp_conn = @stream_socket_client(
  197. $host . ":" . $port,
  198. $errno,
  199. $errstr,
  200. $timeout,
  201. STREAM_CLIENT_CONNECT,
  202. $socket_context
  203. );
  204. // Verify we connected properly
  205. if (empty($this->smtp_conn)) {
  206. $this->error = array(
  207. 'error' => 'Failed to connect to server',
  208. 'errno' => $errno,
  209. 'errstr' => $errstr
  210. );
  211. if ($this->do_debug >= 1) {
  212. $this->edebug(
  213. 'SMTP ERROR: ' . $this->error['error']
  214. . ": $errstr ($errno)"
  215. );
  216. }
  217. return false;
  218. }
  219. if ($this->do_debug >= 3) {
  220. $this->edebug('Connection: opened');
  221. }
  222. // SMTP server can take longer to respond, give longer timeout for first read
  223. // Windows does not have support for this timeout function
  224. if (substr(PHP_OS, 0, 3) != 'WIN') {
  225. $max = ini_get('max_execution_time');
  226. if ($max != 0 && $timeout > $max) { // Don't bother if unlimited
  227. @set_time_limit($timeout);
  228. }
  229. stream_set_timeout($this->smtp_conn, $timeout, 0);
  230. }
  231. // Get any announcement
  232. $announce = $this->get_lines();
  233. if ($this->do_debug >= 2) {
  234. $this->edebug('SERVER -> CLIENT: ' . $announce);
  235. }
  236. return true;
  237. }
  238. /**
  239. * Initiate a TLS (encrypted) session.
  240. * @access public
  241. * @return bool
  242. */
  243. public function startTLS()
  244. {
  245. if (!$this->sendCommand('STARTTLS', 'STARTTLS', 220)) {
  246. return false;
  247. }
  248. // Begin encrypted connection
  249. if (!stream_socket_enable_crypto(
  250. $this->smtp_conn,
  251. true,
  252. STREAM_CRYPTO_METHOD_TLS_CLIENT
  253. )) {
  254. return false;
  255. }
  256. return true;
  257. }
  258. /**
  259. * Perform SMTP authentication.
  260. * Must be run after hello().
  261. * @see hello()
  262. * @param string $username The user name
  263. * @param string $password The password
  264. * @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5)
  265. * @param string $realm The auth realm for NTLM
  266. * @param string $workstation The auth workstation for NTLM
  267. * @access public
  268. * @return bool True if successfully authenticated.
  269. */
  270. public function authenticate(
  271. $username,
  272. $password,
  273. $authtype = 'LOGIN',
  274. $realm = '',
  275. $workstation = ''
  276. ) {
  277. if (empty($authtype)) {
  278. $authtype = 'LOGIN';
  279. }
  280. switch ($authtype) {
  281. case 'PLAIN':
  282. // Start authentication
  283. if (!$this->sendCommand('AUTH', 'AUTH PLAIN', 334)) {
  284. return false;
  285. }
  286. // Send encoded username and password
  287. if (!$this->sendCommand(
  288. 'User & Password',
  289. base64_encode("\0" . $username . "\0" . $password),
  290. 235
  291. )
  292. ) {
  293. return false;
  294. }
  295. break;
  296. case 'LOGIN':
  297. // Start authentication
  298. if (!$this->sendCommand('AUTH', 'AUTH LOGIN', 334)) {
  299. return false;
  300. }
  301. if (!$this->sendCommand("Username", base64_encode($username), 334)) {
  302. return false;
  303. }
  304. if (!$this->sendCommand("Password", base64_encode($password), 235)) {
  305. return false;
  306. }
  307. break;
  308. case 'NTLM':
  309. /*
  310. * ntlm_sasl_client.php
  311. * Bundled with Permission
  312. *
  313. * How to telnet in windows:
  314. * http://technet.microsoft.com/en-us/library/aa995718%28EXCHG.65%29.aspx
  315. * PROTOCOL Docs http://curl.haxx.se/rfc/ntlm.html#ntlmSmtpAuthentication
  316. */
  317. require_once 'extras/ntlm_sasl_client.php';
  318. $temp = new stdClass();
  319. $ntlm_client = new ntlm_sasl_client_class;
  320. //Check that functions are available
  321. if (!$ntlm_client->Initialize($temp)) {
  322. $this->error = array('error' => $temp->error);
  323. if ($this->do_debug >= 1) {
  324. $this->edebug(
  325. 'You need to enable some modules in your php.ini file: '
  326. . $this->error['error']
  327. );
  328. }
  329. return false;
  330. }
  331. //msg1
  332. $msg1 = $ntlm_client->TypeMsg1($realm, $workstation); //msg1
  333. if (!$this->sendCommand(
  334. 'AUTH NTLM',
  335. 'AUTH NTLM ' . base64_encode($msg1),
  336. 334
  337. )
  338. ) {
  339. return false;
  340. }
  341. //Though 0 based, there is a white space after the 3 digit number
  342. //msg2
  343. $challenge = substr($this->last_reply, 3);
  344. $challenge = base64_decode($challenge);
  345. $ntlm_res = $ntlm_client->NTLMResponse(
  346. substr($challenge, 24, 8),
  347. $password
  348. );
  349. //msg3
  350. $msg3 = $ntlm_client->TypeMsg3(
  351. $ntlm_res,
  352. $username,
  353. $realm,
  354. $workstation
  355. );
  356. // send encoded username
  357. return $this->sendCommand('Username', base64_encode($msg3), 235);
  358. break;
  359. case 'CRAM-MD5':
  360. // Start authentication
  361. if (!$this->sendCommand('AUTH CRAM-MD5', 'AUTH CRAM-MD5', 334)) {
  362. return false;
  363. }
  364. // Get the challenge
  365. $challenge = base64_decode(substr($this->last_reply, 4));
  366. // Build the response
  367. $response = $username . ' ' . $this->hmac($challenge, $password);
  368. // send encoded credentials
  369. return $this->sendCommand('Username', base64_encode($response), 235);
  370. break;
  371. }
  372. return true;
  373. }
  374. /**
  375. * Calculate an MD5 HMAC hash.
  376. * Works like hash_hmac('md5', $data, $key)
  377. * in case that function is not available
  378. * @param string $data The data to hash
  379. * @param string $key The key to hash with
  380. * @access protected
  381. * @return string
  382. */
  383. protected function hmac($data, $key)
  384. {
  385. if (function_exists('hash_hmac')) {
  386. return hash_hmac('md5', $data, $key);
  387. }
  388. // The following borrowed from
  389. // http://php.net/manual/en/function.mhash.php#27225
  390. // RFC 2104 HMAC implementation for php.
  391. // Creates an md5 HMAC.
  392. // Eliminates the need to install mhash to compute a HMAC
  393. // Hacked by Lance Rushing
  394. $bytelen = 64; // byte length for md5
  395. if (strlen($key) > $bytelen) {
  396. $key = pack('H*', md5($key));
  397. }
  398. $key = str_pad($key, $bytelen, chr(0x00));
  399. $ipad = str_pad('', $bytelen, chr(0x36));
  400. $opad = str_pad('', $bytelen, chr(0x5c));
  401. $k_ipad = $key ^ $ipad;
  402. $k_opad = $key ^ $opad;
  403. return md5($k_opad . pack('H*', md5($k_ipad . $data)));
  404. }
  405. /**
  406. * Check connection state.
  407. * @access public
  408. * @return bool True if connected.
  409. */
  410. public function connected()
  411. {
  412. if (!empty($this->smtp_conn)) {
  413. $sock_status = stream_get_meta_data($this->smtp_conn);
  414. if ($sock_status['eof']) {
  415. // the socket is valid but we are not connected
  416. if ($this->do_debug >= 1) {
  417. $this->edebug(
  418. 'SMTP NOTICE: EOF caught while checking if connected'
  419. );
  420. }
  421. $this->close();
  422. return false;
  423. }
  424. return true; // everything looks good
  425. }
  426. return false;
  427. }
  428. /**
  429. * Close the socket and clean up the state of the class.
  430. * Don't use this function without first trying to use QUIT.
  431. * @see quit()
  432. * @access public
  433. * @return void
  434. */
  435. public function close()
  436. {
  437. $this->error = null; // so there is no confusion
  438. $this->helo_rply = null;
  439. if (!empty($this->smtp_conn)) {
  440. // close the connection and cleanup
  441. fclose($this->smtp_conn);
  442. if ($this->do_debug >= 3) {
  443. $this->edebug('Connection: closed');
  444. }
  445. $this->smtp_conn = 0;
  446. }
  447. }
  448. /**
  449. * Send an SMTP DATA command.
  450. * Issues a data command and sends the msg_data to the server,
  451. * finializing the mail transaction. $msg_data is the message
  452. * that is to be send with the headers. Each header needs to be
  453. * on a single line followed by a <CRLF> with the message headers
  454. * and the message body being separated by and additional <CRLF>.
  455. * Implements rfc 821: DATA <CRLF>
  456. * @param string $msg_data Message data to send
  457. * @access public
  458. * @return bool
  459. */
  460. public function data($msg_data)
  461. {
  462. if (!$this->sendCommand('DATA', 'DATA', 354)) {
  463. return false;
  464. }
  465. /* The server is ready to accept data!
  466. * According to rfc821 we should not send more than 1000 characters on a single line (including the CRLF)
  467. * so we will break the data up into lines by \r and/or \n then if needed we will break each of those into
  468. * smaller lines to fit within the limit.
  469. * We will also look for lines that start with a '.' and prepend an additional '.'.
  470. * NOTE: this does not count towards line-length limit.
  471. */
  472. // Normalize line breaks before exploding
  473. $lines = explode("\n", str_replace(array("\r\n", "\r"), "\n", $msg_data));
  474. /* To distinguish between a complete RFC822 message and a plain message body, we check if the first field
  475. * of the first line (':' separated) does not contain a space then it _should_ be a header and we will
  476. * process all lines before a blank line as headers.
  477. */
  478. $field = substr($lines[0], 0, strpos($lines[0], ':'));
  479. $in_headers = false;
  480. if (!empty($field) && strpos($field, ' ') === false) {
  481. $in_headers = true;
  482. }
  483. foreach ($lines as $line) {
  484. $lines_out = array();
  485. if ($in_headers and $line == '') {
  486. $in_headers = false;
  487. }
  488. // ok we need to break this line up into several smaller lines
  489. //This is a small micro-optimisation: isset($str[$len]) is equivalent to (strlen($str) > $len)
  490. while (isset($line[self::MAX_LINE_LENGTH])) {
  491. //Working backwards, try to find a space within the last MAX_LINE_LENGTH chars of the line to break on
  492. //so as to avoid breaking in the middle of a word
  493. $pos = strrpos(substr($line, 0, self::MAX_LINE_LENGTH), ' ');
  494. if (!$pos) { //Deliberately matches both false and 0
  495. //No nice break found, add a hard break
  496. $pos = self::MAX_LINE_LENGTH - 1;
  497. $lines_out[] = substr($line, 0, $pos);
  498. $line = substr($line, $pos);
  499. } else {
  500. //Break at the found point
  501. $lines_out[] = substr($line, 0, $pos);
  502. //Move along by the amount we dealt with
  503. $line = substr($line, $pos + 1);
  504. }
  505. /* If processing headers add a LWSP-char to the front of new line
  506. * RFC822 section 3.1.1
  507. */
  508. if ($in_headers) {
  509. $line = "\t" . $line;
  510. }
  511. }
  512. $lines_out[] = $line;
  513. // Send the lines to the server
  514. foreach ($lines_out as $line_out) {
  515. //RFC2821 section 4.5.2
  516. if (!empty($line_out) and $line_out[0] == '.') {
  517. $line_out = '.' . $line_out;
  518. }
  519. $this->client_send($line_out . self::CRLF);
  520. }
  521. }
  522. // Message data has been sent, complete the command
  523. return $this->sendCommand('DATA END', '.', 250);
  524. }
  525. /**
  526. * Send an SMTP HELO or EHLO command.
  527. * Used to identify the sending server to the receiving server.
  528. * This makes sure that client and server are in a known state.
  529. * Implements RFC 821: HELO <SP> <domain> <CRLF>
  530. * and RFC 2821 EHLO.
  531. * @param string $host The host name or IP to connect to
  532. * @access public
  533. * @return bool
  534. */
  535. public function hello($host = '')
  536. {
  537. // Try extended hello first (RFC 2821)
  538. return (bool)($this->sendHello('EHLO', $host) or $this->sendHello('HELO', $host));
  539. }
  540. /**
  541. * Send an SMTP HELO or EHLO command.
  542. * Low-level implementation used by hello()
  543. * @see hello()
  544. * @param string $hello The HELO string
  545. * @param string $host The hostname to say we are
  546. * @access protected
  547. * @return bool
  548. */
  549. protected function sendHello($hello, $host)
  550. {
  551. $noerror = $this->sendCommand($hello, $hello . ' ' . $host, 250);
  552. $this->helo_rply = $this->last_reply;
  553. return $noerror;
  554. }
  555. /**
  556. * Send an SMTP MAIL command.
  557. * Starts a mail transaction from the email address specified in
  558. * $from. Returns true if successful or false otherwise. If True
  559. * the mail transaction is started and then one or more recipient
  560. * commands may be called followed by a data command.
  561. * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
  562. * @param string $from Source address of this message
  563. * @access public
  564. * @return bool
  565. */
  566. public function mail($from)
  567. {
  568. $useVerp = ($this->do_verp ? ' XVERP' : '');
  569. return $this->sendCommand(
  570. 'MAIL FROM',
  571. 'MAIL FROM:<' . $from . '>' . $useVerp,
  572. 250
  573. );
  574. }
  575. /**
  576. * Send an SMTP QUIT command.
  577. * Closes the socket if there is no error or the $close_on_error argument is true.
  578. * Implements from rfc 821: QUIT <CRLF>
  579. * @param bool $close_on_error Should the connection close if an error occurs?
  580. * @access public
  581. * @return bool
  582. */
  583. public function quit($close_on_error = true)
  584. {
  585. $noerror = $this->sendCommand('QUIT', 'QUIT', 221);
  586. $err = $this->error; //Save any error
  587. if ($noerror or $close_on_error) {
  588. $this->close();
  589. $this->error = $err; //Restore any error from the quit command
  590. }
  591. return $noerror;
  592. }
  593. /**
  594. * Send an SMTP RCPT command.
  595. * Sets the TO argument to $toaddr.
  596. * Returns true if the recipient was accepted false if it was rejected.
  597. * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
  598. * @param string $toaddr The address the message is being sent to
  599. * @access public
  600. * @return bool
  601. */
  602. public function recipient($toaddr)
  603. {
  604. return $this->sendCommand(
  605. 'RCPT TO',
  606. 'RCPT TO:<' . $toaddr . '>',
  607. array(250, 251)
  608. );
  609. }
  610. /**
  611. * Send an SMTP RSET command.
  612. * Abort any transaction that is currently in progress.
  613. * Implements rfc 821: RSET <CRLF>
  614. * @access public
  615. * @return bool True on success.
  616. */
  617. public function reset()
  618. {
  619. return $this->sendCommand('RSET', 'RSET', 250);
  620. }
  621. /**
  622. * Send a command to an SMTP server and check its return code.
  623. * @param string $command The command name - not sent to the server
  624. * @param string $commandstring The actual command to send
  625. * @param int|array $expect One or more expected integer success codes
  626. * @access protected
  627. * @return bool True on success.
  628. */
  629. protected function sendCommand($command, $commandstring, $expect)
  630. {
  631. if (!$this->connected()) {
  632. $this->error = array(
  633. 'error' => "Called $command without being connected"
  634. );
  635. return false;
  636. }
  637. $this->client_send($commandstring . self::CRLF);
  638. $reply = $this->get_lines();
  639. $code = substr($reply, 0, 3);
  640. if ($this->do_debug >= 2) {
  641. $this->edebug('SERVER -> CLIENT: ' . $reply);
  642. }
  643. if (!in_array($code, (array)$expect)) {
  644. $this->last_reply = null;
  645. $this->error = array(
  646. 'error' => "$command command failed",
  647. 'smtp_code' => $code,
  648. 'detail' => substr($reply, 4)
  649. );
  650. if ($this->do_debug >= 1) {
  651. $this->edebug(
  652. 'SMTP ERROR: ' . $this->error['error'] . ': ' . $reply
  653. );
  654. }
  655. return false;
  656. }
  657. $this->last_reply = $reply;
  658. $this->error = null;
  659. return true;
  660. }
  661. /**
  662. * Send an SMTP SAML command.
  663. * Starts a mail transaction from the email address specified in $from.
  664. * Returns true if successful or false otherwise. If True
  665. * the mail transaction is started and then one or more recipient
  666. * commands may be called followed by a data command. This command
  667. * will send the message to the users terminal if they are logged
  668. * in and send them an email.
  669. * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
  670. * @param string $from The address the message is from
  671. * @access public
  672. * @return bool
  673. */
  674. public function sendAndMail($from)
  675. {
  676. return $this->sendCommand('SAML', "SAML FROM:$from", 250);
  677. }
  678. /**
  679. * Send an SMTP VRFY command.
  680. * @param string $name The name to verify
  681. * @access public
  682. * @return bool
  683. */
  684. public function verify($name)
  685. {
  686. return $this->sendCommand('VRFY', "VRFY $name", array(250, 251));
  687. }
  688. /**
  689. * Send an SMTP NOOP command.
  690. * Used to keep keep-alives alive, doesn't actually do anything
  691. * @access public
  692. * @return bool
  693. */
  694. public function noop()
  695. {
  696. return $this->sendCommand('NOOP', 'NOOP', 250);
  697. }
  698. /**
  699. * Send an SMTP TURN command.
  700. * This is an optional command for SMTP that this class does not support.
  701. * This method is here to make the RFC821 Definition complete for this class
  702. * and _may_ be implemented in future
  703. * Implements from rfc 821: TURN <CRLF>
  704. * @access public
  705. * @return bool
  706. */
  707. public function turn()
  708. {
  709. $this->error = array(
  710. 'error' => 'The SMTP TURN command is not implemented'
  711. );
  712. if ($this->do_debug >= 1) {
  713. $this->edebug('SMTP NOTICE: ' . $this->error['error']);
  714. }
  715. return false;
  716. }
  717. /**
  718. * Send raw data to the server.
  719. * @param string $data The data to send
  720. * @access public
  721. * @return int|bool The number of bytes sent to the server or false on error
  722. */
  723. public function client_send($data)
  724. {
  725. if ($this->do_debug >= 1) {
  726. $this->edebug("CLIENT -> SERVER: $data");
  727. }
  728. return fwrite($this->smtp_conn, $data);
  729. }
  730. /**
  731. * Get the latest error.
  732. * @access public
  733. * @return array
  734. */
  735. public function getError()
  736. {
  737. return $this->error;
  738. }
  739. /**
  740. * Get the last reply from the server.
  741. * @access public
  742. * @return string
  743. */
  744. public function getLastReply()
  745. {
  746. return $this->last_reply;
  747. }
  748. /**
  749. * Read the SMTP server's response.
  750. * Either before eof or socket timeout occurs on the operation.
  751. * With SMTP we can tell if we have more lines to read if the
  752. * 4th character is '-' symbol. If it is a space then we don't
  753. * need to read anything else.
  754. * @access protected
  755. * @return string
  756. */
  757. protected function get_lines()
  758. {
  759. // If the connection is bad, give up straight away
  760. if (!is_resource($this->smtp_conn)) {
  761. return '';
  762. }
  763. $data = '';
  764. $endtime = 0;
  765. stream_set_timeout($this->smtp_conn, $this->Timeout);
  766. if ($this->Timelimit > 0) {
  767. $endtime = time() + $this->Timelimit;
  768. }
  769. while (is_resource($this->smtp_conn) && !feof($this->smtp_conn)) {
  770. $str = @fgets($this->smtp_conn, 515);
  771. if ($this->do_debug >= 4) {
  772. $this->edebug("SMTP -> get_lines(): \$data was \"$data\"");
  773. $this->edebug("SMTP -> get_lines(): \$str is \"$str\"");
  774. }
  775. $data .= $str;
  776. if ($this->do_debug >= 4) {
  777. $this->edebug("SMTP -> get_lines(): \$data is \"$data\"");
  778. }
  779. // If 4th character is a space, we are done reading, break the loop, micro-optimisation over strlen
  780. if ((isset($str[3]) and $str[3] == ' ')) {
  781. break;
  782. }
  783. // Timed-out? Log and break
  784. $info = stream_get_meta_data($this->smtp_conn);
  785. if ($info['timed_out']) {
  786. if ($this->do_debug >= 4) {
  787. $this->edebug(
  788. 'SMTP -> get_lines(): timed-out (' . $this->Timeout . ' sec)'
  789. );
  790. }
  791. break;
  792. }
  793. // Now check if reads took too long
  794. if ($endtime and time() > $endtime) {
  795. if ($this->do_debug >= 4) {
  796. $this->edebug(
  797. 'SMTP -> get_lines(): timelimit reached ('.
  798. $this->Timelimit . ' sec)'
  799. );
  800. }
  801. break;
  802. }
  803. }
  804. return $data;
  805. }
  806. /**
  807. * Enable or disable VERP address generation.
  808. * @param bool $enabled
  809. */
  810. public function setVerp($enabled = false)
  811. {
  812. $this->do_verp = $enabled;
  813. }
  814. /**
  815. * Get VERP address generation mode.
  816. * @return bool
  817. */
  818. public function getVerp()
  819. {
  820. return $this->do_verp;
  821. }
  822. /**
  823. * Set debug output method.
  824. * @param string $method The function/method to use for debugging output.
  825. */
  826. public function setDebugOutput($method = 'echo')
  827. {
  828. $this->Debugoutput = $method;
  829. }
  830. /**
  831. * Get debug output method.
  832. * @return string
  833. */
  834. public function getDebugOutput()
  835. {
  836. return $this->Debugoutput;
  837. }
  838. /**
  839. * Set debug output level.
  840. * @param int $level
  841. */
  842. public function setDebugLevel($level = 0)
  843. {
  844. $this->do_debug = $level;
  845. }
  846. /**
  847. * Get debug output level.
  848. * @return int
  849. */
  850. public function getDebugLevel()
  851. {
  852. return $this->do_debug;
  853. }
  854. /**
  855. * Set SMTP timeout.
  856. * @param int $timeout
  857. */
  858. public function setTimeout($timeout = 0)
  859. {
  860. $this->Timeout = $timeout;
  861. }
  862. /**
  863. * Get SMTP timeout.
  864. * @return int
  865. */
  866. public function getTimeout()
  867. {
  868. return $this->Timeout;
  869. }
  870. }