Global variables in Mailer

How do I set global variables in the Mailer class, please?

$this->EmailFrom = Configure::read('Settings.App.email');
$this->EmailTo = Configure::read('Settings.App.email');

public function initialize() cannot be used in mailer

 public function contact(array $data)
 {
        return $this
            ->setProfile('default')
            ->setFrom($this->EmailFrom, $this->EmailFrom)
            ->setTo($this->EmailTo)
            ->setEmailFormat('html');
 }
 public function registration(array $data)
 {
        return $this
            ->setProfile('default')
            ->setFrom($this->EmailFrom, $this->EmailFrom)
            ->setTo($this->EmailTo)
            ->setEmailFormat('html');
 }

Thank you

Add the following function to your mailer

  public function __construct( $config = null ) {
    parent::__construct( $config );
    $this->EmailFrom = Configure::read('Settings.App.email');
    $this->EmailTo = Configure::read('Settings.App.email');
  }

but be aware: What you try to do can easily be done with mailer profiles. Don’t need to fiddle around with OOP.

https://book.cakephp.org/4/en/core-libraries/email.html#configuration

These are defined in your config/app.php or can be overwritten in your config/app_local.php like so:

    'Email' => [
        'default' => [
            'transport' => 'default',
            'from' => 'someone@local.com',
            'to' => 'anotherone@local.com'
        ],
    ],
1 Like