1 追蹤者

抽象類別 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 作為一個基礎類別,實作了 yii\mail\MailerInterface 所需的基本功能。

具體的子類別應專注於實作 sendMessage() 方法。

另請參閱 yii\mail\BaseMessage。 關於 BaseMailer 的更多詳細資訊和使用方式,請參閱郵件指南文章

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$behaviors yii\base\Behavior[] 附加到此元件的行為列表。 yii\base\Component
$fileTransportCallback callable|null $useFileTransport 為 true 時,將由 send() 呼叫的 PHP 回呼函數。 yii\mail\BaseMailer
$fileTransportPath string $useFileTransport 為 true 時,電子郵件訊息儲存的目錄。 yii\mail\BaseMailer
$htmlLayout string|boolean HTML 版面配置視圖名稱。 yii\mail\BaseMailer
$messageClass string createMessage() 建立的新訊息實例的預設類別名稱 yii\mail\BaseMailer
$messageConfig array 應套用於由 createMessage()compose() 建立的任何新電子郵件訊息實例的配置。 yii\mail\BaseMailer
$textLayout string|boolean 文字版面配置視圖名稱。 yii\mail\BaseMailer
$useFileTransport boolean 是否將電子郵件訊息儲存為 $fileTransportPath 下的檔案,而不是將它們發送到實際收件人。 yii\mail\BaseMailer
$view yii\web\View View 實例。 yii\mail\BaseMailer
$viewPath string 包含用於撰寫郵件訊息的視圖檔案的目錄。預設為 '@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() 此方法在郵件發送後立即調用。 yii\mail\BaseMailer
attachBehavior() 將行為附加到此元件。 yii\base\Component
attachBehaviors() 將行為列表附加到元件。 yii\base\Component
beforeSend() 此方法在郵件發送前立即調用。 yii\mail\BaseMailer
behaviors() 傳回此元件應表現為的行為列表。 yii\base\Component
canGetProperty() 傳回一個值,指示是否可以讀取屬性。 yii\base\Component
canSetProperty() 傳回一個值,指示是否可以設定屬性。 yii\base\Component
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
compose() 建立新的訊息實例,並可選擇透過視圖渲染來撰寫其主體內容。 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() 使用可選參數和版面配置渲染指定的視圖。 yii\mail\BaseMailer
send() 發送給定的電子郵件訊息。 yii\mail\BaseMailer
sendMultiple() 一次發送多個訊息。 yii\mail\BaseMailer
setView() yii\mail\BaseMailer
setViewPath() yii\mail\BaseMailer
trigger() 觸發一個事件。 yii\base\Component

受保護的方法

隱藏繼承的方法

方法 描述 定義於
createMessage() 建立一個新的訊息實例。 yii\mail\BaseMailer
createView() 從給定的配置建立視圖實例。 yii\mail\BaseMailer
saveMessage() 將訊息儲存為檔案,路徑在 $fileTransportPath 下。 yii\mail\BaseMailer
sendMessage() 發送指定的訊息。 yii\mail\BaseMailer

事件

隱藏繼承的事件

事件 類型 描述 定義於
EVENT_AFTER_SEND yii\mail\MailEvent 在發送後立即觸發的事件。 yii\mail\BaseMailer
EVENT_BEFORE_SEND yii\mail\MailEvent 在發送前立即觸發的事件。 yii\mail\BaseMailer

屬性詳情

隱藏繼承的屬性

$fileTransportCallback 公開屬性

$useFileTransport 為 true 時,send() 將會呼叫此 PHP 回呼函式。此回呼函式應回傳一個檔案名稱,該檔案名稱將用於儲存電子郵件訊息。如果未設定,檔案名稱將根據目前的時間戳記產生。

此回呼函式的簽名為

function ($mailer, $message)
$fileTransportPath 公開屬性

$useFileTransport 為 true 時,電子郵件訊息儲存的目錄。

public string $fileTransportPath '@runtime/mail'
$htmlLayout 公開屬性

HTML 版面配置視圖名稱。這是用於渲染 HTML 郵件內文的版面配置。此屬性可以採用以下值

  • 相對視圖名稱:相對於 $viewPath 的視圖檔案,例如 'layouts/html'。
  • 路徑別名:指定為路徑別名的絕對視圖檔案路徑,例如 '@app/mail/html'。
  • 布林值 false:停用版面配置。
