Skip to content

Commit

Permalink
[10.x] Improve log mail mailer output (#45620)
Browse files Browse the repository at this point in the history
* improve log mail mailer output

* wip

* cs
  • Loading branch information
SjorsO authored Jan 12, 2023
1 parent 57c79aa commit bef6396
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Mail/Transport/LogTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public function __construct(LoggerInterface $logger)
*/
public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
{
$this->logger->debug($message->toString());
$string = $message->toString();

if (str_contains($string, 'Content-Transfer-Encoding: quoted-printable')) {
$string = quoted_printable_decode($string);
}

$this->logger->debug($string);

return new SentMessage($message, $envelope ?? Envelope::create($message));
}
Expand Down
45 changes: 45 additions & 0 deletions tests/Mail/MailLogTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Illuminate\Tests\Mail;

use Illuminate\Mail\Message;
use Illuminate\Mail\Transport\LogTransport;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Orchestra\Testbench\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Mime\Email;

class MailLogTransportTest extends TestCase
{
Expand All @@ -33,6 +35,30 @@ public function testGetLogTransportWithConfiguredChannel()
$this->assertInstanceOf(StreamHandler::class, $handlers[0]);
}

public function testItDecodesTheMessageBeforeLogging()
{
$message = (new Message(new Email))
->from('noreply@example.com', 'no-reply')
->to('taylor@example.com', 'Taylor')
->html(<<<'BODY'
Hi,
<a href="https://example.com/reset-password=5e113c71a4c210aff04b3fa66f1b1299">Click here to reset your password</a>.
All the best,
Burt & Irving
BODY)
->text('A text part');

$actualLoggedValue = $this->getLoggedEmailMessage($message);

$this->assertStringNotContainsString("=\r\n", $actualLoggedValue);
$this->assertStringContainsString('href=', $actualLoggedValue);
$this->assertStringContainsString('Burt & Irving', $actualLoggedValue);
$this->assertStringContainsString('https://example.com/reset-password=5e113c71a4c210aff04b3fa66f1b1299', $actualLoggedValue);
}

public function testGetLogTransportWithPsrLogger()
{
$this->app['config']->set('mail.driver', 'log');
Expand All @@ -43,4 +69,23 @@ public function testGetLogTransportWithPsrLogger()

$this->assertEquals($logger, $transportLogger);
}

private function getLoggedEmailMessage(Message $message): string
{
$logger = new class extends NullLogger
{
public string $loggedValue = '';

public function log($level, string|\Stringable $message, array $context = []): void
{
$this->loggedValue = (string) $message;
}
};

(new LogTransport($logger))->send(
$message->getSymfonyMessage()
);

return $logger->loggedValue;
}
}

0 comments on commit bef6396

Please sign in to comment.