|
1 | 1 | <?php
|
2 | 2 | namespace webfiori\framework\mail;
|
3 | 3 |
|
| 4 | +use webfiori\framework\exceptions\SMTPException; |
4 | 5 | /**
|
5 | 6 | * A class which can be used to connect to SMTP server and execute commands on it.
|
6 | 7 | *
|
@@ -139,7 +140,9 @@ public function connect() {
|
139 | 140 |
|
140 | 141 | if (is_resource($this->serverCon)) {
|
141 | 142 | $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 | + } |
143 | 146 | if ($this->sendHello()) {
|
144 | 147 | //We might need to switch to secure connection.
|
145 | 148 | $retVal = $this->_checkStartTls();
|
@@ -276,14 +279,17 @@ public function isInWritingMode() {
|
276 | 279 | */
|
277 | 280 | public function read() {
|
278 | 281 | $message = '';
|
279 |
| - |
| 282 | + |
280 | 283 | while (!feof($this->serverCon)) {
|
281 |
| - $str = fgets($this->serverCon); |
| 284 | + $str = stream_get_contents($this->serverCon); |
| 285 | + if ($str !== false) { |
| 286 | + $message .= $str; |
282 | 287 |
|
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.'); |
287 | 293 | }
|
288 | 294 | }
|
289 | 295 | $this->_setLastResponseCode($message);
|
@@ -429,13 +435,15 @@ private function _parseHelloResponse($response) {
|
429 | 435 | * @since 1.0
|
430 | 436 | */
|
431 | 437 | 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 | + } |
439 | 447 | }
|
440 | 448 | }
|
441 | 449 | private function _switchToTls() {
|
@@ -484,7 +492,11 @@ private function _tryConnect($protocol) {
|
484 | 492 | }
|
485 | 493 |
|
486 | 494 | 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 | + } |
488 | 500 | }
|
489 | 501 |
|
490 | 502 | return $conn;
|
|
0 commit comments