0 follower

Final Class Yiisoft\Log\Target\Email\EmailTarget

InheritanceYiisoft\Log\Target\Email\EmailTarget » Yiisoft\Log\Target

EmailTarget sends selected log messages to the specified email addresses.

{@see \Yiisoft\Log\Target\Email\EmailTarget::$mailer} is instance of {@see \Yiisoft\Mailer\MailerInterface} that sends email and should be already configured.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Log\Target\Email\EmailTarget

Protected Methods

Hide inherited methods

Method Description Defined By
export() Sends log messages to specified email addresses. Yiisoft\Log\Target\Email\EmailTarget

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Mailer\MailerInterface $mailer, string|string[] $emailTo, string $subjectEmail '', string[] $levels = [] )
$mailer \Yiisoft\Mailer\MailerInterface

The mailer instance.

$emailTo string|string[]

The receiver email address. You may pass an array of addresses if multiple recipients should receive this message. You may also specify receiver name in addition to email address using format: [email => name].

$subjectEmail string

The email message subject.

$levels string[]

The {@see \Psr\Log\LogLevel log message levels} that this target is interested in.

throws InvalidArgumentException

If the "to" email message argument is invalid.

                public function __construct(
    private MailerInterface $mailer,
    array|string $emailTo,
    string $subjectEmail = '',
    array $levels = []
) {
    if (empty($emailTo)) {
        throw new InvalidArgumentException('The "to" argument must be an array or string and must not be empty.');
    }
    $this->emailTo = $emailTo;
    $this->subjectEmail = $subjectEmail ?: 'Application Log';
    parent::__construct($levels);
}

            
export() protected method

Sends log messages to specified email addresses.

protected void export ( )
throws RuntimeException

If the log cannot be exported.

                protected function export(): void
{
    $message = $this->mailer
        ->compose()
        ->withTo($this->emailTo)
        ->withSubject($this->subjectEmail)
        ->withTextBody(wordwrap($this->formatMessages("\n"), 70))
    ;
    try {
        $this->mailer->send($message);
    } catch (Throwable $e) {
        throw new RuntimeException('Unable to export log through email.', 0, $e);
    }
}