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.

Any suggestion will be greatly appreciated.

Thanks.

Your “mail views” are located in the /mail/ subfolder in your application folder and have it’s own “layout” folder. When you send a new email you specify the selected view as the first parameter in the mailer->compose function like this:


Yii::$app->mailer->compose('viewName', ['variable' => $variable])

More here: http://www.yiiframework.com/doc-2.0/guide-tutorial-mailing.html

Hi, I have seen this example but couldn’t get it. Say my view name in /mail/ subfolder is 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 you suggested.

Thanks.

First off you can’t use (what I know) views from your /views/ folder. You need to put your specific “mail views” in your /mail/ folder. Let’s say your /application folder looks like this:




/application/

/application/mail/

/application/mail/layouts/

/application/mail/layouts/html.php

/application/mail/reports.php



If you want to use your “reports view” you’ll specify it like this Yii::$app->mailer->compose(‘reports’). Your reports.php file will now be used and it’ll extend your “/mail/layouts/html.php” layout file (just like a regular web view will extend its layout file).

If you want to use your “city view” you’ll need to put that file (or folder+file) in your /mail/ subfolder.