Skip to content

Commit

Permalink
Capture HTML body for mails (#617)
Browse files Browse the repository at this point in the history
* Capture HTML body for mails

* Show html in iframe

* Deprecate details

* Add text summary
  • Loading branch information
barryvdh authored Mar 12, 2024
1 parent bce78f9 commit 8c55824
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/DebugBar/Bridge/SwiftMailer/SwiftMailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function __construct(Swift_Mailer $mailer)
$mailer->registerPlugin($this->messagesLogger);
}

public function showMessageBody()
public function showMessageBody($show = true)
{
$this->showBody = true;
$this->showBody = $show;
}

public function collect()
Expand Down
31 changes: 24 additions & 7 deletions src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use Symfony\Component\Mime\Part\AbstractPart;

/**
* Collects data about sent mail events
Expand All @@ -28,14 +29,17 @@ public function addSymfonyMessage($message)
$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()
Expand All @@ -44,15 +48,28 @@ public function collect()

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(
Expand Down
61 changes: 37 additions & 24 deletions src/DebugBar/Resources/widgets/mails/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8c55824

Please sign in to comment.