CakePHP 邮件程序 "Unknown email configuration" 错误
CakePHP Mailer "Unknown email configuration" error
从 CakePHP 3 升级到 4 后,我在使用 Mailer 时遇到了问题。
这是我配置的相关部分:
<?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',
],
],
];
这是导致错误的片段:
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();
}
错误是Unknown email configuration "accounts".
,抛出
两个debug
函数的输出如下:
/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'
]
]
所以邮件配置中似乎存在 accounts
密钥,那为什么我会收到此错误?
确保您已经相应地升级了 bootstrap.php
,特别是关于如何使用 EmailTransport
和 Email
,这是在 CakePHP 3.7 和 4.1 中引入的如果我没记错的话:
TransportFactory::setConfig(Configure::consume('EmailTransport'));
Mailer::setConfig(Configure::consume('Email'));
https://github.com/cakephp/app/blob/4.2.2/config/bootstrap.php#L163-L164
从 CakePHP 3 升级到 4 后,我在使用 Mailer 时遇到了问题。 这是我配置的相关部分:
<?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',
],
],
];
这是导致错误的片段:
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();
}
错误是Unknown email configuration "accounts".
,抛出
两个debug
函数的输出如下:
/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'
]
]
所以邮件配置中似乎存在 accounts
密钥,那为什么我会收到此错误?
确保您已经相应地升级了 bootstrap.php
,特别是关于如何使用 EmailTransport
和 Email
,这是在 CakePHP 3.7 和 4.1 中引入的如果我没记错的话:
TransportFactory::setConfig(Configure::consume('EmailTransport'));
Mailer::setConfig(Configure::consume('Email'));
https://github.com/cakephp/app/blob/4.2.2/config/bootstrap.php#L163-L164