Yii2:如何访问模板邮件中的参数?

时间:2015-04-15 08:00:20

标签: php yii2

我有以下代码:

Yii::$app->mailer->compose('layouts/html', [
                'name' => $this->name,
                'email' => $this->email,
                'subject' => $this->subject,
                'message' => $this->body])
                ->setTo($this->email)
                ->setFrom([$this->email])
                ->setSubject($this->subject)
                ->send();

如何在layout / html.php中访问姓名,电子邮件,主题和消息?

2 个答案:

答案 0 :(得分:1)

很简单,您可以使用$符号访问姓名,电子邮件,主题和邮件。

$name, $email, $subject, $message

答案 1 :(得分:1)

In your example, you need to replace name of template.
Becouse layouts/html does not need to be explicitly specified.

Yii::$app->mailer->compose('mytemplate',
    ['name' => $this->name,
        'email' => $this->email,
        'subject' => $this->subject,
        'message' => $this->body])
    ->setTo($this->email)
    ->setFrom([$this->email])
    ->setSubject($this->subject)
    ->send();

Sample content of mail/mytemplate.php :

<?php
echo "$subject<br> $name<br> $email<br> $message";

And take default content for mail/layouts/html.php here