public string|boolean $htmlLayout 'layouts/html'
$messageClass 公開屬性

createMessage() 建立的新訊息實例的預設類別名稱

public string $messageClass 'yii\mail\BaseMessage'
$messageConfig 公開屬性

應該套用於由 createMessage()compose() 所建立的任何新電子郵件訊息實例的配置。可以配置由 yii\mail\MessageInterface 定義的任何有效屬性,例如 fromtosubjecttextBodyhtmlBody 等。

例如

[
    'charset' => 'UTF-8',
    'from' => 'noreply@mydomain.com',
    'bcc' => 'developer@mydomain.com',
]
public array $messageConfig = []
$textLayout 公開屬性

文字版面配置視圖名稱。這是用於渲染 TEXT 郵件內文的版面配置。請參閱 $htmlLayout 以取得此屬性可以採用的可能值。

public string|boolean $textLayout 'layouts/text'
$useFileTransport 公開屬性

是否將電子郵件訊息儲存為 $fileTransportPath 下的檔案,而不是將它們發送到實際收件者。這通常在開發期間用於除錯目的。

另請參閱 $fileTransportPath

public boolean $useFileTransport false
$view 公開屬性

View 實例。請注意,此屬性的類型在 getter 和 setter 中有所不同。 有關詳細資訊,請參閱 getView()setView()

public yii\web\View $view null
$viewPath 公開屬性

包含用於撰寫郵件訊息的視圖檔案的目錄。預設為 '@app/mail'。

public string $viewPath null

方法詳情

隱藏繼承的方法

__call() 公開方法

定義於: yii\base\Component::__call()

呼叫指定的非類別方法。

此方法將檢查是否有任何附加的行為具有指定的名稱方法,並在可用的情況下執行它。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當調用未知方法時,它會被隱式呼叫。

public mixed __call ( $name, $params )
$name string

方法名稱

$params array

方法參數

return mixed

方法的傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $object) {
        if ($object->hasMethod($name)) {
            return call_user_func_array([$object, $name], $params);
        }
    }
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__clone() 公開方法

定義於: yii\base\Component::__clone()

在透過複製現有物件建立物件後,會呼叫此方法。

它會移除所有行為,因為它們附加到舊物件。

public void __clone ( )

                public function __clone()
{
    $this->_events = [];
    $this->_eventWildcards = [];
    $this->_behaviors = null;
}

            
__construct() 公開方法

定義於: yii\base\BaseObject::__construct()

建構子。

預設實作會執行兩件事

  • 使用給定的配置 $config 初始化物件。
  • 呼叫 init()

如果子類別中覆寫了此方法,建議

  • 建構函式的最後一個參數是一個配置陣列,就像這裡的 $config 一樣。
  • 在建構函式結尾呼叫父類別實作。
public void __construct ( $config = [] )
$config array

將用於初始化物件屬性的名稱-值對

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() 公開方法

定義於: yii\base\Component::__get()

傳回元件屬性的值。

此方法將依以下順序檢查並採取相應措施

  • 由 getter 定義的屬性:傳回 getter 結果
  • 行為的屬性:傳回行為屬性值

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $component->property; 時,它會被隱式呼叫。

另請參閱 __set()

public mixed __get ( $name )
$name string

屬性名稱

return mixed

屬性值或行為屬性的值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性是唯寫的。

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        // read property, e.g. getName()
        return $this->$getter();
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return $behavior->$name;
        }
    }
    if (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() 公開方法

定義於: yii\base\Component::__isset()

檢查屬性是否已設定,即已定義且非 null。

此方法將依以下順序檢查並採取相應措施

  • 由 setter 定義的屬性:傳回屬性是否已設定
  • 行為的屬性:傳回屬性是否已設定
  • 對於不存在的屬性,傳回 false

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($component->property) 時,它會被隱式呼叫。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return boolean

具名屬性是否已設定

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return $behavior->$name !== null;
        }
    }
    return false;
}

            
__set() 公開方法

定義於: yii\base\Component::__set()

設定元件屬性的值。

此方法將依以下順序檢查並採取相應措施

  • 由 setter 定義的屬性:設定屬性值
  • 格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz"
  • 格式為 "as xyz" 的行為:附加名為 "xyz" 的行為
  • 行為的屬性:設定行為屬性值

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $component->property = $value; 時,它會被隱式呼叫。

另請參閱 __get()

