1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-06-07 14:45:02 +02:00

Capture HTML body for mails (#617)

* Capture HTML body for mails

* Show html in iframe

* Deprecate details

* Add text summary
This commit is contained in:
Barry vd. Heuvel 2024-03-12 10:54:01 +01:00 committed by GitHub
parent bce78f928a
commit 8c55824b60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 63 additions and 33 deletions

View File

@ -34,9 +34,9 @@ class SwiftMailCollector extends DataCollector implements Renderable, AssetProvi
$mailer->registerPlugin($this->messagesLogger); $mailer->registerPlugin($this->messagesLogger);
} }
public function showMessageBody() public function showMessageBody($show = true)
{ {
$this->showBody = true; $this->showBody = $show;
} }
public function collect() public function collect()

View File

@ -5,6 +5,7 @@ namespace DebugBar\Bridge\Symfony;
use DebugBar\DataCollector\AssetProvider; use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector; use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable; use DebugBar\DataCollector\Renderable;
use Symfony\Component\Mime\Part\AbstractPart;
/** /**
* Collects data about sent mail events * Collects data about sent mail events
@ -28,14 +29,17 @@ class SymfonyMailCollector extends DataCollector implements Renderable, AssetPro
$this->messages[] = $message->getOriginalMessage(); $this->messages[] = $message->getOriginalMessage();
} }
/**
* @deprecated use showMessageBody()
*/
public function showMessageDetail() public function showMessageDetail()
{ {
$this->showDetailed = true; $this->showMessageBody(true);
} }
public function showMessageBody() public function showMessageBody($show = true)
{ {
$this->showBody = true; $this->showBody = $show;
} }
public function collect() public function collect()
@ -44,15 +48,28 @@ class SymfonyMailCollector extends DataCollector implements Renderable, AssetPro
foreach ($this->messages as $message) { foreach ($this->messages as $message) {
/* @var \Symfony\Component\Mime\Message $message */ /* @var \Symfony\Component\Mime\Message $message */
$mails[] = array( $mail = [
'to' => array_map(function ($address) { 'to' => array_map(function ($address) {
/* @var \Symfony\Component\Mime\Address $address */ /* @var \Symfony\Component\Mime\Address $address */
return $address->toString(); return $address->toString();
}, $message->getTo()), }, $message->getTo()),
'subject' => $message->getSubject(), 'subject' => $message->getSubject(),
'headers' => ($this->showDetailed ? $message : $message->getHeaders())->toString(), 'headers' => $message->getHeaders()->toString(),
'body' => $this->showBody ? $message->getBody()->bodyToString() : null, 'body' => null,
);; 'html' => null,
];
if ($this->showBody) {
$body = $message->getBody();
if ($body instanceof AbstractPart) {
$mail['html'] = $message->getHtmlBody();
$mail['body'] = $message->getTextBody();
} else {
$mail['body'] = $body->bodyToString();
}
}
$mails[] = $mail;
} }
return array( return array(

View File

@ -14,30 +14,43 @@
render: function() { render: function() {
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.body) { if (mail.html || mail.html) {
li.click(function() { var header = $('<span />').addClass(csscls('filename')).text('');
var popup = window.open('about:blank', 'Mail Preview', 'width=650,height=440,scrollbars=yes'); $('<a title="Mail Preview">View Mail</a>').on('click', function () {
var documentToWriteTo = popup.document; var popup = window.open('about:blank', 'Mail Preview', 'width=650,height=440,scrollbars=yes');
var headers = !mail.headers ? '' : $('<pre style="border: 1px solid #ddd; padding: 5px;" />') var documentToWriteTo = popup.document;
.append($('<code />').text(mail.headers)).prop('outerHTML'); var headers = !mail.headers ? '' : $('<pre style="border: 1px solid #ddd; padding: 5px;" />')
documentToWriteTo.open(); .append($('<code />').text(mail.headers));
documentToWriteTo.write(headers + mail.body);
documentToWriteTo.close(); var body = $('<pre style="border: 1px solid #ddd; padding: 5px;" />').text(mail.body)
}); var html = null;
} else if (mail.headers) { if (mail.html) {
var headers = $('<pre />').addClass(csscls('headers')).appendTo(li); body = $('<details />').append($('<summary>Text version</summary>')).append(body);
$('<code />').text(mail.headers).appendTo(headers); html = $('<iframe width="100%" height="400px" sandbox="" referrerpolicy="no-referrer"/>').attr("srcdoc", mail.html)
li.click(function() { }
if (headers.is(':visible')) {
headers.hide(); documentToWriteTo.open();
} else { documentToWriteTo.write(headers.prop('outerHTML') + body.prop('outerHTML') + (html ? html.prop('outerHTML') : ''));
headers.show(); documentToWriteTo.close();
} }).addClass(csscls('editor-link')).appendTo(header);
});
} header.appendTo(li);
}}); }
if (mail.headers) {
var headers = $('<pre />').addClass(csscls('headers')).appendTo(li);
$('<code />').text(mail.headers).appendTo(headers);
li.click(function() {
if (headers.is(':visible')) {
headers.hide();
} else {
headers.show();
}
});
}
}});
this.$list.$el.appendTo(this.$el); this.$list.$el.appendTo(this.$el);
this.bindAttr('data', function(data) { this.bindAttr('data', function(data) {