yii\mail\BaseMailer::compose PHP Method

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.
    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;
    }

Usage Example

Beispiel #1
0
 protected function prepare(BaseMailer $mailer)
 {
     $data = unserialize($this->data);
     $options = $data['options'];
     $methods = $data['methods'];
     if (isset($options['compose'])) {
         $mail = $mailer->compose($options['compose']['view'], $options['compose']['params']);
         unset($options['compose']);
     } else {
         $mail = $mailer->compose();
     }
     foreach ($methods as $method => $values) {
         if (method_exists($mail, $method)) {
             $mail->{$method}($values['value'], $values['options']);
         }
     }
     foreach ($options as $option => $val) {
         $method = 'set' . ucfirst($option);
         if (method_exists($mail, $method)) {
             $mail->{$method}($val);
         }
     }
     return $mail;
 }
All Usage Examples Of yii\mail\BaseMailer::compose