0 follower

Final Class Yiisoft\Mailer\FileMailer

InheritanceYiisoft\Mailer\FileMailer » Yiisoft\Mailer\BaseMailer
ImplementsYiisoft\Mailer\MailerInterface

FileMailer is a mock mailer that save email messages in files instead of sending them.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Mailer\FileMailer
send() Sends the given email message. Child classes should implement {@see BaseMailer::sendMessage()} with the actual email sending logic. Yiisoft\Mailer\BaseMailer
sendMultiple() Sends multiple messages at once. Yiisoft\Mailer\BaseMailer

Protected Methods

Hide inherited methods

Method Description Defined By
sendMessage() Yiisoft\Mailer\FileMailer

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $path, callable|null $filenameCallback null, Yiisoft\Mailer\MessageSettings|null $messageSettings null, \Psr\EventDispatcher\EventDispatcherInterface|null $eventDispatcher null )
$path string

The path where message files located.

$filenameCallback callable|null

A PHP callback that return a file name which will be used to save the email message.

$messageSettings Yiisoft\Mailer\MessageSettings|null

The default and extra message settings.

$eventDispatcher \Psr\EventDispatcher\EventDispatcherInterface|null

The event dispatcher instance.

                public function __construct(
    private readonly string $path,
    ?callable $filenameCallback = null,
    ?MessageSettings $messageSettings = null,
    ?EventDispatcherInterface $eventDispatcher = null,
) {
    parent::__construct($messageSettings, $eventDispatcher);
    $this->filenameCallback = $filenameCallback;
}

            
send() public method

Defined in: Yiisoft\Mailer\BaseMailer::send()

Sends the given email message. Child classes should implement {@see BaseMailer::sendMessage()} with the actual email sending logic.

public void send ( Yiisoft\Mailer\MessageInterface $message )
$message Yiisoft\Mailer\MessageInterface

Email message instance to be sent

                final public function send(MessageInterface $message): void
{
    $message = $this->defaultMessageSettings?->applyTo($message) ?? $message;
    $event = $this->eventDispatcher?->dispatch(new BeforeSend($message));
    if ($event instanceof BeforeSend && $event->preventSendingMessage) {
        return;
    }
    $this->sendMessage($message);
    $this->eventDispatcher?->dispatch(new AfterSend($message));
}

            
sendMessage() protected method

protected void sendMessage ( Yiisoft\Mailer\MessageInterface $message )
$message Yiisoft\Mailer\MessageInterface

                protected function sendMessage(MessageInterface $message): void
{
    $filename = $this->path . DIRECTORY_SEPARATOR . $this->generateMessageFilename($message);
    $filepath = dirname($filename);
    if (!is_dir($filepath) && !mkdir($filepath, 0777, true) && !is_dir($filepath)) {
        throw new RuntimeException(sprintf('Directory "%s" was not created.', $filepath));
    }
    file_put_contents($filename, $message->__toString());
}

            
sendMultiple() public method

Defined in: Yiisoft\Mailer\BaseMailer::sendMultiple()

Sends multiple messages at once.

This method may be implemented by some mailers which support more efficient way of sending multiple messages in the same batch.

public Yiisoft\Mailer\SendResults sendMultiple ( Yiisoft\Mailer\MessageInterface[] $messages )
$messages Yiisoft\Mailer\MessageInterface[]

List of email messages, which should be sent.

return Yiisoft\Mailer\SendResults

The result object that contains all messages and errors for failed sent messages.

                final public function sendMultiple(array $messages): SendResults
{
    $successMessages = [];
    $failMessages = [];
    foreach ($messages as $message) {
        try {
            $this->send($message);
        } catch (Throwable $e) {
            $failMessages[] = ['message' => $message, 'error' => $e];
            continue;
        }
        $successMessages[] = $message;
    }
    return new SendResults($successMessages, $failMessages);
}