Reputation: 109
In app_local how can i set username and password with variables in cakephp 4 ? I want to get the values from a table. Or if i cant use a variable to set the email then is there another way?
'EmailTransport' => [
'default' => [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username'=>'[email protected]',
'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,
https://book.cakephp.org/4/en/core-libraries/email.html
Upvotes: 1
Views: 294
Reputation: 5767
Keep the default email settings in your configuration file.
In your controller actions do something like this:
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]);
}
}
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
Upvotes: 1