Skip to content

Commit 72b51fc

Browse files
committed
Update SMTPServer.php
1 parent 39785d3 commit 72b51fc

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

webfiori/framework/mail/SMTPServer.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace webfiori\framework\mail;
33

4+
use webfiori\framework\exceptions\SMTPException;
45
/**
56
* A class which can be used to connect to SMTP server and execute commands on it.
67
*
@@ -139,7 +140,9 @@ public function connect() {
139140

140141
if (is_resource($this->serverCon)) {
141142
$this->_log('-', 0, $this->read());
142-
143+
if ($this->getLastResponseCode() != 220) {
144+
throw new SMTPException('Server did not respond with code 220 during initial connection.');
145+
}
143146
if ($this->sendHello()) {
144147
//We might need to switch to secure connection.
145148
$retVal = $this->_checkStartTls();
@@ -276,14 +279,17 @@ public function isInWritingMode() {
276279
*/
277280
public function read() {
278281
$message = '';
279-
282+
280283
while (!feof($this->serverCon)) {
281-
$str = fgets($this->serverCon);
284+
$str = stream_get_contents($this->serverCon);
285+
if ($str !== false) {
286+
$message .= $str;
282287

283-
$message .= $str;
284-
285-
if (!isset($str[3]) || (isset($str[3]) && $str[3] == ' ')) {
286-
break;
288+
if (!isset($str[3]) || (isset($str[3]) && $str[3] == ' ')) {
289+
break;
290+
}
291+
} else {
292+
$this->_log('-', '0', 'Unable to read server response.');
287293
}
288294
}
289295
$this->_setLastResponseCode($message);
@@ -429,13 +435,15 @@ private function _parseHelloResponse($response) {
429435
* @since 1.0
430436
*/
431437
private function _setLastResponseCode($serverResponseMessage) {
432-
$firstNum = $serverResponseMessage[0];
433-
$firstAsInt = intval($firstNum);
434-
435-
if ($firstAsInt != 0) {
436-
$secNum = $serverResponseMessage[1];
437-
$thirdNum = $serverResponseMessage[2];
438-
$this->lastResponseCode = intval($firstNum) * 100 + (intval($secNum * 10)) + (intval($thirdNum));
438+
if (strlen($serverResponseMessage) != 0) {
439+
$firstNum = $serverResponseMessage[0];
440+
$firstAsInt = intval($firstNum);
441+
442+
if ($firstAsInt != 0) {
443+
$secNum = $serverResponseMessage[1];
444+
$thirdNum = $serverResponseMessage[2];
445+
$this->lastResponseCode = intval($firstNum) * 100 + (intval($secNum * 10)) + (intval($thirdNum));
446+
}
439447
}
440448
}
441449
private function _switchToTls() {
@@ -484,7 +492,11 @@ private function _tryConnect($protocol) {
484492
}
485493

486494
if (!is_resource($conn)) {
487-
$this->_log('Connect', $err, 'Faild to connect: '.$errStr);
495+
if (strlen($errStr) == 0) {
496+
$this->_log('Connect', $err, 'Faild to connect due to unspecified error.');
497+
} else {
498+
$this->_log('Connect', $err, 'Faild to connect: '.$errStr);
499+
}
488500
}
489501

490502
return $conn;

0 commit comments

Comments
 (0)