PHP Class yii\mail\BaseMailer

Concrete child classes should may focus on implementing the BaseMailer::sendMessage method.
See also: BaseMessage
Since: 2.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Component, implements yii\mail\MailerInterface, implements yii\base\ViewContextInterface
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$fileTransportCallback a PHP callback that will be called by BaseMailer::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: php function ($mailer, $message)
$fileTransportPath the directory where the email messages are saved when [[useFileTransport]] is true.
$htmlLayout 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.
$messageClass the default class name of the new message instances created by BaseMailer::createMessage
$messageConfig the configuration that should be applied to any newly created email message instance by BaseMailer::createMessage or BaseMailer::compose. Any valid property defined by MessageInterface can be configured, such as from, to, subject, textBody, htmlBody, etc. For example: php [ 'charset' => 'UTF-8', 'from' => '[email protected]', 'bcc' => '[email protected]', ]
$textLayout 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 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 Methods

Method Description
afterSend ( yii\mail\MessageInterface $message, boolean $isSuccessful ) This method is invoked right after mail was send.
beforeSend ( yii\mail\MessageInterface $message ) : boolean This method is invoked right before mail send.
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.
generateMessageFileName ( ) : string
getView ( ) : View
getViewPath ( ) : string
render ( string $view, array $params = [], string | boolean $layout = false ) : string Renders the specified view with optional parameters and layout.
send ( yii\mail\MessageInterface $message ) : boolean Sends the given email message.
sendMultiple ( array $messages ) : integer Sends multiple messages at once.
setView ( array | View $view )
setViewPath ( string $path )

Protected Methods

Method Description
createMessage ( ) : yii\mail\MessageInterface Creates a new message instance.
createView ( array $config ) : View Creates view instance from given configuration.
saveMessage ( yii\mail\MessageInterface $message ) : boolean Saves the message as a file under [[fileTransportPath]].
sendMessage ( yii\mail\MessageInterface $message ) : boolean Sends the specified message.

Method Details

afterSend() public method

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 afterSend ( yii\mail\MessageInterface $message, boolean $isSuccessful )
$message yii\mail\MessageInterface
$isSuccessful boolean

beforeSend() public method

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 beforeSend ( yii\mail\MessageInterface $message ) : boolean
$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 compose ( string | array | null $view = null, array $params = [] ) : yii\mail\MessageInterface
$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

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 createMessage ( ) : yii\mail\MessageInterface
return yii\mail\MessageInterface message instance.

createView() protected method

Creates view instance from given configuration.
protected createView ( array $config ) : View
$config array view configuration.
return yii\web\View view instance.

generateMessageFileName() public method

public generateMessageFileName ( ) : string
return string the file name for saving the message when [[useFileTransport]] is true.

getView() public method

public getView ( ) : View
return yii\web\View view instance.

getViewPath() public method

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

render() public method

The view will be rendered using the [[view]] component.
public render ( string $view, array $params = [], string | boolean $layout = false ) : string
$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 saveMessage ( yii\mail\MessageInterface $message ) : boolean
$message yii\mail\MessageInterface
return boolean whether the message is saved successfully

send() public method

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 BaseMailer::sendMessage to send the email to its recipient(s). Child classes should implement BaseMailer::sendMessage with the actual email sending logic.
public send ( yii\mail\MessageInterface $message ) : boolean
$message yii\mail\MessageInterface email message instance to be sent
return boolean whether the message has been sent successfully

sendMessage() abstract protected method

This method should be implemented by child classes with the actual email sending logic.
abstract protected sendMessage ( yii\mail\MessageInterface $message ) : boolean
$message yii\mail\MessageInterface the message to be sent
return boolean whether the message is sent successfully

sendMultiple() public method

The default implementation simply calls BaseMailer::send multiple times. Child classes may override this method to implement more efficient way of sending multiple messages.
public sendMultiple ( array $messages ) : integer
$messages array list of email messages, which should be sent.
return integer number of messages that are successfully sent.

setView() public method

public setView ( array | View $view )
$view array | yii\web\View view instance or its array configuration that will be used to render message bodies.

setViewPath() public method

public setViewPath ( string $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.

Property Details

$fileTransportCallback public property

a PHP callback that will be called by BaseMailer::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: php function ($mailer, $message)
public $fileTransportCallback

$fileTransportPath public property

the directory where the email messages are saved when [[useFileTransport]] is true.
public $fileTransportPath

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

$messageClass public property

the default class name of the new message instances created by BaseMailer::createMessage
public $messageClass

$messageConfig public property

the configuration that should be applied to any newly created email message instance by BaseMailer::createMessage or BaseMailer::compose. Any valid property defined by MessageInterface can be configured, such as from, to, subject, textBody, htmlBody, etc. For example: php [ 'charset' => 'UTF-8', 'from' => '[email protected]', 'bcc' => '[email protected]', ]
public $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 $textLayout

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