From 804d70278458452863a2d6be4c1d5bf5f91b3950 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 2 Oct 2023 22:46:43 +0200 Subject: [PATCH] SmtpMailer: fixed problem with empty response with slow SMTP server --- src/Mail/SmtpMailer.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Mail/SmtpMailer.php b/src/Mail/SmtpMailer.php index 5f07da5..4ebd3c0 100644 --- a/src/Mail/SmtpMailer.php +++ b/src/Mail/SmtpMailer.php @@ -244,14 +244,26 @@ protected function write(string $line, $expectedCode = null, ?string $message = */ protected function read(): string { - $s = ''; - while (($line = fgets($this->connection, 1000)) != null) { // intentionally == - $s .= $line; - if (substr($line, 3, 1) === ' ') { + $data = ''; + $endtime = $this->timeout > 0 ? time() + $this->timeout : 0; + + while (is_resource($this->connection) && !feof($this->connection)) { + $line = @fgets($this->connection); // @ is escalated to exception + if ($line === '' || $line === false) { + $info = stream_get_meta_data($this->connection); + if ($info['timed_out'] || ($endtime && time() > $endtime)) { + throw new SmtpException('Connection timed out.'); + } elseif ($info['eof']) { + throw new SmtpException('Connection has been closed unexpectedly.'); + } + } + + $data .= $line; + if (preg_match('#^.{3}(?:[ \r\n]|$)#D', $line)) { break; } } - return $s; + return $data; } }