PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/phpmailer/class.smtp.php

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