Skip to content

Commit c0b8112

Browse files
committed
SmtpMailer: fixed problem with empty response with slow SMTP server
1 parent 74c582f commit c0b8112

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Mail/SmtpMailer.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,26 @@ protected function write(string $line, int|array|null $expectedCode = null, ?str
213213
*/
214214
protected function read(): string
215215
{
216-
$s = '';
217-
while (($line = fgets($this->connection, 1000)) != null) { // intentionally ==
218-
$s .= $line;
219-
if (substr($line, 3, 1) === ' ') {
216+
$data = '';
217+
$endtime = $this->timeout > 0 ? time() + $this->timeout : 0;
218+
219+
while (is_resource($this->connection) && !feof($this->connection)) {
220+
$line = @fgets($this->connection); // @ is escalated to exception
221+
if ($line === '' || $line === false) {
222+
$info = stream_get_meta_data($this->connection);
223+
if ($info['timed_out'] || ($endtime && time() > $endtime)) {
224+
throw new SmtpException('Connection timed out.');
225+
} elseif ($info['eof']) {
226+
throw new SmtpException('Connection has been closed unexpectedly.');
227+
}
228+
}
229+
230+
$data .= $line;
231+
if (preg_match('#^.{3}(?:[ \r\n]|$)#D', $line)) {
220232
break;
221233
}
222234
}
223235

224-
return $s;
236+
return $data;
225237
}
226238
}

0 commit comments

Comments
 (0)