Skip to content

Commit

Permalink
fix: Email class may not log an error when it fails to send
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 9, 2022
1 parent d4b0483 commit 6d51b2c
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 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 @@ -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

0 comments on commit 6d51b2c

Please sign in to comment.