From a8694a4e0ee2291d13de5ff1d700d94393366511 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Tue, 20 Feb 2024 04:16:27 -0500 Subject: [PATCH] Add body message to mail (#603) --- demo/bridge/swiftmailer/index.php | 10 ++++++---- demo/bridge/symfonymailer/index.php | 4 +++- .../Bridge/SwiftMailer/SwiftMailCollector.php | 11 ++++++++++- src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php | 9 +++++++++ src/DebugBar/Resources/widgets/mails/widget.js | 12 +++++++++++- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/demo/bridge/swiftmailer/index.php b/demo/bridge/swiftmailer/index.php index 8dc78ec..56abaec 100644 --- a/demo/bridge/swiftmailer/index.php +++ b/demo/bridge/swiftmailer/index.php @@ -8,15 +8,17 @@ $debugbarRenderer->setBaseUrl('../../../src/DebugBar/Resources'); use DebugBar\Bridge\SwiftMailer\SwiftLogCollector; 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->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')) ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name')) - ->setBody('Here is the message itself'); + ->setBody('
Here is the message itself
'); $mailer->send($message); diff --git a/demo/bridge/symfonymailer/index.php b/demo/bridge/symfonymailer/index.php index f266efa..557aa49 100644 --- a/demo/bridge/symfonymailer/index.php +++ b/demo/bridge/symfonymailer/index.php @@ -14,6 +14,8 @@ include __DIR__ . '/../../bootstrap.php'; $debugbarRenderer->setBaseUrl('../../../src/DebugBar/Resources'); $mailCollector = new SymfonyMailCollector(); +$mailCollector->showMessageDetail(); +$mailCollector->showMessageBody(); $debugbar->addCollector($mailCollector); $logger = new MessagesCollector('mails'); $debugbar['messages']->aggregate($logger); @@ -41,7 +43,7 @@ $email = (new Email()) //->replyTo('fabien@example.com') //->priority(Email::PRIORITY_HIGH) ->subject('Wonderful Subject') - ->text('Here is the message itself'); + ->html('
Here is the message itself
'); $mailer->send($email); diff --git a/src/DebugBar/Bridge/SwiftMailer/SwiftMailCollector.php b/src/DebugBar/Bridge/SwiftMailer/SwiftMailCollector.php index 01a5e90..eeec1a8 100644 --- a/src/DebugBar/Bridge/SwiftMailer/SwiftMailCollector.php +++ b/src/DebugBar/Bridge/SwiftMailer/SwiftMailCollector.php @@ -25,12 +25,20 @@ class SwiftMailCollector extends DataCollector implements Renderable, AssetProvi { protected $messagesLogger; + /** @var bool */ + private $showBody = false; + public function __construct(Swift_Mailer $mailer) { $this->messagesLogger = new Swift_Plugins_MessageLogger(); $mailer->registerPlugin($this->messagesLogger); } + public function showMessageBody() + { + $this->showBody = true; + } + public function collect() { $mails = array(); @@ -38,7 +46,8 @@ class SwiftMailCollector extends DataCollector implements Renderable, AssetProvi $mails[] = array( 'to' => $this->formatTo($msg->getTo()), 'subject' => $msg->getSubject(), - 'headers' => $msg->getHeaders()->toString() + 'headers' => $msg->getHeaders()->toString(), + 'body' => $this->showBody ? $msg->getBody() : null, ); } return array( diff --git a/src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php b/src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php index 7095e8d..a7f4064 100644 --- a/src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php +++ b/src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php @@ -19,6 +19,9 @@ class SymfonyMailCollector extends DataCollector implements Renderable, AssetPro /** @var bool */ private $showDetailed = false; + /** @var bool */ + private $showBody = false; + /** @param \Symfony\Component\Mailer\SentMessage $message */ public function addSymfonyMessage($message) { @@ -30,6 +33,11 @@ class SymfonyMailCollector extends DataCollector implements Renderable, AssetPro $this->showDetailed = true; } + public function showMessageBody() + { + $this->showBody = true; + } + public function collect() { $mails = array(); @@ -43,6 +51,7 @@ class SymfonyMailCollector extends DataCollector implements Renderable, AssetPro }, $message->getTo()), 'subject' => $message->getSubject(), 'headers' => ($this->showDetailed ? $message : $message->getHeaders())->toString(), + 'body' => $this->showBody ? $message->getBody()->bodyToString() : null, );; } diff --git a/src/DebugBar/Resources/widgets/mails/widget.js b/src/DebugBar/Resources/widgets/mails/widget.js index 7a1a694..dcc6cf5 100644 --- a/src/DebugBar/Resources/widgets/mails/widget.js +++ b/src/DebugBar/Resources/widgets/mails/widget.js @@ -16,7 +16,17 @@ this.$list = new PhpDebugBar.Widgets.ListWidget({ itemRenderer: function(li, mail) { $('').addClass(csscls('subject')).text(mail.subject).appendTo(li); $('').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 ? '' : $('
')
+                            .append($('').text(mail.headers)).prop('outerHTML');
+                        documentToWriteTo.open();
+                        documentToWriteTo.write(headers + mail.body);
+                        documentToWriteTo.close();
+                    });
+                } else if (mail.headers) {
                     var headers = $('
').addClass(csscls('headers')).appendTo(li);
                     $('').text(mail.headers).appendTo(headers);
                     li.click(function() {