Reputation: 123
I'm trying to send one param ($id) to view layout\html.php
using compose()
method of mailer component. But i don't know how to get it
The code:
$id = 1;
Yii::$app->mailer->compose('\layouts\html.php', ['id' => $id])
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Email sent from Yii2-Swiftmailer')
->send();
And in a line of my view layout\html.php
<div><?php echo $id ?></div>
Upvotes: 0
Views: 2149
Reputation: 23778
You don't call a view file with compose()
method like this, you should use alias
like @common
, @frontend
or any other relevant to the view you are trying to load mostly we place all views related to emails in common/mail
so we will use @common
alias in example.
You can use 2 ways
You may pass additional view parameters to compose()
method, which will be available inside the view files.Yii::$app->mailer->compose('@common/path/to/view', ['id' => $id]);
Render HTML
separately
$body =Yii::$app->view->renderFile('@common/path/to/view-file.php',['id'=>$id])
to render the HTML
from the php
file, pass it any parameters you want to like any normal view file and then attach it to the email body using setHtmlBody($body)
, use the following way
$body = Yii::$app->view->renderFile ( '@common/mail/account-activation.php' , [
'id' => $id
] );
Yii::$app->mailer->compose()
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Email sent from Yii2-Swiftmailer')
->setHtmlBody($body)
->send()
For more help see Documentation
Upvotes: 2
Reputation: 46
Compose method takes special alias in format '@app/mail/layouts/default.php' So if you are using absolute format, use @ and path (yii2 aliases)
Reference to swift mailer you can see here: http://www.yiiframework.com/doc-2.0/yii-swiftmailer-mailer.html
Upvotes: 0