0

I am having troubles with the Mailer after upgrading from CakePHP 3 to 4. This is the relevant part my configuration:

<?php

return [
    'EmailTransport' => [
        'default' => [
            'className' => 'Mail',
            'host' => 'localhost',
            'port' => 25,
            'timeout' => 30,
            'username' => 'user',
            'password' => 'password',
            'client' => null,
            'tls' => null,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],
        'cronjob' => [
            'className' => 'Mail',
        ],
        'accounts' => [
            'className' => 'Mail',
        ],
    ],

    'Email' => [
        'default' => [
            'transport' => 'default',
            'from' => 'you@localhost',
        ],
        'cronjob' => [
            'transport' => 'cronjob',
            'from' => 'cronjob@foobar.com',
        ],
        'accounts' => [
            'transport' => 'accounts',
            'from' => 'accounts@foobar.com',
        ],
    ],
];

This is the snippet that's causing the error:

    private function sendActivationEmail(User $user)
    {
        $url = Router::url([
            'prefix' => 'Admin',
            'plugin' => 'UserManager',
            'controller' => 'Users',
            'action' => 'activate',
            $user->username,
            $user->activation_key,
        ], true);

        debug(Configure::read('EmailTransport'));
        debug(Configure::read('Email'));

        $mailer = new Mailer('accounts');
        $mailer->setFrom(['accounts@foobar.com' => 'Foobar Website Manager'])
            ->setTo($user->email, $user->fullName)
            ->setSubject('Please activate your account')
            ->setEmailFormat('html')
            ->setViewVars(compact('url', 'user'))
            ->viewBuilder()
                ->setTemplate('UserManager.register');

        return $mailer->deliver();
    }

The error is Unknown email configuration "accounts"., thrown in error

The output of the two debug functions is the following:

/vendor/plugins/usermanager/src/Model/Table/UsersTable.php (line 72)
[
    'default' => [
        'className' => 'Mail'
    ],
    'cronjob' => [
        'className' => 'Mail'
    ],
    'accounts' => [
        'className' => 'Mail'
    ]
]

/vendor/plugins/usermanager/src/Model/Table/UsersTable.php (line 73)
[
    'default' => [
        'transport' => 'default',
        'from' => 'something@foobar.com'
    ],
    'cronjob' => [
        'transport' => 'cronjob',
        'from' => 'cronjob@foobar.com'
    ],
    'accounts' => [
        'transport' => 'accounts',
        'from' => 'accounts@foobar.com'
    ]
]

So it seems like the accounts key is present in the mail configuration, then why am I getting this error?

mrodo
  • 565
  • 5
  • 21

1 Answers1

1

Make sure that you have upgraded your bootstrap.php accordingly along the way, specifically with regards to how EmailTransport and Email are being consumed, this was introduced with CakePHP 3.7 and 4.1 if I'm not mistaken:

TransportFactory::setConfig(Configure::consume('EmailTransport'));
Mailer::setConfig(Configure::consume('Email'));

https://github.com/cakephp/app/blob/4.2.2/config/bootstrap.php#L163-L164

ndm
  • 59,784
  • 9
  • 71
  • 110