diff --git a/changelog.md b/changelog.md index d765948b..9996d4a6 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # PHPMailer Change Log +## WIP +* Handle early connection errors such as 421 + ## Version 6.2.0 * PHP 8.0 compatibility, many thanks to @jrf_nl! * Switch from PHP CS Fixer to PHP CodeSniffer for coding standards diff --git a/src/SMTP.php b/src/SMTP.php index ab7f46e4..0243501f 100644 --- a/src/SMTP.php +++ b/src/SMTP.php @@ -343,8 +343,20 @@ class SMTP // Get any announcement $this->last_reply = $this->get_lines(); $this->edebug('SERVER -> CLIENT: ' . $this->last_reply, self::DEBUG_SERVER); - - return true; + $responseCode = (int)substr($this->last_reply, 0, 3); + if ($responseCode === 220) { + return true; + } + //Anything other than a 220 response means something went wrong + //RFC 5321 says the server will wait for us to send a QUIT in response to a 554 error + //https://tools.ietf.org/html/rfc5321#section-3.1 + if ($responseCode === 554) { + $this->quit(); + } + //This will handle 421 responses which may not wait for a QUIT (e.g. if the server is being shut down) + $this->edebug('Connection: closing due to error', self::DEBUG_CONNECTION); + $this->close(); + return false; } /**