Skip to content

Commit

Permalink
Update mail log if exception is thrown
Browse files Browse the repository at this point in the history
Resolves: #57
  • Loading branch information
sypets committed Aug 17, 2024
1 parent 40c46ac commit 580deee
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 4 deletions.
18 changes: 18 additions & 0 deletions Classes/Domain/Model/MailLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class MailLog extends AbstractModel
{
final public const MAIL_STATUS_UNKNOWN = 0;
final public const MAIL_STATUS_SENT_OK = 1;
final public const MAIL_STATUS_NOT_SENT = 2;
final public const MAIL_STATUS_DEFAULT = self::MAIL_STATUS_UNKNOWN;

/**
* @var int
*/
Expand Down Expand Up @@ -41,6 +46,8 @@ class MailLog extends AbstractModel

protected string $result = 'Not send until now';

protected int $status = self::MAIL_STATUS_DEFAULT;

protected int $sysLanguageUid = 0;

public function getTypoScriptKey(): string
Expand Down Expand Up @@ -147,6 +154,17 @@ public function setResult(string $result): self
return $this;
}

public function getStatus(): int
{
return $this->status;
}

public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}

public function getSysLanguageUid(): int
{
return $this->sysLanguageUid;
Expand Down
29 changes: 26 additions & 3 deletions Classes/Logging/LoggingTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,36 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa
$mailLogRepository->add($mailLog);
GeneralUtility::makeInstance(PersistenceManager::class)->persistAll();

$result = $this->originalTransport->send($message, $envelope);
/** @var string $result */
$result = '';
/** @var int $status */
$status = MailLog::MAIL_STATUS_UNKNOWN;
$exceptionWasThrown = null;
$sendMessage = null;
try {
$sendMessage = $this->originalTransport->send($message, $envelope);
if ($sendMessage !== null) {
$result = 'Email sent';
$status = MailLog::MAIL_STATUS_SENT_OK;
} else {
$result = 'Email not sent';
$status = MailLog::MAIL_STATUS_NOT_SENT;
}
} catch (\Throwable $e) {
$result = 'Email not sent. Error: ' . $e->getMessage();
$status = MailLog::MAIL_STATUS_NOT_SENT;
$exceptionWasThrown = $e;
}

// write result to log after send
$this->assignMailLog($mailLog, $message);
$mailLog->setResult((string)(bool)$result);
$mailLog->setResult($result);
$mailLog->setStatus($status);
$mailLogRepository->update($mailLog);
return $result;
if ($exceptionWasThrown) {
throw $exceptionWasThrown;
}
return $sendMessage;
}

public function __toString(): string
Expand Down
10 changes: 10 additions & 0 deletions Configuration/TCA/tx_maillogger_domain_model_maillog.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@
'eval' => 'trim',
],
],
'status' => [
'exclude' => 1,
'label' => 'LLL:EXT:mail_logger/Resources/Private/Language/locallang_db.xlf:tx_maillogger_domain_model_maillog.status',
'config' => [
'readOnly' => 1,
'type' => 'input',
'size' => 10,
'eval' => 'trim',
],
],
'headers' => [
'exclude' => 1,
'label' => 'LLL:EXT:mail_logger/Resources/Private/Language/locallang_db.xlf:tx_maillogger_domain_model_maillog.headers',
Expand Down
11 changes: 11 additions & 0 deletions Resources/Private/Backend/Partials/MailLog.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<th>
<mailLogger:translate id="module.mailLog.date"/>
</th>
<th>
<mailLogger:translate id="module.mailLog.status"/>
</th>
<th>
<mailLogger:translate id="module.mailLog.typoScriptKey"/>
</th>
Expand All @@ -34,6 +37,14 @@
<td>
<f:format.date format="Y-m-d H:i:s">{item.crdate}</f:format.date>
</td>
<td>
<f:if condition="{item.status} == 1">
<core:icon identifier="actions-check-circle" size="medium" title="ok"/>
</f:if>
<f:if condition="{item.status} == 2">
<core:icon identifier="actions-exclamation-circle" size="medium" title="E-Mail not sent"/>
</f:if>
</td>
<td>{item.typoScriptKey}</td>
<td>{item.subject}</td>
<td>{item.mailFrom}</td>
Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/Backend/Templates/MailLog/Show.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1><mailLogger:translate id="module.mailLog.title.singleLog"/></h1>
<mailLogger:translate id="module.mailLog.typoScriptKey"/>: {mailLog.typoScriptKey}<br />
</f:if>
<mailLogger:translate id="module.mailLog.sysLanguageUid"/>: <mailLogger:sysLanguageUid uid="{mailLog.sysLanguageUid}" /><br />
<mailLogger:translate id="module.mailLog.result"/>: {mailLog.result}<br />
<mailLogger:translate id="module.mailLog.result_message"/>: {mailLog.result}<br />
<mailLogger:translate id="module.mailLog.date"/>: <f:format.date format="d.m.Y H:i:s">{mailLog.crdate}</f:format.date><br />
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions Resources/Private/Language/de.locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<trans-unit id="tx_maillogger.module.mailLog.result">
<target>Akzeptierte Empfänger</target>
</trans-unit>
<trans-unit id="tx_maillogger.module.mailLog.result_message">
<target>Ergebnis</target>
</trans-unit>
<trans-unit id="tx_maillogger_domain_model_maillog.status">
<target>Status</target>
</trans-unit>
<trans-unit id="tx_maillogger.module.mailLog.mailFrom">
<target>Von</target>
</trans-unit>
Expand All @@ -69,6 +75,9 @@
<trans-unit id="tx_maillogger.module.mailLog.date">
<target>Datum</target>
</trans-unit>
<trans-unit id="tx_maillogger.module.mailLog.status">
<source>Result</source>
</trans-unit>

<!-- Model: Task -->
<trans-unit id="tx_maillogger_domain_model_task">
Expand Down
9 changes: 9 additions & 0 deletions Resources/Private/Language/locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
<trans-unit id="tx_maillogger.module.mailLog.date">
<source>Date</source>
</trans-unit>
<trans-unit id="tx_maillogger.module.mailLog.status">
<source>Result</source>
</trans-unit>

<!-- Model: MailTemplate -->
<trans-unit id="tx_maillogger_domain_model_mailtemplate">
Expand Down Expand Up @@ -142,6 +145,12 @@
<trans-unit id="tx_maillogger_domain_model_maillog.result">
<source>Result</source>
</trans-unit>
<trans-unit id="tx_maillogger.module.mailLog.result_message">
<source>Result</source>
</trans-unit>
<trans-unit id="tx_maillogger_domain_model_maillog.status">
<source>Status</source>
</trans-unit>
<trans-unit id="tx_maillogger_domain_model_maillog.headers">
<source>Headers</source>
</trans-unit>
Expand Down
1 change: 1 addition & 0 deletions ext_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ CREATE TABLE tx_maillogger_domain_model_maillog (
mail_copy varchar(500) DEFAULT '' NOT NULL,
mail_blind_copy varchar(500) DEFAULT '' NOT NULL,
result varchar(500) DEFAULT '' NOT NULL,
status int(11) unsigned DEFAULT '0' NOT NULL,
headers text NOT NULL,
sys_language_uid int(11) DEFAULT '0' NOT NULL,

Expand Down

0 comments on commit 580deee

Please sign in to comment.