public void __set ( $name, $value )
$name string

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性是唯讀的。

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        // set property
        $this->$setter($value);
        return;
    } elseif (strncmp($name, 'on ', 3) === 0) {
        // on event: attach event handler
        $this->on(trim(substr($name, 3)), $value);
        return;
    } elseif (strncmp($name, 'as ', 3) === 0) {
        // as behavior: attach behavior
        $name = trim(substr($name, 3));
        $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = $value;
            return;
        }
    }
    if (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}

            
__unset() 公開方法

定義於: yii\base\Component::__unset()

將元件屬性設定為 null。

此方法將依以下順序檢查並採取相應措施

  • 由 setter 定義的屬性:將屬性值設定為 null
  • 行為的屬性:將屬性值設定為 null

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($component->property) 時,它會被隱式呼叫。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name string

屬性名稱

throws yii\base\InvalidCallException

如果屬性是唯讀的。

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = null;
            return;
        }
    }
    throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}

            
afterSend() 公開方法

此方法在郵件發送後立即調用。

您可以覆寫此方法,根據郵件發送狀態執行一些後處理或記錄。如果您覆寫此方法,請確保先呼叫父類別實作。

public void afterSend ( $message, $isSuccessful )
$message yii\mail\MessageInterface
$isSuccessful boolean

                public function afterSend($message, $isSuccessful)
{
    $event = new MailEvent(['message' => $message, 'isSuccessful' => $isSuccessful]);
    $this->trigger(self::EVENT_AFTER_SEND, $event);
}

            
attachBehavior() 公開方法

定義於: yii\base\Component::attachBehavior()

將行為附加到此元件。

此方法將根據給定的配置建立行為物件。 之後,將透過呼叫 yii\base\Behavior::attach() 方法將行為物件附加到此元件。

另請參閱 detachBehavior()

public yii\base\Behavior attachBehavior ( $name, $behavior )
$name string

行為的名稱。

$behavior string|array|yii\base\Behavior

行為配置。 這可以是以下其中一項

return yii\base\Behavior

行為物件

                public function attachBehavior($name, $behavior)
{
    $this->ensureBehaviors();
    return $this->attachBehaviorInternal($name, $behavior);
}

            
attachBehaviors() 公開方法

定義於: yii\base\Component::attachBehaviors()

將行為列表附加到元件。

每個行為都以其名稱索引,並且應該是 yii\base\Behavior 物件、指定行為類別的字串,或是用於建立行為的配置陣列。

另請參閱 attachBehavior()

public void attachBehaviors ( $behaviors )
$behaviors array

要附加到元件的行為列表

                public function attachBehaviors($behaviors)
{
    $this->ensureBehaviors();
    foreach ($behaviors as $name => $behavior) {
        $this->attachBehaviorInternal($name, $behavior);
    }
}

            
beforeSend() 公開方法

此方法在郵件發送前立即調用。

您可以覆寫此方法,為訊息做最後一分鐘的準備。如果您覆寫此方法,請確保先呼叫父類別實作。

public boolean beforeSend ( $message )
$message yii\mail\MessageInterface
return boolean

是否繼續發送電子郵件。

                public function beforeSend($message)
{
    $event = new MailEvent(['message' => $message]);
    $this->trigger(self::EVENT_BEFORE_SEND, $event);
    return $event->isValid;
}

            
behaviors() 公開方法

定義於: yii\base\Component::behaviors()

傳回此元件應表現為的行為列表。

子類別可以覆寫此方法,以指定它們想要表現為的行為。

此方法的傳回值應該是一個行為物件或配置的陣列,以行為名稱索引。行為配置可以是指定行為類別的字串,或是具有以下結構的陣列

'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

請注意,行為類別必須從 yii\base\Behavior 擴展。 行為可以使用名稱或匿名方式附加。 當使用名稱作為陣列鍵時,使用此名稱,稍後可以使用 getBehavior() 檢索行為,或使用 detachBehavior() 分離行為。 匿名行為無法檢索或分離。

在此方法中宣告的行為將自動附加到元件(按需)。

public array behaviors ( )
return array

行為配置。

                public function behaviors()
{
    return [];
}

            
canGetProperty() 公開方法

定義於: yii\base\Component::canGetProperty()

傳回一個值,指示是否可以讀取屬性。

