# EmailTarget

> EmailTarget sends selected log messages to the specified email addresses.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [log](/docs/api/yii/log/index.md) › EmailTarget

| class | `yii\log\EmailTarget` |
| --- | --- |
| Extends | [`Target`](/docs/api/yii/log/target/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/log/EmailTarget.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/log/EmailTarget.php#L46) |

EmailTarget sends selected log messages to the specified email addresses.

You may configure the email to be sent by setting the [`$message`](/docs/api/yii/log/emailtarget/index.md#property-message) property, through which
you can set the target email addresses, subject, etc.:

```
'components' => [
    'log' => [
         'targets' => [
             [
                 'class' => 'yii\log\EmailTarget',
                 'mailer' => 'mailer',
                 'levels' => ['error', 'warning'],
                 'message' => [
                     'from' => ['log@example.com'],
                     'to' => ['developer1@example.com', 'developer2@example.com'],
                     'subject' => 'Log message',
                 ],
             ],
         ],
    ],
],
```

In the above `mailer` is ID of the component that sends email and should be already configured.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-categories"></span>[`$categories`](/docs/api/yii/log/target/index.md#property-categories) | `list<string>` | List of message categories that this target is interested in. Default: `[]`. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-enabled"></span>[`$enabled`](/docs/api/yii/log/target/index.md#property-enabled) | `bool\|callable` | A boolean value or a callable to obtain the value from. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-except"></span>[`$except`](/docs/api/yii/log/target/index.md#property-except) | `list<string>` | List of message categories that this target is NOT interested in. Default: `[]`. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-exportInterval"></span>[`$exportInterval`](/docs/api/yii/log/target/index.md#property-exportInterval) | `int` | How many messages should be accumulated before they are exported. Default: `1000`. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-levels"></span>[`$levels`](/docs/api/yii/log/target/index.md#property-levels) | `array\|int` | Message levels that this target is interested in. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-logVars"></span>[`$logVars`](/docs/api/yii/log/target/index.md#property-logVars) | `list<string>` | List of the PHP predefined variables that should be logged in a message. Default: `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']`. | [`Target`](/docs/api/yii/log/target/index.md) |
| [`$mailer`](#property-mailer) | [`yii\mail\MailerInterface`](/docs/api/yii/mail/mailerinterface/index.md)`\|array\|string` | The mailer object or the application component ID of the mailer object. Default: `'mailer'`. | `EmailTarget` |
| <span id="property-maskVars"></span>[`$maskVars`](/docs/api/yii/log/target/index.md#property-maskVars) | `list<string>` | List of the PHP predefined variables that should NOT be logged "as is" and should always be replaced with a mask `***` before logging, when exist. Default: `['_SERVER.HTTP_AUTHORIZATION', '_SERVER.PHP_AUTH_USER', '_SERVER.PHP_AUTH_PW']`. Available since 2.0.16. | [`Target`](/docs/api/yii/log/target/index.md) |
| [`$message`](#property-message) | `array` | The configuration array for creating a [message](/docs/api/yii/mail/messageinterface/index.md) object. Default: `[]`. | `EmailTarget` |
| <span id="property-messages"></span>[`$messages`](/docs/api/yii/log/target/index.md#property-messages) | `list<yii\log\LogMessage>` | The messages that are retrieved from the logger so far by this log target. Default: `[]`. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-microtime"></span>[`$microtime`](/docs/api/yii/log/target/index.md#property-microtime) | `bool` | Whether to log time with microseconds. Default: `false`. Available since 2.0.13. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-prefix"></span>[`$prefix`](/docs/api/yii/log/target/index.md#property-prefix) | `callable(yii\log\LogMessage): string\|null` | A PHP callable that returns a string to be prefixed to every exported message. Default: `null`. | [`Target`](/docs/api/yii/log/target/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-collect"></span>[`collect()`](/docs/api/yii/log/target/index.md#method-collect) | Processes the given log messages. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`export()`](#method-export) | Sends log messages to specified email addresses. | `EmailTarget` |
| <span id="method-filterMessages"></span>[`filterMessages()`](/docs/api/yii/log/target/index.md#method-filterMessages) static | Filters the given messages according to their categories and levels. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-formatMessage"></span>[`formatMessage()`](/docs/api/yii/log/target/index.md#method-formatMessage) | Formats a log message for display as a string. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getEnabled"></span>[`getEnabled()`](/docs/api/yii/log/target/index.md#method-getEnabled) | Check whether the log target is enabled. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getLevels"></span>[`getLevels()`](/docs/api/yii/log/target/index.md#method-getLevels) |  | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getMessagePrefix"></span>[`getMessagePrefix()`](/docs/api/yii/log/target/index.md#method-getMessagePrefix) | Returns a string to be prefixed to the given message. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the object. | `EmailTarget` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-setEnabled"></span>[`setEnabled()`](/docs/api/yii/log/target/index.md#method-setEnabled) | Sets a value indicating whether this log target is enabled. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-setLevels"></span>[`setLevels()`](/docs/api/yii/log/target/index.md#method-setLevels) | Sets the message levels that this target is interested in. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`composeMessage()`](#method-composeMessage) protected | Composes a mail message with the given body content. | `EmailTarget` |
| <span id="method-formatMessageText"></span>[`formatMessageText()`](/docs/api/yii/log/target/index.md#method-formatMessageText) protected | Converts a log message value to text. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getContextMessage"></span>[`getContextMessage()`](/docs/api/yii/log/target/index.md#method-getContextMessage) protected | Generates the context information to be logged. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getTime"></span>[`getTime()`](/docs/api/yii/log/target/index.md#method-getTime) protected | Returns formatted ('Y-m-d H:i:s') timestamp for message. | [`Target`](/docs/api/yii/log/target/index.md) |

## Property details

### $mailer <span id="property-mailer"></span>

```php
public yii\mail\MailerInterface|array|string $mailer = 'mailer'
```

The mailer object or the application component ID of the mailer object.

After the EmailTarget object is created, if you want to change this property, you should only assign it
with a mailer object.
Starting from version 2.0.2, this can also be a configuration array for creating the object.

### $message <span id="property-message"></span>

```php
public array $message = []
```

The configuration array for creating a [message](/docs/api/yii/mail/messageinterface/index.md) object.

Note that the "to" option must be set, which specifies the destination email address(es).

## Method details

### export() <span id="method-export"></span>

```php
public function export(): mixed
```

Sends log messages to specified email addresses.

Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.

**Returns**

`mixed`

**Throws**

- [`LogRuntimeException`](/docs/api/yii/log/logruntimeexception/index.md)

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

### composeMessage() <span id="method-composeMessage"></span>

```php
protected function composeMessage(string $body): yii\mail\MessageInterface
```

Composes a mail message with the given body content.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$body` | `string` | The body content |

**Returns**

[`yii\mail\MessageInterface`](/docs/api/yii/mail/messageinterface/index.md): $message