0

How do we override mail view files of a 3rd party module/component?

Let's assume a module is using the following code to send an email:

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

How would we override these individual email views @myvendor/mymodule/mail/email-html and @myvendor/mymodule/mail/email-text?

mae
  • 14,947
  • 8
  • 32
  • 47

2 Answers2

1

You can override these two aliases in your config:

'aliases' => [
    '@myvendor/mymodule/mail/email-html' => '@app/views/mail/email-html',
    '@myvendor/mymodule/mail/email-text' => '@app/views/mail/email-text',
],
rob006
  • 21,383
  • 5
  • 53
  • 74
0

Configure and rewrite the $viewPath property in the mail file in the module.

example:

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

First, create new html and text files. Create both files. Create both files.

  • 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...

  If the VIEW file: Only need to change the name for HTML and TEXT file, (both)

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;
    }
}

// for use. Changes

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

To change the address of the files in the component. Depending on your email module, it varies

example:

'modules' => [
        'myMudul' => [
            'class' => 'PathModule\Module',
            'mailer' => [
                #code ..
        ],
   ],
    ...
user206
  • 1,085
  • 1
  • 10
  • 15