Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2: how to render a view in mail

I am able to send mail using swiftmailer extension. I am also able to include the view file from @app/mail folder.

Now I am wondering how I can include the data for my model views for example say I have a Model City.php and related view as view.php or index.php(grid-view) how can I include the data rendered in view.php(single record) or table data in index.php in mail body.

In short I want to include the default data rendered in yii2 in views or index in mail body.

Hi, I have seen this example like - Yii::$app->mailer->compose('viewName', ['variable' => $variable])

another example - Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])

I have tried like this without much success:

Yii::$app->mailer->compose('medicine-request-entry/html',['medicine_request_entry'=>$form])

but I am getting the error undefined variable form. medicie_request_entry is the folder in views folder.(not in @app/views)

but couldn't get it. Say my view name in /mail/ subfolder is say report.php and I have a folder in my views subfoler /city/ and in city folder there is view.php and index.php how I can specify this in the parameter in the example code.

Any suggestion will be greatly appreciated.

Thanks.

like image 378
Pawan Avatar asked Jan 10 '23 03:01

Pawan


1 Answers

Ok I found the solution and putting the answer for anyone having the same difficulty

I modified the code in my controller like:

Yii::$app->mailer->compose('@app/views/medicine-request-entry/view',['model'=>$model])

But I still didn't get, how to use layout given in @app/mail or to send the mail as html.

Thanks.

like image 191
Pawan Avatar answered Jan 17 '23 06:01

Pawan