BaseMailer
API reference › yii › mail › BaseMailer
BaseMailer serves as a base class that implements the basic functions required by MailerInterface.
Concrete child classes should may focus on implementing the sendMessage() method.
See also: BaseMessage (For more details and usage information on BaseMailer, see the guide article on mailing.).
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$fileTransportCallback | callable| | A PHP callback that will be called by send() when $useFileTransport is true. Default: null. | BaseMailer |
$fileTransportPath | string | The directory where the email messages are saved when $useFileTransport is true. Default: '@runtime/mail'. | BaseMailer |
$htmlLayout | string| | HTML layout view name. Default: 'layouts/html'. | BaseMailer |
$messageClass | string | The default class name of the new message instances created by createMessage() Default: 'yii\. | BaseMailer |
$messageConfig | array | The configuration that should be applied to any newly created email message instance by createMessage() or compose(). Default: []. | BaseMailer |
$textLayout | string| | Text layout view name. Default: 'layouts/text'. | BaseMailer |
$useFileTransport | bool | Whether to save email messages as files under $fileTransportPath instead of sending them to the actual recipients. Default: false. | BaseMailer |
$view | array|yii\ | View instance or its array configuration that will be used to render message bodies. | BaseMailer |
$viewPath | string | The directory that contains the view files for composing mail messages Defaults to ‘@app/mail’. | BaseMailer |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterSend() | This method is invoked right after mail was send. | BaseMailer |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeSend() | This method is invoked right before mail send. | BaseMailer |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
compose() | Creates a new message instance and optionally composes its body content via view rendering. | BaseMailer |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
generateMessageFileName() | BaseMailer | |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getView() | BaseMailer | |
getViewPath() | BaseMailer | |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
render() | Renders the specified view with optional parameters and layout. | BaseMailer |
send() | Sends the given email message. | BaseMailer |
sendMultiple() | Sends multiple messages at once. | BaseMailer |
setView() | BaseMailer | |
setViewPath() | BaseMailer | |
trigger() | Triggers an event. | yii\ |
createMessage() protected | Creates a new message instance. | BaseMailer |
createView() protected | Creates view instance from given configuration. | BaseMailer |
saveMessage() protected | Saves the message as a file under $fileTransportPath. | BaseMailer |
sendMessage() protected abstract | Sends the specified message. | BaseMailer |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEFORE_SEND | MailEvent | An event raised right before send. Event name: 'beforeSend'. | BaseMailer |
EVENT_AFTER_SEND | MailEvent | An event raised right after send. Event name: 'afterSend'. | BaseMailer |
Property details
Section titled “Property details”$fileTransportCallback
Section titled “$fileTransportCallback ”public callable|null $fileTransportCallback = nullA 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)$htmlLayout
Section titled “$htmlLayout ”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, 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.
$messageConfig
Section titled “$messageConfig ”public array $messageConfig = []The configuration that should be applied to any newly created email message instance by createMessage() or compose().
Any valid property defined
by 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',]$textLayout
Section titled “$textLayout ”public string|bool $textLayout = 'layouts/text'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.
$useFileTransport
Section titled “$useFileTransport ”public bool $useFileTransport = falseWhether 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.
Method details
Section titled “Method details”afterSend()
Section titled “afterSend() ”public function afterSend(yii\mail\MessageInterface $message, bool $isSuccessful): mixedThis 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.
beforeSend()
Section titled “beforeSend() ”public function beforeSend(yii\mail\MessageInterface $message): boolThis 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 |
Returns
bool: Whether to continue sending an email.
compose()
Section titled “compose() ”public function compose(string|array|null $view = null, array $params = []): yii\mail\MessageInterfaceCreates a new message instance and optionally composes its body content via view rendering.
Parameters
| Parameter | Type | Description |
|---|---|---|
$view | string| | 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',. - 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. |
Returns
MessageInterface: Message instance.
generateMessageFileName()
Section titled “generateMessageFileName() ”public function generateMessageFileName(): stringReturns
string: The file name for saving the message when $useFileTransport is true.
getView()
Section titled “getView() ”public function getView(): yii\web\ViewReturns
yii\web\View: View instance.
getViewPath()
Section titled “getViewPath() ”public function getViewPath(): stringReturns
string: The directory that contains the view files for composing mail messages Defaults to ‘@app/mail’.
render()
Section titled “render() ”public function render(string $view, array $params = [], string|bool $layout = false): stringRenders the specified view with optional parameters and layout.
The view will be rendered using the $view component.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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| | Layout view name or path alias. If false, no layout will be applied. |
Returns
string: The rendering result.
send()
Section titled “send() ”public function send(yii\mail\MessageInterface $message): boolSends 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | MessageInterface | Email message instance to be sent |
Returns
bool: Whether the message has been sent successfully
sendMultiple()
Section titled “sendMultiple() ”public function sendMultiple(array $messages): intSends 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array | List of email messages, which should be sent. |
Returns
int: Number of messages that are successfully sent.
setView()
Section titled “setView() ”public function setView(array|yii\web\View $view): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$view | array|yii\ | View instance or its array configuration that will be used to render message bodies. |
Returns
mixed
Throws
yii\base\InvalidConfigException: on invalid argument.
setViewPath()
Section titled “setViewPath() ”public function setViewPath(string $path): mixedParameters
| 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. |
Returns
mixed
createMessage()
Section titled “createMessage() ”protected function createMessage(): yii\mail\MessageInterfaceCreates 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.
Returns
MessageInterface: Message instance.
createView()
Section titled “createView() ”protected function createView(array $config): yii\web\ViewCreates view instance from given configuration.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | View configuration. |
Returns
yii\web\View: View instance.
saveMessage()
Section titled “saveMessage() ”protected function saveMessage(yii\mail\MessageInterface $message): boolSaves the message as a file under $fileTransportPath.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | MessageInterface |
Returns
bool: Whether the message is saved successfully
sendMessage()
Section titled “sendMessage() ”abstract protected function sendMessage(yii\mail\MessageInterface $message): boolSends the specified message.
This method should be implemented by child classes with the actual email sending logic.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | MessageInterface | The message to be sent |
Returns
bool: Whether the message is sent successfully