Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture HTML body for mails #617

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
barryvdh marked this conversation as resolved.
Show resolved Hide resolved
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) {
barryvdh marked this conversation as resolved.
Show resolved Hide resolved
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