ajith
(Ajith Tejas)
June 9, 2015, 5:52am
1
hi i want to send a view file via mail in yi2.
i tries the following way. i have a view file email which i want to use(its inside account view folder) called email in its my account controller code
Yii::$app->mailer->compose('email')
->setFrom('x@x.com')
->setTo($email)
->setSubject('Message subject')
->setTextBody('Plain text content')
->setHtmlBody(Html::a('Reset password', Url::home('http') . '?r=users/reset_password&email='.$email))
->send();
and i encountered the following error
The view file does not exist: /home/user/public_html/urshow/frontend/mail/layouts/html.php
what am i doing wrong
thanks
Hi!
You need a "email layout" file and inside this layout file you can render vour view … like in your application.
For example create following files:
app/mail/layout/html.php
app/mail/views/mailview.php
Example: html-layout.php
use yii\helpers\Html;
?>
<?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() ?>
<?= $content ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
Example mailview.php
echo $mymodel->value;
And then compose email like:
// set tle layout file for the email
Yii::$app->mailer->htmlLayout = '@app/mail/layouts/html-layout';
Yii::$app->mailer->compose([
'text' => 'app/mail/views/mailview' // set the view
],
['mymodel' => Model::find(1)] // pass model to view
)
->setFrom('sender@mydomain.de')
->setTo('receiver@mydomain.de')
->setSubject('My Title')
->send();
Regards
wal24
(Wlockwood)
February 16, 2016, 1:01am
4
MetaCrawler:
Hi!
You need a "email layout" file and inside this layout file you can render vour view … like in your application.
For example create following files:
app/mail/layout/html.php
app/mail/views/mailview.php
Example: html-layout.php
use yii\helpers\Html;
?>
<?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() ?>
<?= $content ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
Example mailview.php
echo $mymodel->value;
And then compose email like:
// set tle layout file for the email ############# Model or Controller? ################
Yii::$app->mailer->htmlLayout = '@app/mail/layouts/html-layout';
Yii::$app->mailer->compose([
'text' => 'app/mail/views/mailview' // set the view
],
['mymodel' => Model::find(1)] // pass model to view
)
->setFrom('sender@mydomain.de')
->setTo('receiver@mydomain.de')
->setSubject('My Title')
->send();
Regards
Is the compose in a model or a controller?