cakephp - 我如何通过 cakehphp app_local 中的变量设置电子邮件
问题描述
在 app_local 中,如何使用 cakephp 4 中的变量设置用户名和密码?我想从表中获取值。或者,如果我不能使用变量来设置电子邮件,那么还有其他方法吗?
'EmailTransport' => [
'default' => [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username'=>'xx@gmail.com',
'password'=>'xx',
//how can i do this code below with the variables as i cant get data from a table in this file?
'EmailTransport' => [
'default' => [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username'=>$username,
'password'=>$password,
解决方案
将默认电子邮件设置保留在配置文件中。
在您的控制器操作中执行以下操作:
use Cake\Mailer\MailerAwareTrait;
use Cake\Mailer\TransportFactory;
// ....
public function index()
{
$users = $this->Users->find();
foreach ($users as $user) {
TransportFactory::drop('gmail'); // If you wish to modify an existing configuration, you should drop it, change configuration and then re-add it.
TransportFactory::setConfig('gmail', [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => $user->mail_username,
'password' => $user->mail_password,
'className' => 'Smtp',
]);
$this->getMailer('Users')->send('user', [$user]);
}
}
或试试这个:
$this->getMailer('Users')
->drop('gmail')
->setConfig('gmail', [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => $user->mail_username,
'password' => $user->mail_password,
'className' => 'Smtp',
])
->send('user', [$user]);
阅读更多 https://book.cakephp.org/4/en/core-libraries/email.html#configuring-transports
注意:出于安全原因,请务必不要将纯文本密码保存到数据库中
推荐阅读
- git - 警告:在 Git 中 LF 将被 CRLF 取代
- python - python sqlite ValueError:无法解析日期时间字符串
- machine-learning - 了解卷积滤波器
- flutter - Android Studio 失去与设备的连接
- google-bigquery - 如何在 bigquery 中执行宏
- mariadb - MariaDB:如何在默认子句中使用函数?
- mysql - PRIMARY KEY 必须包括表的分区函数中的所有列
- python-3.x - 如何遍历不断更新的列表?
- laravel - Laravel 混合 __webpack_require__
- wordpress - 订购自定义分类层次结构