1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-06-06 06:05:24 +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);
}
public function showMessageBody()
public function showMessageBody($show = true)
{
$this->showBody = true;
$this->showBody = $show;
}
public function collect()

View File

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

View File

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