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 {@see MailerInterface}.
Concrete child classes may focus on implementing the {@see \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 {@see 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 mixed __construct ( ?\Yiisoft\Mailer\MessageSettings $defaultMessageSettings = null, ?\Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher = null ) | ||
| $defaultMessageSettings | ?\Yiisoft\Mailer\MessageSettings | |
| $eventDispatcher | ?\Psr\EventDispatcher\EventDispatcherInterface | |
public function __construct(
private readonly ?MessageSettings $defaultMessageSettings = null,
private readonly ?EventDispatcherInterface $eventDispatcher = null,
) {}
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));
}
Sends the specified message.
This method should be implemented by child classes with the actual email sending logic.
| protected abstract void sendMessage ( Yiisoft\Mailer\MessageInterface $message ) | ||
| $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 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);
}
Signup or Login in order to comment.