Created
November 26, 2018 23:24
-
-
Save gheydon/a4044d19970b88a719fc789523c9aab7 to your computer and use it in GitHub Desktop.
Drupal 8 - Display communication from swiftmailer with the MTA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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