Yii2:覆盖第 3 方邮件视图

Yii2: Override 3rd party mail views

我们如何覆盖第 3 方的邮件视图文件module/component?

假设一个模块使用以下代码发送电子邮件:

Yii::$app->mailer->compose([
    'html'  => '@myvendor/mymodule/mail/email-html',
    'text'  => '@myvendor/mymodule/mail/email-text',
])
    ->setTo([$email => $name])
    ->setSubject('Hi');
    ->send();

我们如何覆盖这些单独的电子邮件视图 @myvendor/mymodule/mail/email-html@myvendor/mymodule/mail/email-text

配置并重写模块中邮件文件中的$viewPath属性

示例:

 public $viewPath = '@myvendor/mymodule/mail';

首先,创建新的 html 和文本文件。创建这两个文件。 创建两个文件

  • mail/newHTML
  • mail/trxt/NewTEXT

       $mailer = Yii::$app->mailer;
       $mailer->viewPath = $this->viewPath;
       $mailer->getView()->theme = Yii::$app->view->theme;
        return $mailer->compose(['html' => $view, 'text' => 'text/' . $view], $params)
            ->setTo($to)
            ->setFrom($this->sender)
            ->setSubject($subject)
            ->send();
    

If you want to change the path for only one: Use before code:

Yii :: $ app-> mailer-> viewPath = '@ myvendor / newPath';

Yii::$app->mailer->compose([ #code...

如果是VIEW文件:只需要更改HTML和TEXT文件的名称,(两者)

Update:

It can be override or through a component and ...

//new file: path\widgets\Mailer.php
namespace path\widgets;
use yourpath\Mailer as DefaultMailer;  //path:mymodule/mail
class Mailer extends DefaultMailer{
    public $viewPath = '@myvendor/mymodule/mail';
    public function changeviewPath($_path){
        $this->viewPath; = $_path;
    }
}

// 供使用。变化

use path\widgets\Maile;  // New path
// Use before the usual code
$mailer->changeviewPath('newpath\mail');

更改组件中文件的地址。根据您的电子邮件模块,它会有所不同

示例:

'modules' => [
        'myMudul' => [
            'class' => 'PathModule\Module',
            'mailer' => [
                #code ..
        ],
   ],
    ...

您可以在配置中覆盖这两个别名:

'aliases' => [
    '@myvendor/mymodule/mail/email-html' => '@app/views/mail/email-html',
    '@myvendor/mymodule/mail/email-text' => '@app/views/mail/email-text',
],