# BaseMailer

> BaseMailer serves as a base class that implements the basic functions required by yii\mail\MailerInterface.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [mail](/docs/api/yii/mail/index.md) › BaseMailer

| abstract class | `yii\mail\BaseMailer` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`MailerInterface`](/docs/api/yii/mail/mailerinterface/index.md), [`yii\base\ViewContextInterface`](/docs/api/yii/base/viewcontextinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/mail/BaseMailer.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/mail/BaseMailer.php#L35) |

BaseMailer serves as a base class that implements the basic functions required by [`MailerInterface`](/docs/api/yii/mail/mailerinterface/index.md).

Concrete child classes should may focus on implementing the [`sendMessage()`](/docs/api/yii/mail/basemailer/index.md#method-sendMessage) method.

See also: [`BaseMessage`](/docs/api/yii/mail/basemessage/index.md) (For more details and usage information on BaseMailer, see the [guide article on mailing](/docs/guide/tutorial-mailing/index.md).).

## 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) |
| [`$fileTransportCallback`](#property-fileTransportCallback) | `callable\|null` | A PHP callback that will be called by [`send()`](/docs/api/yii/mail/basemailer/index.md#method-send) when [`$useFileTransport`](/docs/api/yii/mail/basemailer/index.md#property-useFileTransport) is true. Default: `null`. | `BaseMailer` |
| <span id="property-fileTransportPath"></span>`$fileTransportPath` | `string` | The directory where the email messages are saved when [`$useFileTransport`](/docs/api/yii/mail/basemailer/index.md#property-useFileTransport) is true. Default: `'@runtime/mail'`. | `BaseMailer` |
| [`$htmlLayout`](#property-htmlLayout) | `string\|bool` | HTML layout view name. Default: `'layouts/html'`. | `BaseMailer` |
| <span id="property-messageClass"></span>`$messageClass` | `string` | The default class name of the new message instances created by [`createMessage()`](/docs/api/yii/mail/basemailer/index.md#method-createMessage) Default: `'yii\mail\BaseMessage'`. | `BaseMailer` |
| [`$messageConfig`](#property-messageConfig) | `array` | The configuration that should be applied to any newly created email message instance by [`createMessage()`](/docs/api/yii/mail/basemailer/index.md#method-createMessage) or [`compose()`](/docs/api/yii/mail/basemailer/index.md#method-compose). Default: `[]`. | `BaseMailer` |
| [`$textLayout`](#property-textLayout) | `string\|bool` | Text layout view name. Default: `'layouts/text'`. | `BaseMailer` |
| [`$useFileTransport`](#property-useFileTransport) | `bool` | Whether to save email messages as files under [`$fileTransportPath`](/docs/api/yii/mail/basemailer/index.md#property-fileTransportPath) instead of sending them to the actual recipients. Default: `false`. | `BaseMailer` |
| <span id="property-view"></span>`$view` | `array\|`[`yii\web\View`](/docs/api/yii/web/view/index.md) | View instance or its array configuration that will be used to render message bodies. | `BaseMailer` |
| <span id="property-viewPath"></span>`$viewPath` | `string` | The directory that contains the view files for composing mail messages Defaults to '@app/mail'. | `BaseMailer` |

## 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) |
| [`afterSend()`](#method-afterSend) | This method is invoked right after mail was send. | `BaseMailer` |
| <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) |
| [`beforeSend()`](#method-beforeSend) | This method is invoked right before mail send. | `BaseMailer` |
| <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) |
| [`compose()`](#method-compose) | Creates a new message instance and optionally composes its body content via view rendering. | `BaseMailer` |
| <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) |
| [`generateMessageFileName()`](#method-generateMessageFileName) |  | `BaseMailer` |
| <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) |
| [`getView()`](#method-getView) |  | `BaseMailer` |
| [`getViewPath()`](#method-getViewPath) |  | `BaseMailer` |
| <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) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <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) |
| [`render()`](#method-render) | Renders the specified view with optional parameters and layout. | `BaseMailer` |
| [`send()`](#method-send) | Sends the given email message. | `BaseMailer` |
| [`sendMultiple()`](#method-sendMultiple) | Sends multiple messages at once. | `BaseMailer` |
| [`setView()`](#method-setView) |  | `BaseMailer` |
| [`setViewPath()`](#method-setViewPath) |  | `BaseMailer` |
| <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) |
| [`createMessage()`](#method-createMessage) protected | Creates a new message instance. | `BaseMailer` |
| [`createView()`](#method-createView) protected | Creates view instance from given configuration. | `BaseMailer` |
| [`saveMessage()`](#method-saveMessage) protected | Saves the message as a file under [`$fileTransportPath`](/docs/api/yii/mail/basemailer/index.md#property-fileTransportPath). | `BaseMailer` |
| [`sendMessage()`](#method-sendMessage) protected abstract | Sends the specified message. | `BaseMailer` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_BEFORE_SEND"></span>`EVENT_BEFORE_SEND` | [`MailEvent`](/docs/api/yii/mail/mailevent/index.md) | An event raised right before send. Event name: `'beforeSend'`. | `BaseMailer` |
| <span id="event-EVENT_AFTER_SEND"></span>`EVENT_AFTER_SEND` | [`MailEvent`](/docs/api/yii/mail/mailevent/index.md) | An event raised right after send. Event name: `'afterSend'`. | `BaseMailer` |

## Property details

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

```php
public callable|null $fileTransportCallback = null
```

A PHP callback that will be called by [`send()`](/docs/api/yii/mail/basemailer/index.md#method-send) when [`$useFileTransport`](/docs/api/yii/mail/basemailer/index.md#property-useFileTransport) is true.

The callback should return a file name which will be used to save the email message.
If not set, the file name will be generated based on the current timestamp.

The signature of the callback is:

```
function ($mailer, $message)
```

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

```php
public string|bool $htmlLayout = 'layouts/html'
```

HTML layout view name.

This is the layout used to render HTML mail body.
The property can take the following values:

- a relative view name: a view file relative to [`$viewPath`](/docs/api/yii/mail/basemailer/index.md#property-viewPath), e.g., 'layouts/html'.
- a [path alias](/docs/guide/concept-aliases/index.md): an absolute view file path specified as a path alias, e.g., '@app/mail/html'.
- a boolean false: the layout is disabled.

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

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

The configuration that should be applied to any newly created email message instance by [`createMessage()`](/docs/api/yii/mail/basemailer/index.md#method-createMessage) or [`compose()`](/docs/api/yii/mail/basemailer/index.md#method-compose).

Any valid property defined
by [`MessageInterface`](/docs/api/yii/mail/messageinterface/index.md) can be configured, such as `from`, `to`, `subject`, `textBody`, `htmlBody`, etc.

For example:

```
[
    'charset' => 'UTF-8',
    'from' => 'noreply@mydomain.com',
    'bcc' => 'developer@mydomain.com',
]
```

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

```php
public string|bool $textLayout = 'layouts/text'
```

Text layout view name.

This is the layout used to render TEXT mail body.
Please refer to [`$htmlLayout`](/docs/api/yii/mail/basemailer/index.md#property-htmlLayout) for possible values that this property can take.

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

```php
public bool $useFileTransport = false
```

Whether to save email messages as files under [`$fileTransportPath`](/docs/api/yii/mail/basemailer/index.md#property-fileTransportPath) instead of sending them to the actual recipients.

This is usually used during development for debugging purpose.

## Method details

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

```php
public function afterSend(yii\mail\MessageInterface $message, bool $isSuccessful): mixed
```

This method is invoked right after mail was send.

You may override this method to do some postprocessing or logging based on mail send status.
If you override this method, please make sure you call the parent implementation first.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | [`MessageInterface`](/docs/api/yii/mail/messageinterface/index.md) |  |
| `$isSuccessful` | `bool` |  |

**Returns**

`mixed`

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

```php
public function beforeSend(yii\mail\MessageInterface $message): bool
```

This method is invoked right before mail send.

You may override this method to do last-minute preparation for the message.
If you override this method, please make sure you call the parent implementation first.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | [`MessageInterface`](/docs/api/yii/mail/messageinterface/index.md) |  |

**Returns**

`bool`: Whether to continue sending an email.

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

```php
public function compose(string|array|null $view = null, array $params = []): yii\mail\MessageInterface
```

Creates a new message instance and optionally composes its body content via view rendering.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | `string\|array\|null` | The view to be used for rendering the message body. This can be: - a string, which represents the view name or [path alias](/docs/guide/concept-aliases/index.md) for rendering the HTML body of the email. In this case, the text body will be generated by applying `strip_tags()` to the HTML body. - an array with 'html' and/or 'text' elements. The 'html' element refers to the view name or path alias for rendering the HTML body, while 'text' element is for rendering the text body. For example, `['html' => 'contact-html', 'text' => 'contact-text']`. - null, meaning the message instance will be returned without body content. The view to be rendered can be specified in one of the following formats: - path alias (e.g. "@app/mail/contact"); - a relative view name (e.g. "contact") located under [`$viewPath`](/docs/api/yii/mail/basemailer/index.md#property-viewPath). |
| `$params` | `array` | The parameters (name-value pairs) that will be extracted and made available in the view file. |

**Returns**

[`MessageInterface`](/docs/api/yii/mail/messageinterface/index.md): Message instance.

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

```php
public function generateMessageFileName(): string
```

**Returns**

`string`: The file name for saving the message when [`$useFileTransport`](/docs/api/yii/mail/basemailer/index.md#property-useFileTransport) is true.

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

```php
public function getView(): yii\web\View
```

**Returns**

[`yii\web\View`](/docs/api/yii/web/view/index.md): View instance.

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

```php
public function getViewPath(): string
```

**Returns**

`string`: The directory that contains the view files for composing mail messages Defaults to '@app/mail'.

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

```php
public function render(string $view, array $params = [], string|bool $layout = false): string
```

Renders the specified view with optional parameters and layout.

The view will be rendered using the [`$view`](/docs/api/yii/mail/basemailer/index.md#property-view) component.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | `string` | The view name or the [path alias](/docs/guide/concept-aliases/index.md) of the view file. |
| `$params` | `array` | The parameters (name-value pairs) that will be extracted and made available in the view file. |
| `$layout` | `string\|bool` | Layout view name or [path alias](/docs/guide/concept-aliases/index.md). If false, no layout will be applied. |

**Returns**

`string`: The rendering result.

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

```php
public function send(yii\mail\MessageInterface $message): bool
```

Sends the given email message.

This method will log a message about the email being sent.
If [`$useFileTransport`](/docs/api/yii/mail/basemailer/index.md#property-useFileTransport) is true, it will save the email as a file under [`$fileTransportPath`](/docs/api/yii/mail/basemailer/index.md#property-fileTransportPath).
Otherwise, it will call [`sendMessage()`](/docs/api/yii/mail/basemailer/index.md#method-sendMessage) to send the email to its recipient(s).
Child classes should implement [`sendMessage()`](/docs/api/yii/mail/basemailer/index.md#method-sendMessage) with the actual email sending logic.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | [`MessageInterface`](/docs/api/yii/mail/messageinterface/index.md) | Email message instance to be sent |

**Returns**

`bool`: Whether the message has been sent successfully

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

```php
public function sendMultiple(array $messages): int
```

Sends multiple messages at once.

The default implementation simply calls [`send()`](/docs/api/yii/mail/basemailer/index.md#method-send) multiple times.
Child classes may override this method to implement more efficient way of
sending multiple messages.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$messages` | `array` | List of email messages, which should be sent. |

**Returns**

`int`: Number of messages that are successfully sent.

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

```php
public function setView(array|yii\web\View $view): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | `array\|`[`yii\web\View`](/docs/api/yii/web/view/index.md) | View instance or its array configuration that will be used to render message bodies. |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): on invalid argument.

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

```php
public function setViewPath(string $path): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$path` | `string` | The directory that contains the view files for composing mail messages This can be specified as an absolute path or a [path alias](/docs/guide/concept-aliases/index.md). |

**Returns**

`mixed`

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

```php
protected function createMessage(): yii\mail\MessageInterface
```

Creates a new message instance.

The newly created instance will be initialized with the configuration specified by [`$messageConfig`](/docs/api/yii/mail/basemailer/index.md#property-messageConfig).
If the configuration does not specify a 'class', the [`$messageClass`](/docs/api/yii/mail/basemailer/index.md#property-messageClass) will be used as the class
of the new message instance.

**Returns**

[`MessageInterface`](/docs/api/yii/mail/messageinterface/index.md): Message instance.

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

```php
protected function createView(array $config): yii\web\View
```

Creates view instance from given configuration.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$config` | `array` | View configuration. |

**Returns**

[`yii\web\View`](/docs/api/yii/web/view/index.md): View instance.

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

```php
protected function saveMessage(yii\mail\MessageInterface $message): bool
```

Saves the message as a file under [`$fileTransportPath`](/docs/api/yii/mail/basemailer/index.md#property-fileTransportPath).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | [`MessageInterface`](/docs/api/yii/mail/messageinterface/index.md) |  |

**Returns**

`bool`: Whether the message is saved successfully

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

```php
abstract protected function sendMessage(yii\mail\MessageInterface $message): bool
```

Sends the specified message.

This method should be implemented by child classes with the actual email sending logic.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | [`MessageInterface`](/docs/api/yii/mail/messageinterface/index.md) | The message to be sent |

**Returns**

`bool`: Whether the message is sent successfully