1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-17 05:18:32 +01:00

Add body message to mail (#603)

This commit is contained in:
erikn69 2024-02-20 04:16:27 -05:00 committed by GitHub
parent 9321913520
commit a8694a4e0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 7 deletions

View File

@ -8,15 +8,17 @@ $debugbarRenderer->setBaseUrl('../../../src/DebugBar/Resources');
use DebugBar\Bridge\SwiftMailer\SwiftLogCollector; use DebugBar\Bridge\SwiftMailer\SwiftLogCollector;
use DebugBar\Bridge\SwiftMailer\SwiftMailCollector; use DebugBar\Bridge\SwiftMailer\SwiftMailCollector;
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance()); $mailer = new Swift_Mailer(new Swift_NullTransport());
$debugbar['messages']->aggregate(new SwiftLogCollector($mailer)); $debugbar['messages']->aggregate(new SwiftLogCollector($mailer));
$debugbar->addCollector(new SwiftMailCollector($mailer)); $mailCollector = new SwiftMailCollector($mailer);
$mailCollector->showMessageBody();
$debugbar->addCollector($mailCollector);
$message = Swift_Message::newInstance('Wonderful Subject') $message = (new Swift_Message('Wonderful Subject'))
->setFrom(array('john@doe.com' => 'John Doe')) ->setFrom(array('john@doe.com' => 'John Doe'))
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name')) ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
->setBody('Here is the message itself'); ->setBody('<div>Here is the message itself</div>');
$mailer->send($message); $mailer->send($message);

View File

@ -14,6 +14,8 @@ include __DIR__ . '/../../bootstrap.php';
$debugbarRenderer->setBaseUrl('../../../src/DebugBar/Resources'); $debugbarRenderer->setBaseUrl('../../../src/DebugBar/Resources');
$mailCollector = new SymfonyMailCollector(); $mailCollector = new SymfonyMailCollector();
$mailCollector->showMessageDetail();
$mailCollector->showMessageBody();
$debugbar->addCollector($mailCollector); $debugbar->addCollector($mailCollector);
$logger = new MessagesCollector('mails'); $logger = new MessagesCollector('mails');
$debugbar['messages']->aggregate($logger); $debugbar['messages']->aggregate($logger);
@ -41,7 +43,7 @@ $email = (new Email())
//->replyTo('fabien@example.com') //->replyTo('fabien@example.com')
//->priority(Email::PRIORITY_HIGH) //->priority(Email::PRIORITY_HIGH)
->subject('Wonderful Subject') ->subject('Wonderful Subject')
->text('Here is the message itself'); ->html('<div>Here is the message itself</div>');
$mailer->send($email); $mailer->send($email);

View File

@ -25,12 +25,20 @@ class SwiftMailCollector extends DataCollector implements Renderable, AssetProvi
{ {
protected $messagesLogger; protected $messagesLogger;
/** @var bool */
private $showBody = false;
public function __construct(Swift_Mailer $mailer) public function __construct(Swift_Mailer $mailer)
{ {
$this->messagesLogger = new Swift_Plugins_MessageLogger(); $this->messagesLogger = new Swift_Plugins_MessageLogger();
$mailer->registerPlugin($this->messagesLogger); $mailer->registerPlugin($this->messagesLogger);
} }
public function showMessageBody()
{
$this->showBody = true;
}
public function collect() public function collect()
{ {
$mails = array(); $mails = array();
@ -38,7 +46,8 @@ class SwiftMailCollector extends DataCollector implements Renderable, AssetProvi
$mails[] = array( $mails[] = array(
'to' => $this->formatTo($msg->getTo()), 'to' => $this->formatTo($msg->getTo()),
'subject' => $msg->getSubject(), 'subject' => $msg->getSubject(),
'headers' => $msg->getHeaders()->toString() 'headers' => $msg->getHeaders()->toString(),
'body' => $this->showBody ? $msg->getBody() : null,
); );
} }
return array( return array(

View File

@ -19,6 +19,9 @@ class SymfonyMailCollector extends DataCollector implements Renderable, AssetPro
/** @var bool */ /** @var bool */
private $showDetailed = false; private $showDetailed = false;
/** @var bool */
private $showBody = false;
/** @param \Symfony\Component\Mailer\SentMessage $message */ /** @param \Symfony\Component\Mailer\SentMessage $message */
public function addSymfonyMessage($message) public function addSymfonyMessage($message)
{ {
@ -30,6 +33,11 @@ class SymfonyMailCollector extends DataCollector implements Renderable, AssetPro
$this->showDetailed = true; $this->showDetailed = true;
} }
public function showMessageBody()
{
$this->showBody = true;
}
public function collect() public function collect()
{ {
$mails = array(); $mails = array();
@ -43,6 +51,7 @@ class SymfonyMailCollector extends DataCollector implements Renderable, AssetPro
}, $message->getTo()), }, $message->getTo()),
'subject' => $message->getSubject(), 'subject' => $message->getSubject(),
'headers' => ($this->showDetailed ? $message : $message->getHeaders())->toString(), 'headers' => ($this->showDetailed ? $message : $message->getHeaders())->toString(),
'body' => $this->showBody ? $message->getBody()->bodyToString() : null,
);; );;
} }

View File

@ -16,7 +16,17 @@
this.$list = new PhpDebugBar.Widgets.ListWidget({ itemRenderer: function(li, mail) { this.$list = new PhpDebugBar.Widgets.ListWidget({ itemRenderer: function(li, mail) {
$('<span />').addClass(csscls('subject')).text(mail.subject).appendTo(li); $('<span />').addClass(csscls('subject')).text(mail.subject).appendTo(li);
$('<span />').addClass(csscls('to')).text(mail.to).appendTo(li); $('<span />').addClass(csscls('to')).text(mail.to).appendTo(li);
if (mail.headers) { if (mail.body) {
li.click(function() {
var popup = window.open('about:blank', 'Mail Preview', 'width=650,height=440,scrollbars=yes');
var documentToWriteTo = popup.document;
var headers = !mail.headers ? '' : $('<pre style="border: 1px solid #ddd; padding: 5px;" />')
.append($('<code />').text(mail.headers)).prop('outerHTML');
documentToWriteTo.open();
documentToWriteTo.write(headers + mail.body);
documentToWriteTo.close();
});
} else if (mail.headers) {
var headers = $('<pre />').addClass(csscls('headers')).appendTo(li); var headers = $('<pre />').addClass(csscls('headers')).appendTo(li);
$('<code />').text(mail.headers).appendTo(headers); $('<code />').text(mail.headers).appendTo(headers);
li.click(function() { li.click(function() {