如果滿足以下條件,則可以讀取屬性

  • 類別具有與指定名稱關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的行為具有給定名稱的可讀屬性(當 $checkBehaviors 為 true 時)。

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true, $checkBehaviors true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此元件的屬性

return boolean

屬性是否可讀

                public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canGetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}

            
canSetProperty() 公開方法

定義於: yii\base\Component::canSetProperty()

傳回一個值,指示是否可以設定屬性。

如果滿足以下條件,則可以寫入屬性

  • 類別具有與指定名稱關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的行為具有給定名稱的可寫屬性(當 $checkBehaviors 為 true 時)。

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true, $checkBehaviors true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此元件的屬性

return boolean

屬性是否可寫

                public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canSetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}

            
className() 公開靜態方法
自 2.0.14 版本起已棄用。 在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整名稱。

                public static function className()
{
    return get_called_class();
}

            
compose() 公開方法

建立新的訊息實例,並可選擇透過視圖渲染來撰寫其主體內容。

public yii\mail\MessageInterface compose ( $view null, array $params = [] )
$view string|array|null

用於渲染訊息內文的視圖。 這可以是

  • 字串,表示視圖名稱或 路徑別名,用於渲染電子郵件的 HTML 內文。 在這種情況下,文字內文將通過將 strip_tags() 應用於 HTML 內文來產生。
  • 具有 'html' 和/或 'text' 元素的陣列。 'html' 元素指的是用於渲染 HTML 內文的視圖名稱或路徑別名,而 'text' 元素用於渲染文字內文。 例如,['html' => 'contact-html', 'text' => 'contact-text']
  • null,表示將傳回沒有內文內容的訊息實例。

要渲染的視圖可以使用以下格式之一指定

  • 路徑別名(例如 "@app/mail/contact");
  • 位於 $viewPath 下的相對視圖名稱(例如 "contact")。
$params array

將在視圖檔案中提取並可用的參數(名稱-值對)。

return yii\mail\MessageInterface

訊息實例。

                public function compose($view = null, array $params = [])
{
    $message = $this->createMessage();
    if ($view === null) {
        return $message;
    }
    if (!array_key_exists('message', $params)) {
        $params['message'] = $message;
    }
    $this->_message = $message;
    if (is_array($view)) {
        if (isset($view['html'])) {
            $html = $this->render($view['html'], $params, $this->htmlLayout);
        }
        if (isset($view['text'])) {
            $text = $this->render($view['text'], $params, $this->textLayout);
        }
    } else {
        $html = $this->render($view, $params, $this->htmlLayout);
    }
    $this->_message = null;
    if (isset($html)) {
        $message->setHtmlBody($html);
    }
    if (isset($text)) {
        $message->setTextBody($text);
    } elseif (isset($html)) {
        if (preg_match('~<body[^>]*>(.*?)</body>~is', $html, $match)) {
            $html = $match[1];
        }
        // remove style and script
        $html = preg_replace('~<((style|script))[^>]*>(.*?)</\1>~is', '', $html);
        // strip all HTML tags and decoded HTML entities
        $text = html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, Yii::$app ? Yii::$app->charset : 'UTF-8');
        // improve whitespace
        $text = preg_replace("~^[ \t]+~m", '', trim($text));
        $text = preg_replace('~\R\R+~mu', "\n\n", $text);
        $message->setTextBody($text);
    }
    return $message;
}

            
createMessage() 受保護的方法

建立一個新的訊息實例。

新建立的實例將使用 $messageConfig 指定的配置進行初始化。 如果配置未指定 'class',則 $messageClass 將用作新訊息實例的類別。

protected yii\mail\MessageInterface createMessage ( )
return yii\mail\MessageInterface

訊息實例。

                protected function createMessage()
{
    $config = $this->messageConfig;
    if (!array_key_exists('class', $config)) {
        $config['class'] = $this->messageClass;
    }
    $config['mailer'] = $this;
    return Yii::createObject($config);
}

            
createView() protected method

從給定的配置建立視圖實例。

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

視圖配置。

return yii\web\View

View 實例。

                protected function createView(array $config)
{
    if (!array_key_exists('class', $config)) {
        $config['class'] = View::className();
    }
    return Yii::createObject($config);
}

            
detachBehavior() public method

定義於: yii\base\Component::detachBehavior()

從元件分離行為。

將會調用 behavior 的 yii\base\Behavior::detach() 方法。

public yii\base\Behavior|null detachBehavior ( $name )
$name string

