Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Email class may not log an error when it fails to send #6362

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ class Email
*/
protected $debugMessage = [];

/**
* Raw debug messages
*
* @var string[]
*/
private array $debugMessageRaw = [];

/**
* Recipients
*
Expand Down Expand Up @@ -434,16 +441,17 @@ public function initialize($config)
*/
public function clear($clearAttachments = false)
{
$this->subject = '';
$this->body = '';
$this->finalBody = '';
$this->headerStr = '';
$this->replyToFlag = false;
$this->recipients = [];
$this->CCArray = [];
$this->BCCArray = [];
$this->headers = [];
$this->debugMessage = [];
$this->subject = '';
$this->body = '';
$this->finalBody = '';
$this->headerStr = '';
$this->replyToFlag = false;
$this->recipients = [];
$this->CCArray = [];
$this->BCCArray = [];
$this->headers = [];
$this->debugMessage = [];
$this->debugMessageRaw = [];

$this->setHeader('Date', $this->setDate());

Expand Down Expand Up @@ -1658,7 +1666,12 @@ protected function spoolEmail()
}

if (! $success) {
$this->setErrorMessage(lang('Email.sendFailure' . ($protocol === 'mail' ? 'PHPMail' : ucfirst($protocol))));
$message = lang('Email.sendFailure' . ($protocol === 'mail' ? 'PHPMail' : ucfirst($protocol)));

log_message('error', 'Email: ' . $message);
log_message('error', $this->printDebuggerRaw());

$this->setErrorMessage($message);

return false;
}
Expand Down Expand Up @@ -1937,7 +1950,8 @@ protected function sendCommand($cmd, $data = '')

$reply = $this->getSMTPData();

$this->debugMessage[] = '<pre>' . $cmd . ': ' . $reply . '</pre>';
$this->debugMessage[] = '<pre>' . $cmd . ': ' . $reply . '</pre>';
$this->debugMessageRaw[] = $cmd . ': ' . $reply;

if ($resp === null || ((int) static::substr($reply, 0, 3) !== $resp)) {
$this->setErrorMessage(lang('Email.SMTPError', [$reply]));
Expand Down Expand Up @@ -2090,8 +2104,8 @@ protected function getHostname()
}

/**
* @param array $include List of raw data chunks to include in the output
* Valid options are: 'headers', 'subject', 'body'
* @param array|string $include List of raw data chunks to include in the output
* Valid options are: 'headers', 'subject', 'body'
*
* @return string
*/
Expand Down Expand Up @@ -2119,12 +2133,21 @@ public function printDebugger($include = ['headers', 'subject', 'body'])
return $msg . ($rawData === '' ? '' : '<pre>' . $rawData . '</pre>');
}

/**
* Returns raw debug messages
*/
private function printDebuggerRaw(): string
{
return implode("\n", $this->debugMessageRaw);
}

/**
* @param string $msg
*/
protected function setErrorMessage($msg)
{
$this->debugMessage[] = $msg . '<br />';
$this->debugMessage[] = $msg . '<br />';
$this->debugMessageRaw[] = $msg;
}

/**
Expand Down