Keep the default email settings in your configuration file.
In your controller actions do something like this:
use CakeMailerMailerAwareTrait;
use CakeMailerTransportFactory;
// ....
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]);
}
}
or try this:
$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]);
read more https://book.cakephp.org/4/en/core-libraries/email.html#configuring-transports
note: for security reasons, be sure not to save a plain text password to the database
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…