master

laravel/framework

Last updated at: 29/12/2023 09:24

LogTransport.php

TLDR

The LogTransport.php file is a part of the Illuminate\Mail\Transport namespace in the Demo Projects project. It contains a class called LogTransport that implements the Stringable and TransportInterface interfaces. This class is used for logging email messages.

Methods

send

This method sends the provided RawMessage object as an email. It logs the message body using the logger and returns a SentMessage object.

logger

This method returns the logger instance used by the LogTransport class.

__toString

This method returns the string representation of the transport, which is "log".

<?php

namespace Illuminate\Mail\Transport;

use Psr\Log\LoggerInterface;
use Stringable;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mime\RawMessage;

class LogTransport implements Stringable, TransportInterface
{
    /**
     * The Logger instance.
     *
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * Create a new log transport instance.
     *
     * @param  \Psr\Log\LoggerInterface  $logger
     * @return void
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
    {
        $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));
    }

    /**
     * Get the logger for the LogTransport instance.
     *
     * @return \Psr\Log\LoggerInterface
     */
    public function logger()
    {
        return $this->logger;
    }

    /**
     * Get the string representation of the transport.
     *
     * @return string
     */
    public function __toString(): string
    {
        return 'log';
    }
}