Skip to content

Instantly share code, notes, and snippets.

@gheydon
Created November 26, 2018 23:24
Show Gist options
  • Save gheydon/a4044d19970b88a719fc789523c9aab7 to your computer and use it in GitHub Desktop.
Save gheydon/a4044d19970b88a719fc789523c9aab7 to your computer and use it in GitHub Desktop.
Drupal 8 - Display communication from swiftmailer with the MTA
<?php
use Drupal\example\DrupalSwiftMailerMessageLogger;
/**
* Implements hook_swiftmailer().
*/
function example_swiftmailer_alter($mailer, $m, $message) {
$logger = new DrupalSwiftMailerMessageLogger(500);
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
}
<?php
namespace Drupal\example;
use Drupal\Core\Messenger\MessengerTrait;
/**
* Class DrupalSwiftMailerMessageLogger.
*
* @package Drupal\pfsamplesaustr_profile
*/
class DrupalSwiftMailerMessageLogger implements \Swift_Plugins_Logger {
use MessengerTrait;
/**
* Messages.
*
* @var array
*/
private $log;
/**
* Max lines.
*
* @var int
*/
private $size = 0;
/**
* DrupalSwiftMailerMessageLogger constructor.
*/
public function __construct($size = 50) {
$this->size = $size;
}
/**
* {@inheritdoc}
*/
public function add($entry) {
$this->log[] = $entry;
while (count($this->log) > $this->size) {
array_shift($this->log);
}
}
/**
* {@inheritdoc}
*/
public function dump() {
$this->messenger()->addMessage(json_encode($this->log));
}
/**
* {@inheritdoc}
*/
public function clear() {
$this->log = [];
}
/**
* Display log.
*/
public function __destruct() {
$this->dump();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment