Skip to content
PHP Ecosystem
Explore

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

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$fileTransportCallbackcallable|nullA PHP callback that will be called by send() when $useFileTransport is true. Default: null.BaseMailer
$fileTransportPathstringThe directory where the email messages are saved when $useFileTransport is true. Default: '@runtime/mail'.BaseMailer
$htmlLayoutstring|boolHTML layout view name. Default: 'layouts/html'.BaseMailer
$messageClassstringThe default class name of the new message instances created by createMessage() Default: 'yii\mail\BaseMessage'.BaseMailer
$messageConfigarrayThe configuration that should be applied to any newly created email message instance by createMessage() or compose(). Default: [].BaseMailer
$textLayoutstring|boolText layout view name. Default: 'layouts/text'.BaseMailer
$useFileTransportboolWhether to save email messages as files under $fileTransportPath instead of sending them to the actual recipients. Default: false.BaseMailer
$viewarray|yii\web\ViewView instance or its array configuration that will be used to render message bodies.BaseMailer
$viewPathstringThe directory that contains the view files for composing mail messages Defaults to ‘@app/mail’.BaseMailer
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__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
__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.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.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() staticReturns 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.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()BaseMailer
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getView()BaseMailer
getViewPath()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.BaseMailer
send()Sends the given email message.BaseMailer
sendMultiple()Sends multiple messages at once.BaseMailer
setView()BaseMailer
setViewPath()BaseMailer
trigger()Triggers an event.yii\base\Component
createMessage() protectedCreates a new message instance.BaseMailer
createView() protectedCreates view instance from given configuration.BaseMailer
saveMessage() protectedSaves the message as a file under $fileTransportPath.BaseMailer
sendMessage() protected abstractSends the specified message.BaseMailer
EventTypeDescriptionDefined by
EVENT_BEFORE_SENDMailEventAn event raised right before send. Event name: 'beforeSend'.BaseMailer
EVENT_AFTER_SENDMailEventAn event raised right after send. Event name: 'afterSend'.BaseMailer
public callable|null $fileTransportCallback = null

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 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.
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',
]
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.

public bool $useFileTransport = false

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.

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

ParameterTypeDescription
$messageMessageInterface
$isSuccessfulbool

Returns

mixed

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

ParameterTypeDescription
$messageMessageInterface

Returns

bool: Whether to continue sending an email.

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

ParameterTypeDescription
$viewstring|array|nullThe 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.
$paramsarrayThe parameters (name-value pairs) that will be extracted and made available in the view file.

Returns

MessageInterface: Message instance.

public function generateMessageFileName(): string

Returns

string: The file name for saving the message when $useFileTransport is true.

public function getView(): yii\web\View

Returns

yii\web\View: View instance.

public function getViewPath(): string

Returns

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

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

Parameters

ParameterTypeDescription
$viewstringThe view name or the path alias of the view file.
$paramsarrayThe parameters (name-value pairs) that will be extracted and made available in the view file.
$layoutstring|boolLayout view name or path alias. If false, no layout will be applied.

Returns

string: The rendering result.

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

ParameterTypeDescription
$messageMessageInterfaceEmail message instance to be sent

Returns

bool: Whether the message has been sent successfully

public function sendMultiple(array $messages): int

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.

Parameters

ParameterTypeDescription
$messagesarrayList of email messages, which should be sent.

Returns

int: Number of messages that are successfully sent.

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

Parameters

ParameterTypeDescription
$viewarray|yii\web\ViewView instance or its array configuration that will be used to render message bodies.

Returns

mixed

Throws

public function setViewPath(string $path): mixed

Parameters

ParameterTypeDescription
$pathstringThe directory that contains the view files for composing mail messages This can be specified as an absolute path or a path alias.

Returns

mixed

protected function createMessage(): yii\mail\MessageInterface

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.

Returns

MessageInterface: Message instance.

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

Creates view instance from given configuration.

Parameters

ParameterTypeDescription
$configarrayView configuration.

Returns

yii\web\View: View instance.

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

Saves the message as a file under $fileTransportPath.

Parameters

ParameterTypeDescription
$messageMessageInterface

Returns

bool: Whether the message is saved successfully

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

ParameterTypeDescription
$messageMessageInterfaceThe message to be sent

Returns

bool: Whether the message is sent successfully