How can I override a mail view of a package?

I am trying to override the passwordResetToken view in the rogbregonm/yii2-auth package (github.com/robregonm/yii2-auth), which is used to send an email to the user.

The view is located in vendor/robregonm/yii2-auth/views/mail/passwordResetToken.php and the view I want to use is in frontend/themes/liva/vendor/auth/mail/passwordResetToken.php

I tried it in the main frontend configuration (advanced template):


(...)

'view' => [

    'theme' => [

        'pathMap' => [

            '@frontend/views'                   => '@frontend/themes/liva',

            '@vendor/robregonm/yii2-auth/views' => '@frontend/themes/liva/vendor/auth',

        ],

        'baseUrl' => '@web/themes/liva',

    ],

],

(...)

This works fine for the views in the default folder of the package, but the mail view is ignored. It always sends the view from the package.

I also tried:


'@auth/views' => '@frontend/themes/liva/vendor/auth'

and


'@auth/views/mail/passwordResetToken' => '@frontend/themes/liva/vendor/auth/mail/passwordResetToken'

The code that sends the mail:


return \Yii::$app->mailer->compose('@auth/views/mail/passwordResetToken', ['user' => $user])

                         ->setFrom([\Yii::$app->getModule('auth')->supportEmail => \Yii::$app->name])

                         ->setTo($this->email)

                         ->setSubject(Yii::t('auth.reset-password', 'Password reset for {name}', ['name' => \Yii::$app->name]))

                         ->send();

Is there something wrong with my approach? Or another way to override the view?

Ever solved this? I’m running into the same problem.

Figured it out after scouring source code :)

For some reason which I don’t understand yet, the Mailer component creates a new View component that it uses, with no configuration applied to it (such as the View->Theme->Path map set in the config file)

You can tell the Mailer component to use the very same View component simply by doing:




Yii::$app->mailer->view = Yii::$app->view;



I ended up putting this in my app config like this for now:




    'on beforeAction' => function($event) {

        Yii::$app->mailer->view = Yii::$app->view;

    },



I’m sure theres a better way to apply that globally but for now that’s working fine.