class DevelMailLogger in Devel Mail Logger 8
Defines a mail backend that saves emails to DB.
To enable, save a variable in settings.php (or otherwise) whose value can be as simple as:
$config['system.mail']['interface']['default'] = 'devel_mail_logger';
Plugin annotation
@Mail(
id = "devel_mail_logger",
label = @Translation("Devel DB Mail Logger"),
description = @Translation("Saves Mails to DB")
)
Hierarchy
- class \Drupal\devel_mail_logger\Plugin\Mail\DevelMailLogger implements MailInterface, ContainerFactoryPluginInterface
Expanded class hierarchy of DevelMailLogger
File
- src/
Plugin/ Mail/ DevelMailLogger.php, line 26
Namespace
Drupal\devel_mail_logger\Plugin\MailView source
class DevelMailLogger implements MailInterface, ContainerFactoryPluginInterface {
/**
* Drupal\Core\Database\Driver\mysql\Connection definition.
*
* @var \Drupal\Core\Database\Driver\mysql\Connection
*/
protected $database;
/**
* Constructs a new DevelMailLog object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Database\Connection $database
* A Database connection to use for reading and writing database data.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'));
}
/**
* {@inheritdoc}
*/
public function mail(array $message) {
$query = $this->database
->insert('devel_mail_logger')
->fields([
'timestamp' => \Drupal::time()
->getRequestTime(),
'recipient' => $message['to'],
'subject' => $message['subject'],
'message' => json_encode($message),
]);
$query
->execute();
return TRUE;
}
/**
* {@inheritdoc}
*/
public function format(array $message) {
// Join the body array into one string.
$message['body'] = implode("\n\n", $message['body']);
// Convert any HTML to plain-text.
$message['body'] = MailFormatHelper::htmlToText($message['body']);
// Wrap the mail body for sending.
$message['body'] = MailFormatHelper::wrapMail($message['body']);
return $message;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DevelMailLogger:: |
protected | property | Drupal\Core\Database\Driver\mysql\Connection definition. | |
DevelMailLogger:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
DevelMailLogger:: |
public | function |
Formats a message prior to sending. Overrides MailInterface:: |
|
DevelMailLogger:: |
public | function |
Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail(). Overrides MailInterface:: |
|
DevelMailLogger:: |
public | function | Constructs a new DevelMailLog object. |