Skip to content

Commit 9204889

Browse files
authored
Merge pull request #6362 from kenjis/fix-email-logging
fix: Email class may not log an error when it fails to send
2 parents 4fdffaa + 6d51b2c commit 9204889

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

system/Email/Email.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,13 @@ class Email
288288
*/
289289
protected $debugMessage = [];
290290

291+
/**
292+
* Raw debug messages
293+
*
294+
* @var string[]
295+
*/
296+
private array $debugMessageRaw = [];
297+
291298
/**
292299
* Recipients
293300
*
@@ -434,16 +441,17 @@ public function initialize($config)
434441
*/
435442
public function clear($clearAttachments = false)
436443
{
437-
$this->subject = '';
438-
$this->body = '';
439-
$this->finalBody = '';
440-
$this->headerStr = '';
441-
$this->replyToFlag = false;
442-
$this->recipients = [];
443-
$this->CCArray = [];
444-
$this->BCCArray = [];
445-
$this->headers = [];
446-
$this->debugMessage = [];
444+
$this->subject = '';
445+
$this->body = '';
446+
$this->finalBody = '';
447+
$this->headerStr = '';
448+
$this->replyToFlag = false;
449+
$this->recipients = [];
450+
$this->CCArray = [];
451+
$this->BCCArray = [];
452+
$this->headers = [];
453+
$this->debugMessage = [];
454+
$this->debugMessageRaw = [];
447455

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

@@ -1658,7 +1666,12 @@ protected function spoolEmail()
16581666
}
16591667

16601668
if (! $success) {
1661-
$this->setErrorMessage(lang('Email.sendFailure' . ($protocol === 'mail' ? 'PHPMail' : ucfirst($protocol))));
1669+
$message = lang('Email.sendFailure' . ($protocol === 'mail' ? 'PHPMail' : ucfirst($protocol)));
1670+
1671+
log_message('error', 'Email: ' . $message);
1672+
log_message('error', $this->printDebuggerRaw());
1673+
1674+
$this->setErrorMessage($message);
16621675

16631676
return false;
16641677
}
@@ -1937,7 +1950,8 @@ protected function sendCommand($cmd, $data = '')
19371950

19381951
$reply = $this->getSMTPData();
19391952

1940-
$this->debugMessage[] = '<pre>' . $cmd . ': ' . $reply . '</pre>';
1953+
$this->debugMessage[] = '<pre>' . $cmd . ': ' . $reply . '</pre>';
1954+
$this->debugMessageRaw[] = $cmd . ': ' . $reply;
19411955

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

20922106
/**
2093-
* @param array $include List of raw data chunks to include in the output
2094-
* Valid options are: 'headers', 'subject', 'body'
2107+
* @param array|string $include List of raw data chunks to include in the output
2108+
* Valid options are: 'headers', 'subject', 'body'
20952109
*
20962110
* @return string
20972111
*/
@@ -2119,12 +2133,21 @@ public function printDebugger($include = ['headers', 'subject', 'body'])
21192133
return $msg . ($rawData === '' ? '' : '<pre>' . $rawData . '</pre>');
21202134
}
21212135

2136+
/**
2137+
* Returns raw debug messages
2138+
*/
2139+
private function printDebuggerRaw(): string
2140+
{
2141+
return implode("\n", $this->debugMessageRaw);
2142+
}
2143+
21222144
/**
21232145
* @param string $msg
21242146
*/
21252147
protected function setErrorMessage($msg)
21262148
{
2127-
$this->debugMessage[] = $msg . '<br />';
2149+
$this->debugMessage[] = $msg . '<br />';
2150+
$this->debugMessageRaw[] = $msg;
21282151
}
21292152

21302153
/**

0 commit comments

Comments
 (0)