How to access parameters in Mail-view?

how get parameters from compose() in view Yii-2?

I try:

/controllers/SiteController


Yii::$app->mailer->compose('layouts/html.php', ['model' => 'trtrtrtrt',])

 ->setFrom('ergegergerger@gmail.com')

 ->setTo('ergergergegerg@mail.ru')

 ->setSubject('TEST')

 ->send();

mail/layouts/html.php


<?php

use yii\helpers\Html;

use yii\mail\BaseMailer;




/* @var $this \yii\web\View view component instance */

/* @var $message \yii\mail\MessageInterface the message being composed */

/* @var $content string main view render result */

?>

<?php $this->beginPage() ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />

    <title><?= Html::encode($this->title) ?>Тестовое письмо</title>

    <?php $this->head() ?>

</head>

<body>

    <?php $this->beginBody() ?>


    <div style="background-color: green;">Приветик !</div>


     <?= Html::encode($model) ?>


    <?php $this->endBody() ?>

</body>

</html>

<?php $this->endPage() ?>

i get error "Undefined variable: model" - <?= Html::encode($model) ?>

how get parameters from compose()?

You are calling a parameter in the layout file. But the parameter model is available only in the view.

So you have to change the code:

mail/layouts/html.php




<?php

use yii\helpers\Html;

use yii\mail\BaseMailer;




/* @var $this \yii\web\View view component instance */

/* @var $message \yii\mail\MessageInterface the message being composed */

/* @var $content string main view render result */

?>

<?php $this->beginPage() ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />

    <title><?= Html::encode($this->title) ?>Тестовое письмо</title>

    <?php $this->head() ?>

</head>

<body>

    <?php $this->beginBody() ?>


    <div style="background-color: green;">Приветик !</div>


     <?= $content ?>


    <?php $this->endBody() ?>

</body>

</html>

<?php $this->endPage() ?>



emailview.php




<?= Html::encode($model) ?>



php code:




Yii::$app->mailer->compose('emailview.php', ['model' => 'trtrtrtrt',])

 ->setFrom('ergegergerger@gmail.com')

 ->setTo('ergergergegerg@mail.ru')

 ->setSubject('TEST')

 ->send();