LoggerMail.php in Helper 8
File
src/Plugin/Mail/LoggerMail.php
View source
<?php
namespace Drupal\helper\Plugin\Mail;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Site\Settings;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LoggerMail implements MailInterface, ContainerFactoryPluginInterface {
protected $logger;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger) {
$this->logger = $logger;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.factory')
->get('mail'));
}
public function mail(array $message) {
$this->logger
->info('Mail sent to @to with subject %subject: <pre>@mail</pre>', [
'@to' => $message['to'],
'%subject' => $message['subject'],
'@mail' => $this
->formatMessage($message),
]);
return TRUE;
}
public function format(array $message) {
$message['body'] = implode("\n\n", $message['body']);
$message['body'] = MailFormatHelper::htmlToText($message['body']);
$message['body'] = MailFormatHelper::wrapMail($message['body']);
return $message;
}
protected function formatMessage(array $message) {
$mimeheaders = [];
$message['headers']['To'] = $message['to'];
foreach ($message['headers'] as $name => $value) {
$mimeheaders[] = $name . ': ' . Unicode::mimeHeaderEncode($value);
}
$line_endings = Settings::get('mail_line_endings', PHP_EOL);
$output = implode($line_endings, $mimeheaders) . $line_endings;
$output .= 'Subject: ' . $message['subject'] . $line_endings;
$output .= $line_endings;
$output .= preg_replace('@\\r?\\n@', $line_endings, $message['body']);
return $output;
}
}
Classes
Name |
Description |
LoggerMail |
Defines a mail backend that captures sent messages to the logger. |