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

Add body message to mail #603

Merged
merged 1 commit into from
Feb 20, 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
10 changes: 6 additions & 4 deletions demo/bridge/swiftmailer/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
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('<div>Here is the message itself</div>');

$mailer->send($message);

Expand Down
4 changes: 3 additions & 1 deletion demo/bridge/symfonymailer/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
$debugbarRenderer->setBaseUrl('../../../src/DebugBar/Resources');

$mailCollector = new SymfonyMailCollector();
$mailCollector->showMessageDetail();
$mailCollector->showMessageBody();
$debugbar->addCollector($mailCollector);
$logger = new MessagesCollector('mails');
$debugbar['messages']->aggregate($logger);
Expand Down Expand Up @@ -41,7 +43,7 @@ public function __toString(): string{ return 'null://'; }
//->replyTo('fabien@example.com')
//->priority(Email::PRIORITY_HIGH)
->subject('Wonderful Subject')
->text('Here is the message itself');
->html('<div>Here is the message itself</div>');

$mailer->send($email);

Expand Down
11 changes: 10 additions & 1 deletion src/DebugBar/Bridge/SwiftMailer/SwiftMailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,29 @@ 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();
foreach ($this->messagesLogger->getMessages() as $msg) {
$mails[] = array(
'to' => $this->formatTo($msg->getTo()),
'subject' => $msg->getSubject(),
'headers' => $msg->getHeaders()->toString()
'headers' => $msg->getHeaders()->toString(),
'body' => $this->showBody ? $msg->getBody() : null,
erikn69 marked this conversation as resolved.
Show resolved Hide resolved
);
}
return array(
Expand Down
9 changes: 9 additions & 0 deletions src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -30,6 +33,11 @@ public function showMessageDetail()
$this->showDetailed = true;
}

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

public function collect()
{
$mails = array();
Expand All @@ -43,6 +51,7 @@ public function collect()
}, $message->getTo()),
'subject' => $message->getSubject(),
'headers' => ($this->showDetailed ? $message : $message->getHeaders())->toString(),
'body' => $this->showBody ? $message->getBody()->bodyToString() : null,
);;
}

Expand Down
12 changes: 11 additions & 1 deletion src/DebugBar/Resources/widgets/mails/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
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.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 ? '' : $('<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() {
Expand Down