Abstract Class yii\mail\BaseMailer

继承yii\mail\BaseMailer » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\base\ViewContextInterface, yii\mail\MailerInterface
可用自版本2.0
源代码 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.

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

公共属性

隐藏继承的属性

属性类型描述定义在
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. 该属性是只读的. 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

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\base\BaseObject
__get() 返回组件属性的值. yii\base\Component
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\Component
__set() 设置组件属性的值. yii\base\Component
__unset() 将组件属性设置为null. yii\base\Component
afterSend() This method is invoked right after mail was send. yii\mail\BaseMailer
attachBehavior() 将行为附加到此组件. yii\base\Component
attachBehaviors() 将行为列表附加到组件. yii\base\Component
beforeSend() This method is invoked right before mail send. yii\mail\BaseMailer
behaviors() 返回此组件应该具有的行为列表. yii\base\Component
canGetProperty() 返回一个值, 指示属性是否可读取. yii\base\Component
canSetProperty() 返回一个值, 指示属性是否可设置. yii\base\Component
className() 返回此类的完全限定名称. yii\base\BaseObject
compose() Creates a new message instance and optionally composes its body content via view rendering. yii\mail\BaseMailer
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
generateMessageFileName() yii\mail\BaseMailer
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. yii\base\Component
getView() yii\mail\BaseMailer
getViewPath() yii\mail\BaseMailer
hasEventHandlers() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() 初始化对象. yii\base\BaseObject
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. 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() 触发事件. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述定义在
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

事件

隐藏继承的事件

事件类型描述定义在
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

属性详情

$fileTransportCallback public 属性

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)
$fileTransportPath public 属性

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

public string $fileTransportPath '@runtime/mail'
$htmlLayout public 属性

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

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

public string $messageClass 'yii\mail\BaseMessage'
$messageConfig public 属性

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

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

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.

参见 $fileTransportPath.

public boolean $useFileTransport false
$view public 属性

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

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

public string $viewPath null

方法详情

afterSend() public 方法

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

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

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

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

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

public string generateMessageFileName ( )
return string

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

getView() public 方法

public yii\web\View getView ( )
return yii\web\View

View instance.

getViewPath() public 方法

public string getViewPath ( )
return string

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

render() public 方法

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

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

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

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

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

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

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_AFTER_SEND 事件类型 yii\mail\MailEvent

An event raised right after send.

EVENT_BEFORE_SEND 事件类型 yii\mail\MailEvent

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