Yii2: How to access parameters in template mail?

别说谁变了你拦得住时间么 提交于 2020-04-11 05:14:40

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!