# Abstract Class yii\mail\BaseMailer

Inheritance | yii\mail\BaseMailer » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable, yii\base\ViewContextInterface, yii\mail\MailerInterface
Subclasses | yii\swiftmailer\Mailer
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/mail/BaseMailer.php

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

Concrete child classes should may focus on implementing the sendMessage() method.

See also yii\mail\BaseMessage For more details and usage information on BaseMailer, see the guide article on mailing.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$fileTransportCallback | callable | A PHP callback that will be called by send() when $useFileTransport is true. | yii\mail\BaseMailer
$fileTransportPath | string | The directory where the email messages are saved when $useFileTransport is true. | yii\mail\BaseMailer
$htmlLayout | string|boolean | HTML layout view name. | yii\mail\BaseMailer
$messageClass | string | The default class name of the new message instances created by createMessage() | yii\mail\BaseMailer
$messageConfig | array | The configuration that should be applied to any newly created email message instance by createMessage() or compose(). | yii\mail\BaseMailer
$textLayout | string|boolean | Text layout view name. | yii\mail\BaseMailer
$useFileTransport | boolean | Whether to save email messages as files under $fileTransportPath instead of sending them to the actual recipients. | yii\mail\BaseMailer
$view | yii\web\View | View instance. Note that the type of this property differs in getter and setter. See getView() and setView() for details. | yii\mail\BaseMailer
$viewPath | string | The directory that contains the view files for composing mail messages Defaults to '@app/mail'. | yii\mail\BaseMailer

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\Component
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component
__construct() | Constructor. | yii\base\BaseObject
__get() | Returns the value of a component property. | yii\base\Component
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component
__set() | Sets the value of a component property. | yii\base\Component
__unset() | Sets a component property to be null. | yii\base\Component
afterSend() | This method is invoked right after mail was send. | yii\mail\BaseMailer
attachBehavior() | Attaches a behavior to this component. | yii\base\Component
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component
beforeSend() | This method is invoked right before mail send. | yii\mail\BaseMailer
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
compose() | Creates a new message instance and optionally composes its body content via view rendering. | yii\mail\BaseMailer
detachBehavior() | Detaches a behavior from the component. | yii\base\Component
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component
generateMessageFileName() |  | yii\mail\BaseMailer
getBehavior() | Returns the named behavior object. | yii\base\Component
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component
getView() |  | yii\mail\BaseMailer
getViewPath() |  | yii\mail\BaseMailer
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component
init() | Initializes the object. | yii\base\BaseObject
off() | Detaches an existing event handler from this component. | yii\base\Component
on() | Attaches an event handler to an event. | yii\base\Component
render() | Renders the specified view with optional parameters and layout. | yii\mail\BaseMailer
send() | Sends the given email message. | yii\mail\BaseMailer
sendMultiple() | Sends multiple messages at once. | yii\mail\BaseMailer
setView() |  | yii\mail\BaseMailer
setViewPath() |  | yii\mail\BaseMailer
trigger() | Triggers an event. | yii\base\Component

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
createMessage() | Creates a new message instance. | yii\mail\BaseMailer
createView() | Creates view instance from given configuration. | yii\mail\BaseMailer
saveMessage() | Saves the message as a file under $fileTransportPath. | yii\mail\BaseMailer
sendMessage() | Sends the specified message. | yii\mail\BaseMailer

## Events

Hide inherited events

Event | Type | Description | Defined By
--- | --- | --- | ---
EVENT_AFTER_SEND | yii\mail\MailEvent | An event raised right after send. | yii\mail\BaseMailer
EVENT_BEFORE_SEND | yii\mail\MailEvent | An event raised right before send. | yii\mail\BaseMailer

## Property Details

$fileTransportCallback public property

A PHP callback that will be called by send() when $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)

public callable $fileTransportCallback = null
$fileTransportPath public property

The directory where the email messages are saved when $useFileTransport is true.

public string $fileTransportPath = '@runtime/mail'
$htmlLayout public property

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, e.g., 'layouts/html'.
- a path alias: an absolute view file path specified as a path alias, e.g., '@app/mail/html'.
- a boolean false: the layout is disabled.

public string|boolean $htmlLayout = 'layouts/html'
$messageClass public property

The default class name of the new message instances created by createMessage()

