Abstract Class Yiisoft\Mailer\BaseMailer
| Inheritance | Yiisoft\Mailer\BaseMailer |
|---|---|
| Implements | Yiisoft\Mailer\MailerInterface |
| Subclasses | Yiisoft\Mailer\FileMailer |
BaseMailer serves as a base class that implements the basic functions required by Yiisoft\Mailer\MailerInterface.
Concrete child classes may focus on implementing the Yiisoft\Mailer\BaseMailer::sendMessage() method.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Mailer\BaseMailer | |
| send() | Sends the given email message. Child classes should implement Yiisoft\Mailer\BaseMailer::sendMessage() with the actual email sending logic. | Yiisoft\Mailer\BaseMailer |
| sendMultiple() | Sends multiple messages at once. | Yiisoft\Mailer\BaseMailer |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| sendMessage() | Sends the specified message. | Yiisoft\Mailer\BaseMailer |
Method Details
| public __construct( Yiisoft\Mailer\MessageSettings|null $defaultMessageSettings = null, \Psr\EventDispatcher\EventDispatcherInterface|null $eventDispatcher = null ): mixed | ||
| $defaultMessageSettings | Yiisoft\Mailer\MessageSettings|null | |
| $eventDispatcher | \Psr\EventDispatcher\EventDispatcherInterface|null | |
public function __construct(
private readonly ?MessageSettings $defaultMessageSettings = null,
private readonly ?EventDispatcherInterface $eventDispatcher = null,
) {}
Sends the given email message. Child classes should implement Yiisoft\Mailer\BaseMailer::sendMessage() with the actual email sending logic.
| public send( Yiisoft\Mailer\MessageInterface $message ): void | ||
| $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));
}
Sends the specified message.
This method should be implemented by child classes with the actual email sending logic.
| protected abstract sendMessage( Yiisoft\Mailer\MessageInterface $message ): void | ||
| $message | Yiisoft\Mailer\MessageInterface |
The message to be sent |
abstract protected function sendMessage(MessageInterface $message): void;
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 sendMultiple( Yiisoft\Mailer\MessageInterface[] $messages ): Yiisoft\Mailer\SendResults | ||
| $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);
}
Signup or Login in order to comment.