Skip to content

Commit

Permalink
SmtpMailer: fixed problem with empty response with slow SMTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 2, 2023
1 parent 74c582f commit c0b8112
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Mail/SmtpMailer.php
Expand Up @@ -213,14 +213,26 @@ protected function write(string $line, int|array|null $expectedCode = null, ?str
*/
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;
}
}

0 comments on commit c0b8112

Please sign in to comment.