behavior 的名稱。

return yii\base\Behavior|null

已分離的 behavior。如果 behavior 不存在則為 Null。

                public function detachBehavior($name)
{
    $this->ensureBehaviors();
    if (isset($this->_behaviors[$name])) {
        $behavior = $this->_behaviors[$name];
        unset($this->_behaviors[$name]);
        $behavior->detach();
        return $behavior;
    }
    return null;
}

            
detachBehaviors() public method

定義於: yii\base\Component::detachBehaviors()

從元件分離所有行為。

public void detachBehaviors ( )

                public function detachBehaviors()
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $name => $behavior) {
        $this->detachBehavior($name);
    }
}

            
ensureBehaviors() public method

定義於: yii\base\Component::ensureBehaviors()

確保在 behaviors() 中宣告的行為已附加到此元件。

public void ensureBehaviors ( )

                public function ensureBehaviors()
{
    if ($this->_behaviors === null) {
        $this->_behaviors = [];
        foreach ($this->behaviors() as $name => $behavior) {
            $this->attachBehaviorInternal($name, $behavior);
        }
    }
}

            
generateMessageFileName() public method

public string generateMessageFileName ( )
return string

$useFileTransport 為 true 時,用於儲存訊息的檔案名稱。

                public function generateMessageFileName()
{
    $time = microtime(true);
    $timeInt = (int) $time;
    return date('Ymd-His-', $timeInt) . sprintf('%04d', (int) (($time - $timeInt) * 10000)) . '-' . sprintf('%04d', random_int(0, 10000)) . '.eml';
}

            
getBehavior() public method

定義於: yii\base\Component::getBehavior()

傳回指定的行為物件。

public yii\base\Behavior|null getBehavior ( $name )
$name string

behavior 名稱

return yii\base\Behavior|null

behavior 物件,如果 behavior 不存在則為 null

                public function getBehavior($name)
{
    $this->ensureBehaviors();
    return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}

            
getBehaviors() public method

定義於: yii\base\Component::getBehaviors()

傳回附加到此元件的所有行為。

public yii\base\Behavior[] getBehaviors ( )
return yii\base\Behavior[]

附加到此元件的 behavior 列表

                public function getBehaviors()
{
    $this->ensureBehaviors();
    return $this->_behaviors;
}

            
getView() public method

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

View 實例。

                public function getView()
{
    if (!is_object($this->_view)) {
        $this->_view = $this->createView($this->_view);
    }
    return $this->_view;
}

            
getViewPath() public method

public string getViewPath ( )
return string

包含用於撰寫郵件訊息的視圖檔案的目錄。預設為 '@app/mail'。

                public function getViewPath()
{
    if ($this->_viewPath === null) {
        $this->setViewPath('@app/mail');
    }
    return $this->_viewPath;
}

            
hasEventHandlers() public method

定義於: yii\base\Component::hasEventHandlers()

傳回一個值,指示是否有名稱事件附加任何處理常式。

public boolean hasEventHandlers ( $name )
$name string

事件名稱

return boolean

是否有任何處理器附加到該事件。

                public function hasEventHandlers($name)
{
    $this->ensureBehaviors();
    if (!empty($this->_events[$name])) {
        return true;
    }
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
            return true;
        }
    }
    return Event::hasHandlers($this, $name);
}

            
hasMethod() public method

定義於: yii\base\Component::hasMethod()

傳回一個值,指示是否已定義方法。

如果滿足以下條件,則定義了方法:

  • 類別具有指定名稱的方法
  • 附加的 behavior 具有給定名稱的方法(當 $checkBehaviors 為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors true )
$name string

屬性名稱

$checkBehaviors boolean

是否將 behavior 的方法視為此元件的方法

return boolean

方法是否已定義

                public function hasMethod($name, $checkBehaviors = true)
{
    if (method_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->hasMethod($name)) {
                return true;
            }
        }
    }
    return false;
}

            
hasProperty() public method

定義於: yii\base\Component::hasProperty()

傳回一個值,指示是否為此元件定義了屬性。

如果滿足以下條件,則定義了屬性:

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的 behavior 具有給定名稱的屬性(當 $checkBehaviors 為 true 時)。

參見

public boolean hasProperty ( $name, $checkVars true, $checkBehaviors true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此元件的屬性

return boolean

屬性是否已定義

                public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
    return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}

            
init() public method

