Skip to content
Snippets Groups Projects
Commit 694fb6ce authored by Ivo  Van Geertruyen's avatar Ivo Van Geertruyen
Browse files

Issue #3411420: Add error message field and logging in case sending the email fails.

parent 7fb9362a
Branches
Tags
1 merge request!1Issue #3411420: Add error message field and logging in case sending the email fails.
......@@ -289,7 +289,27 @@ class SymfonyMailerLog extends ContentEntityBase implements SymfonyMailerLogInte
->setDescription(t('The time when the log entry was created.'))
->setDisplayConfigurable('view', TRUE);
$fields['error_message'] = BaseFieldDefinition::create('string')
->setLabel(t('Error message'))
->setRequired(FALSE)
->setDescription(t('The error message if sending the email failed.'))
->setDisplayConfigurable('view', TRUE);
return $fields;
}
/**
* {@inheritdoc}
*/
public function getErrorMessage(): string|null {
return $this->get('error_message');
}
/**
* {@inheritdoc}
*/
public function setErrorMessage(string $errorMessage) {
$this->set('error_message', $errorMessage);
}
}
......@@ -137,4 +137,19 @@ interface SymfonyMailerLogInterface extends ContentEntityInterface {
*/
public function setCreatedTime(int $timestamp): SymfonyMailerLogInterface;
/**
* Returns the error message if sending failed.
*
* @return string|null
* The error message or null if there was no error sending the email.
*/
public function getErrorMessage(): string|null;
/**
* Sets the error message if sending failed.
*
* @param $errorMessage
*/
public function setErrorMessage(string $errorMessage);
}
......@@ -85,12 +85,31 @@ class LogMail extends EmailAdjusterBase implements ContainerFactoryPluginInterfa
'langcode' => $email->getLangcode(),
]);
$log->save();
// Store the log entity on the $email so we can update it later when the
// 'errorMessage' is set (or left unset) after attempting to actually send
// the email.
$email->_symfony_mailer_log = $log;
}
catch (\Exception $ex) {
$this->getLogger('symfony_mail_log', $ex->getMessage());
}
}
/**
* React to EmailInterface::PHASE_POST_SEND.
*
* @param \Drupal\symfony_mailer\EmailInterface $email
* The email to process.
*/
public function postSend(EmailInterface $email) {
if ($log = $email->_symfony_mailer_log) {
if ($errorMessage = $email->getError())
$log->setErrorMessage(substr($errorMessage, 0, 255));
$log->save();
}
}
/**
* Render array of addresses as array of strings.
*
......
<?php
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Adds the
*/
function symfony_mailer_log_update_9100() {
$field_storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Error message'))
->setRequired(FALSE)
->setDescription(t('The error message if sending the email failed.'))
->setDisplayConfigurable('view', TRUE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('error_message', 'symfony_mailer_log', 'symfony_mailer_log', $field_storage_definition);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment