使用Yii mailer
课程时,有没有办法在邮件布局中访问您的参数?我可以从视图中访问$model
,但不能访问布局。
<?php
$params = array(
"model" => $model
);
$message = Yii::$app->mailer->compose([
'html' => $view.'Html',
'text' => $view,
], $params)
->setFrom("me@placeholder.com")
->setTo($recipient)
->setSubject($subject);
?>
我知道,对于标准网页视图,您可以设置yii\web\View::$params
来访问布局中的变量,但这似乎不适用于邮件程序。
有什么想法吗?
答案 0 :(得分:4)
我会做一些类似于将面包屑带回视图的方法。
//In your view file, model was passed down via compose as you have it.
//this adds model element to the View object's params.
$this->params['model'] = $model;
//In your layout
echo $this->params['model']->attribute;
答案 1 :(得分:4)
我刚刚发现另一种方法可以将params设置为来自控制器的布局:
r = Regexp.union(r1,r2)
#=> /(?x-mi:
# \A # match start of string
# (?-mix:[[:alnum:]]+)
# -
# (?-mix:[[:upper:]][[:lower:]]+(?:\s[[:upper:]][[:lower:]]+)*)
# -
# (?-mix:[[:upper:]][[:lower:]]+\.?)
# _
# (?-mix:[[:upper:]][[:lower:]]+(?:\s[[:upper:]][[:lower:]]+)*)
# _
# (?-mix:[[:upper:]]{2})
# _
# (?-mix:\d{5}(?:-\d{4})?)
# \z # match end of string
# )|(?x-mi:
# \A
# \/
# (?-mix:[[:alnum:]]+)
# -
# (?-mix:[[:upper:]][[:lower:]]+(?:\s[[:upper:]][[:lower:]]+)*)
# -
# (?-mix:[[:upper:]][[:lower:]]+\.?)
# \z
# )/
"201-Grandview-Dr_Early_TX_76802" =~ r
#=> 0
"/50-Washington-St" =~ r
#=> 0
希望这有帮助!
答案 2 :(得分:-1)
我遇到了同样的问题,现在可以将传递给您的参数直接作为电子邮件模板中的变量:
// In controller
<?php
$params = [
'username' => 'diegouser',
'password' => 'supersecret',
];
Yii::$app->mailer->compose([
'html' => 'layouts/' . $view . '-html',
'text' => 'layouts/' . $view . '-text',
], $params)
->setFrom($mailFrom)
->setTo($emailTo)
->setSubject($subject)
->send();
因此,您可以在布局中回显变量:
<?php
// In email layout:
<?= $username ?>
<?= $password ?>