定義於: yii\base\BaseObject::init()

初始化物件。

在物件使用給定配置初始化後,此方法會在建構函式的結尾被調用。

public void init ( )

                public function init()
{
}

            
off() public method

定義於: yii\base\Component::off()

從此元件分離現有的事件處理常式。

此方法與 on() 相反。

注意:如果事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理器,而使用符合此萬用字元的純名稱註冊的處理器將會保留。

參見 on()

public boolean off ( $name, $handler null )
$name string

事件名稱

$handler callable|null

要移除的事件處理器。如果為 null,則會移除附加到已命名事件的所有處理器。

return boolean

如果找到並分離了處理器

                public function off($name, $handler = null)
{
    $this->ensureBehaviors();
    if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
        return false;
    }
    if ($handler === null) {
        unset($this->_events[$name], $this->_eventWildcards[$name]);
        return true;
    }
    $removed = false;
    // plain event names
    if (isset($this->_events[$name])) {
        foreach ($this->_events[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_events[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_events[$name] = array_values($this->_events[$name]);
            return true;
        }
    }
    // wildcard event names
    if (isset($this->_eventWildcards[$name])) {
        foreach ($this->_eventWildcards[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_eventWildcards[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
            // remove empty wildcards to save future redundant regex checks:
            if (empty($this->_eventWildcards[$name])) {
                unset($this->_eventWildcards[$name]);
            }
        }
    }
    return $removed;
}

            
on() public method

定義於: yii\base\Component::on()

將事件處理常式附加到事件。

事件處理器必須是有效的 PHP 回呼。以下是一些範例

function ($event) { ... }         // anonymous function
[$object, 'handleClick']          // $object->handleClick()
['Page', 'handleClick']           // Page::handleClick()
'handleClick'                     // global function handleClick()

事件處理器必須使用以下簽章定義:

function ($event)

其中 $event 是一個 yii\base\Event 物件,其中包含與事件相關聯的參數。

自 2.0.14 起,您可以將事件名稱指定為萬用字元模式

$component->on('event.group.*', function ($event) {
    Yii::trace($event->name . ' is triggered.');
});

參見 off()

public void on ( $name, $handler, $data null, $append true )
$name string

事件名稱

$handler 可呼叫 (callable)

事件處理器

$data mixed

事件觸發時要傳遞給事件處理器的資料。當事件處理器被調用時,可以透過 yii\base\Event::$data 存取此資料。

$append boolean

是否將新的事件處理器附加到現有處理器列表的末尾。如果為 false,則新的處理器將插入到現有處理器列表的開頭。

                public function on($name, $handler, $data = null, $append = true)
{
    $this->ensureBehaviors();
    if (strpos($name, '*') !== false) {
        if ($append || empty($this->_eventWildcards[$name])) {
            $this->_eventWildcards[$name][] = [$handler, $data];
        } else {
            array_unshift($this->_eventWildcards[$name], [$handler, $data]);
        }
        return;
    }
    if ($append || empty($this->_events[$name])) {
        $this->_events[$name][] = [$handler, $data];
    } else {
        array_unshift($this->_events[$name], [$handler, $data]);
    }
}

            
render() public method

使用可選參數和版面配置渲染指定的視圖。

視圖將使用 $view 元件進行渲染。

public string render ( $view, $params = [], $layout false )
$view string

視圖名稱或視圖檔案的 路徑別名

$params array

將在視圖檔案中提取並可用的參數(名稱-值對)。

$layout string|boolean

版型視圖名稱或 路徑別名。如果為 false,則不會應用任何版型。

return string

渲染結果。

                public function render($view, $params = [], $layout = false)
{
    $output = $this->getView()->render($view, $params, $this);
    if ($layout !== false) {
        return $this->getView()->render($layout, ['content' => $output, 'message' => $this->_message], $this);
    }
    return $output;
}

            
saveMessage() protected method

將訊息儲存為檔案,路徑在 $fileTransportPath 下。

protected boolean saveMessage ( $message )
$message yii\mail\MessageInterface
return boolean

訊息是否成功儲存

                protected function saveMessage($message)
{
    $path = Yii::getAlias($this->fileTransportPath);
    if (!is_dir($path)) {
        mkdir($path, 0777, true);
    }
    if ($this->fileTransportCallback !== null) {
        $file = $path . '/' . call_user_func($this->fileTransportCallback, $this, $message);
    } else {
        $file = $path . '/' . $this->generateMessageFileName();
    }
    file_put_contents($file, $message->toString());
    return true;
}

            
send() public method

發送給定的電子郵件訊息。

此方法將記錄有關電子郵件正在發送的訊息。如果 $useFileTransport 為 true,它會將電子郵件儲存為 $fileTransportPath 下的檔案。否則,它將調用 sendMessage() 以將電子郵件發送給其收件者。子類別應實作 sendMessage(),其中包含實際的電子郵件發送邏輯。

public boolean send ( $message )
$message yii\mail\MessageInterface

要發送的電子郵件訊息實例

return boolean

訊息是否已成功發送

                public function send($message)
{
    if (!$this->beforeSend($message)) {
        return false;
    }
    $address = $message->getTo();
    if (is_array($address)) {
        $address = implode(', ', array_keys($address));
    }
    Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
    if ($this->useFileTransport) {
        $isSuccessful = $this->saveMessage($message);
    } else {
        $isSuccessful = $this->sendMessage($message);
    }
    $this->afterSend($message, $isSuccessful);
    return $isSuccessful;
}

            
sendMessage() protected abstract method

發送指定的訊息。

此方法應由子類別實作,其中包含實際的電子郵件發送邏輯。

protected abstract boolean sendMessage ( $message )
$message yii\mail\MessageInterface

要發送的訊息

return boolean

訊息是否成功發送

                abstract protected function sendMessage($message);

            
sendMultiple() public method

一次發送多個訊息。

預設實作只是多次調用 send()。子類別可以覆寫此方法以實作更有效率的發送多個訊息的方式。

public integer sendMultiple ( array $messages )
$messages array

應發送的電子郵件訊息列表。

return integer

成功發送的訊息數量。

                public function sendMultiple(array $messages)
{
    $successCount = 0;
    foreach ($messages as $message) {
        if ($this->send($message)) {
            $successCount++;
        }
    }
    return $successCount;
}

            
setView() public method

public void setView ( $view )
$view array|yii\web\View

視圖實例或其陣列配置,將用於渲染訊息內文。

throws yii\base\InvalidConfigException

在無效的參數時。

                public function setView($view)
{
    if (!is_array($view) && !is_object($view)) {
        throw new InvalidConfigException('"' . get_class($this) . '::view" should be either object or configuration array, "' . gettype($view) . '" given.');
    }
    $this->_view = $view;
}

            
setViewPath() public method

public void setViewPath ( $path )
$path string

包含用於撰寫郵件訊息的視圖檔案的目錄。這可以指定為絕對路徑或 路徑別名

                public function setViewPath($path)
{
    $this->_viewPath = Yii::getAlias($path);
}

            
trigger() public method

定義於: yii\base\Component::trigger()

觸發一個事件。

此方法表示事件的發生。它會調用附加到事件的所有處理器,包括類別層級的處理器。

public void trigger ( $name, yii\base\Event $event null )
$name string

事件名稱

$event yii\base\Event|null

事件實例。如果未設定,將會建立預設的 yii\base\Event 物件。

                public function trigger($name, Event $event = null)
{
    $this->ensureBehaviors();
    $eventHandlers = [];
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (StringHelper::matchWildcard($wildcard, $name)) {
            $eventHandlers[] = $handlers;
        }
    }
    if (!empty($this->_events[$name])) {
        $eventHandlers[] = $this->_events[$name];
    }
    if (!empty($eventHandlers)) {
        $eventHandlers = call_user_func_array('array_merge', $eventHandlers);
        if ($event === null) {
            $event = new Event();
        }
        if ($event->sender === null) {
            $event->sender = $this;
        }
        $event->handled = false;
        $event->name = $name;
        foreach ($eventHandlers as $handler) {
            $event->data = $handler[1];
            call_user_func($handler[0], $event);
            // stop further handling if the event is handled
            if ($event->handled) {
                return;
            }
        }
    }
    // invoke class-level attached handlers
    Event::trigger($this, $name, $event);
}

            

事件詳細資訊

隱藏繼承的屬性

EVENT_AFTER_SEND event of type yii\mail\MailEvent

在發送後立即觸發的事件。

EVENT_BEFORE_SEND event of type yii\mail\MailEvent

在發送前立即觸發的事件。您可以將 yii\mail\MailEvent::$isValid 設定為 false 以取消發送。