问题
I have the following code:
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();
How can I access the name, email, subject and message in layout/html.php?
回答1:
It's simple you can access name, email, subject and message with $
sign.
$name, $email, $subject, $message
回答2:
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
来源:https://stackoverflow.com/questions/29644709/yii2-how-to-access-parameters-in-template-mail