public string $messageClass = 'yii\mail\BaseMessage'
$messageConfig public property

The configuration that should be applied to any newly created email message instance by createMessage() or compose(). Any valid property defined by yii\mail\MessageInterface can be configured, such as `from`, `to`, `subject`, `textBody`, `htmlBody`, etc.

For example:

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

public array $messageConfig = []
$textLayout public property

Text layout view name. This is the layout used to render TEXT mail body. Please refer to $htmlLayout for possible values that this property can take.

public string|boolean $textLayout = 'layouts/text'
$useFileTransport public property

Whether to save email messages as files under $fileTransportPath instead of sending them to the actual recipients. This is usually used during development for debugging purpose.

See also $fileTransportPath.

public boolean $useFileTransport = false
$view public property

View instance. Note that the type of this property differs in getter and setter. See getView() and setView() for details.

public yii\web\View $view = null
$viewPath public property

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

public string $viewPath = null

## Method Details

afterSend() public method

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.

public void afterSend ( $message, $isSuccessful )
$message | yii\mail\MessageInterface |
$isSuccessful | boolean |

beforeSend() public method

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.

public boolean beforeSend ( $message )
$message | yii\mail\MessageInterface |
return | boolean | Whether to continue sending an email.

compose() public method

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

public yii\mail\MessageInterface compose ( $view = null, array $params = [] )

$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 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.

$params | array | The parameters (name-value pairs) that will be extracted and made available in the view file.
return | yii\mail\MessageInterface | Message instance.

createMessage() protected method

Creates a new message instance.

The newly created instance will be initialized with the configuration specified by $messageConfig. If the configuration does not specify a 'class', the $messageClass will be used as the class of the new message instance.

protected yii\mail\MessageInterface createMessage ( )
return | yii\mail\MessageInterface | Message instance.

createView() protected method

Creates view instance from given configuration.

protected yii\web\View createView ( array $config )
$config | array | View configuration.
return | yii\web\View | View instance.

generateMessageFileName() public method

public string generateMessageFileName ( )
return | string | The file name for saving the message when $useFileTransport is true.

getView() public method

public yii\web\View getView ( )
return | yii\web\View | View instance.

getViewPath() public method

public string getViewPath ( )
return | string | The directory that contains the view files for composing mail messages Defaults to '@app/mail'.

render() public method

Renders the specified view with optional parameters and layout.

The view will be rendered using the $view component.

public string render ( $view, $params = [], $layout = false )
$view | string | The view name or the path alias of the view file.
$params | array | The parameters (name-value pairs) that will be extracted and made available in the view file.
$layout | string|boolean | Layout view name or path alias. If false, no layout will be applied.
return | string | The rendering result.

saveMessage() protected method

Saves the message as a file under $fileTransportPath.

protected boolean saveMessage ( $message )
$message | yii\mail\MessageInterface |
return | boolean | Whether the message is saved successfully

send() public method

Sends the given email message.

This method will log a message about the email being sent. If $useFileTransport is true, it will save the email as a file under $fileTransportPath. Otherwise, it will call sendMessage() to send the email to its recipient(s). Child classes should implement sendMessage() with the actual email sending logic.

public boolean send ( $message )
$message | yii\mail\MessageInterface | Email message instance to be sent
return | boolean | Whether the message has been sent successfully

sendMessage() protected abstract method

Sends the specified message.

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

protected abstract boolean sendMessage ( $message )
$message | yii\mail\MessageInterface | The message to be sent
return | boolean | Whether the message is sent successfully

sendMultiple() public method

Sends multiple messages at once.

The default implementation simply calls send() multiple times. Child classes may override this method to implement more efficient way of sending multiple messages.

public integer sendMultiple ( array $messages )
$messages | array | List of email messages, which should be sent.
return | integer | Number of messages that are successfully sent.

setView() public method

public void setView ( $view )
$view | array|yii\web\View | View instance or its array configuration that will be used to render message bodies.
throws | yii\base\InvalidConfigException | on invalid argument.

setViewPath() public method

public void setViewPath ( $path )
$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.

## Event Details

EVENT_AFTER_SEND event of type yii\mail\MailEvent

An event raised right after send.

EVENT_BEFORE_SEND event of type yii\mail\MailEvent

An event raised right before send. You may set yii\mail\MailEvent::$isValid to be false to cancel the send.