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 Nov 2, 2023
1 parent 541a211 commit 804d702
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Mail/SmtpMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 804d702

Please sign in to comment.