Abstract Class yii\mail\BaseMailer
BaseMailer 充当基类,实现 yii\mail\MailerInterface 所需的基本功能。
具体的子类应该专注于实现 sendMessage() 方法。
参见 yii\mail\BaseMessage 了解更多关于 BaseMailer 的细节和用法,请参阅 guide article on mailing。.
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
| $fileTransportCallback | callable | 当 $useFileTransport 为 true 的时候,由 send() 调用的 PHP 回调。
回调应该返回一个文件名,用于保存电子邮件。
如果没有设置,文件名将通过当前时间戳来生成。
回调的方法为:
`php
function ($mailer, $message)
` |
yii\mail\BaseMailer |
| $fileTransportPath | string | 当 $useFileTransport 为 true 的时候保存的邮件目录。 | yii\mail\BaseMailer |
| $htmlLayout | string|boolean | HTML 布局视图名。这是用于渲染 HTML 邮件正文的布局。 该属性可以使用以下值: - 相对视图名:$viewPath 的相对视图文件,例如,'layouts/html'。 - 路径别名:指定为路径别名的绝对视图文件路径,例如,'@app/mail/html'。 - 布尔值 false:禁用布局。 | yii\mail\BaseMailer |
| $messageClass | string | createMessage() 创建的新消息实例的默认类名 | yii\mail\BaseMailer |
| $messageConfig | array | 应由 createMessage() 或 compose() 应用于任何新创建的邮件实例的配置数组。
可以配置 yii\mail\MessageInterface 定义任何有效的属性,
例如 from,to,subject,textBody,htmlBody, 等等。
例如:
`php
[
'charset' => 'UTF-8',
'from' => 'noreply@mydomain. |
yii\mail\BaseMailer |
| $textLayout | string|boolean | Text 布局视图名。这是用于渲染 TEXT 邮件正文的布局。 有关于此属性可以使用的值,请参阅 $htmlLayout。 | yii\mail\BaseMailer |
| $useFileTransport | boolean | 是否将电子邮件保存为 $fileTransportPath 下的文件,而不是将它们发送给实际的接收人。 这通常在开发期间用于调试的时候使用。 | yii\mail\BaseMailer |
| $view | array|yii\web\View | View 实例或数组配置, 用于渲染邮件正文。 | yii\mail\BaseMailer |
| $viewPath | string | 包含关于实现邮件消息的视图文件的目录默认为 '@app/mail'。 | yii\mail\BaseMailer |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __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() | 发送邮件后立即调用此方法。 你可以覆盖此方法以根据邮件发送状态执行一些后处理或记录。 如果重写此方法,请确保先调用父类方法。 | 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() | 在邮件发送之前调用此方法。 你可以覆盖此方法以对邮件发送做最后的准备。 如果重写此方法,请确保先调用父类方法。 | 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() | 创建一个新的消息实例,并可选的通过视图渲染来组成主题内容。 | 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() | 使用可选参数和布局渲染指定视图。 视图将使用 $view 组件渲染。 | yii\mail\BaseMailer |
| send() | 发送给定的电子邮件。 此方法将记录关于正在发送的电子邮件的消息。 如果 $useFileTransport 为 true,它会将电子邮件保存为 $fileTransportPath 下的文件。 否则,将调用 sendMessage() 将电子邮件发送给收件人。 子类应该用实际的电子邮件发送逻辑实现 sendMessage()。 | yii\mail\BaseMailer |
| sendMultiple() | 一次发送多条消息。 | yii\mail\BaseMailer |
| setView() | yii\mail\BaseMailer | |
| setViewPath() | yii\mail\BaseMailer | |
| trigger() | Triggers an event. | yii\base\Component |
受保护的方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| createMessage() | 创建一个新的消息实例。 将使用 $messageConfig 指定的配置初始化新创建的实例。 如果配置没有指定 'class',$messageClass 将用作新消息 实例的类。 | yii\mail\BaseMailer |
| createView() | 从给定的配置创建视图实例。 | yii\mail\BaseMailer |
| saveMessage() | 将消息存为 $fileTransportPath 下的文件。 | yii\mail\BaseMailer |
| sendMessage() | 发送指定消息。 此方法应由具有实际电子邮件发送逻辑的子类实现。 | yii\mail\BaseMailer |
Events
| 事件 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| EVENT_AFTER_SEND | yii\mail\MailEvent | 发送后触发的事件。 | yii\mail\BaseMailer |
| EVENT_BEFORE_SEND | yii\mail\MailEvent | 发送前触发的事件。 你可以将 yii\mail\MailEvent::$isValid 设置为 false 以取消发送。 | yii\mail\BaseMailer |
属性详情
当 $useFileTransport 为 true 的时候,由 send() 调用的 PHP 回调。 回调应该返回一个文件名,用于保存电子邮件。 如果没有设置,文件名将通过当前时间戳来生成。
回调的方法为:
function ($mailer, $message)
当 $useFileTransport 为 true 的时候保存的邮件目录。
HTML 布局视图名。这是用于渲染 HTML 邮件正文的布局。 该属性可以使用以下值:
createMessage() 创建的新消息实例的默认类名
应由 createMessage() 或 compose() 应用于任何新创建的邮件实例的配置数组。
可以配置 yii\mail\MessageInterface 定义任何有效的属性,
例如 from,to,subject,textBody,htmlBody, 等等。
例如:
[
'charset' => 'UTF-8',
'from' => 'noreply@mydomain.com',
'bcc' => 'developer@mydomain.com',
]
Text 布局视图名。这是用于渲染 TEXT 邮件正文的布局。 有关于此属性可以使用的值,请参阅 $htmlLayout。
是否将电子邮件保存为 $fileTransportPath 下的文件,而不是将它们发送给实际的接收人。 这通常在开发期间用于调试的时候使用。
View 实例或数组配置, 用于渲染邮件正文。
包含关于实现邮件消息的视图文件的目录默认为 '@app/mail'。
方法详情
发送邮件后立即调用此方法。 你可以覆盖此方法以根据邮件发送状态执行一些后处理或记录。 如果重写此方法,请确保先调用父类方法。
| public void afterSend($message, $isSuccessful) | ||
| $message | yii\mail\MessageInterface | |
| $isSuccessful | boolean | |
在邮件发送之前调用此方法。 你可以覆盖此方法以对邮件发送做最后的准备。 如果重写此方法,请确保先调用父类方法。
| public boolean beforeSend($message) | ||
| $message | yii\mail\MessageInterface | |
| return | boolean | 是否继续发送邮件。 |
|---|---|---|
创建一个新的消息实例,并可选的通过视图渲染来组成主题内容。
| public yii\mail\MessageInterface compose($view = null, array $params = []) | ||
| $view | string|array|null | 用于渲染邮件正文的视图。这可以是:
要渲染的视图可以使用以下格式之一指定:
|
| $params | array | 将在视图文件中可用的参数(键值对)。 |
| return | yii\mail\MessageInterface | 消息实例。 |
|---|---|---|
创建一个新的消息实例。 将使用 $messageConfig 指定的配置初始化新创建的实例。 如果配置没有指定 'class',$messageClass 将用作新消息 实例的类。
| protected yii\mail\MessageInterface createMessage() | ||
| return | yii\mail\MessageInterface | 消息实例。 |
|---|---|---|
从给定的配置创建视图实例。
| protected yii\web\View createView(array $config) | ||
| $config | array | 视图的配置。 |
| return | yii\web\View | 视图的实例。 |
|---|---|---|
| public string generateMessageFileName() | ||
| return | string | 当 $useFileTransport 为 true 时保存消息的文件名。 |
|---|---|---|
| public yii\web\View getView() | ||
| return | yii\web\View | 视图实例。 |
|---|---|---|
| public string getViewPath() | ||
| return | string | 包含关于实现邮件消息的视图文件的目录默认为 '@app/mail'。 |
|---|---|---|
使用可选参数和布局渲染指定视图。 视图将使用 $view 组件渲染。
| public string render($view, $params = [], $layout = false) | ||
| $view | string | 视图文件的视图名或 路径别名。 |
| $params | array | 将在视图文件中使用的参数(键值对)。 |
| $layout | string|boolean | 布局视图名称或 路径别名。如果为 false,不会应用布局。 |
| return | string | 渲染的结果。 |
|---|---|---|
将消息存为 $fileTransportPath 下的文件。
| protected boolean saveMessage($message) | ||
| $message | yii\mail\MessageInterface | |
| return | boolean | 消息是否成功发送 |
|---|---|---|
发送给定的电子邮件。 此方法将记录关于正在发送的电子邮件的消息。 如果 $useFileTransport 为 true,它会将电子邮件保存为 $fileTransportPath 下的文件。 否则,将调用 sendMessage() 将电子邮件发送给收件人。 子类应该用实际的电子邮件发送逻辑实现 sendMessage()。
| public boolean send($message) | ||
| $message | yii\mail\MessageInterface | 要发送的电子邮件实例 |
| return | boolean | 消息是否成功的发送 |
|---|---|---|
发送指定消息。 此方法应由具有实际电子邮件发送逻辑的子类实现。
| protected abstract boolean sendMessage($message) | ||
| $message | yii\mail\MessageInterface | 要发送的消息 |
| return | boolean | 消息是否成功发送 |
|---|---|---|
一次发送多条消息。
默认实现只需要多次调用 send()。 子类可以重写此方法, 实现更有效的方式发送多个消息。
| public integer sendMultiple(array $messages) | ||
| $messages | array | 应发送的电子邮件列表。 |
| return | integer | 成功发送的邮件数。 |
|---|---|---|
| public void setView($view) | ||
| $view | array|yii\web\View | View 实例或数组配置, 用于渲染邮件正文。 |
| throws | yii\base\InvalidConfigException | 使用无效的参数抛出的异常。 |
|---|---|---|
| public void setViewPath($path) | ||
| $path | string | 包含用于实现邮件消息的视图文件的目录 可以指定为绝对路劲或 路径别名。 |
事件详情
发送后触发的事件。
发送前触发的事件。 你可以将 yii\mail\MailEvent::$isValid 设置为 false 以取消发送。