Skip to content

Commit

Permalink
Merge pull request PHPMailer#2526 from miken32/master
Browse files Browse the repository at this point in the history
Provide additional error details after connection failures
  • Loading branch information
Synchro authored Feb 28, 2022
2 parents 3525f1b + 05e70e8 commit ce32116
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,8 @@ public function smtpConnect($options = null)
}
if ($tls) {
if (!$this->smtp->startTLS()) {
throw new Exception($this->lang('connect_host'));
$message = $this->getSmtpErrorMessage('connect_host');
throw new Exception($message);
}
//We must resend EHLO after TLS negotiation
$this->smtp->hello($hello);
Expand Down Expand Up @@ -2191,6 +2192,10 @@ public function smtpConnect($options = null)
//As we've caught all exceptions, just report whatever the last one was
if ($this->exceptions && null !== $lastexception) {
throw $lastexception;
} elseif ($this->exceptions) {
// no exception was thrown, likely $this->smtp->connect() failed
$message = $this->getSmtpErrorMessage('connect_host');
throw new Exception($message);
}

return false;
Expand Down Expand Up @@ -4127,6 +4132,26 @@ protected function lang($key)
return $key;
}

/**
* Build an error message starting with a generic one and adding details if possible.
*
* @param string $base_key
* @return string
*/
private function getSmtpErrorMessage($base_key)
{
$message = $this->lang($base_key);
$error = $this->smtp->getError();
if (!empty($error['error'])) {
$message .= ' ' . $error['error'];
if (!empty($error['detail'])) {
$message .= ' ' . $error['detail'];
}
}

return $message;
}

/**
* Check if an error occurred.
*
Expand Down

0 comments on commit ce32116

Please sign in to comment.