Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lev89's avatar
Level 1

How to set smtp settings from database?

Now the default smtp settings are taken from the .env file. To send a message I use the Mailable class in the controller: Mail::to(Auth::user()->email)->send(new MailToClient(......)); But i want to use smtp settings from database: Table "configs" mail_host varchar(255); mail_port varchar(255); mail_username varchar(255); mail_password varchar(255); mail_encryption varchar(255); 1mail_address varchar(255);

0 likes
12 replies
lev89's avatar
Level 1

@idew dd(config(['mail.mailers.smtp.host' => $app_config->mail_host]));

return null. I redefine the configuration in the controller

Sinnbeck's avatar

@lev89 try

config(['mail.mailers.smtp.host' => $app_config->mail_host]);
dd(config('mail.mailers.smtp.host'));
lev89's avatar
Level 1

@Sinnbeck config/mail.php default: 'mailers' => [ 'smtp' => [ 'transport' => 'smtp', 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 'port' => env('MAIL_PORT', 587), 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, 'auth_mode' => null, ],

Sinnbeck's avatar

@lev89 and you get a value if you dd the database value?

dd($app_config->mail_host);
lev89's avatar
Level 1

@Sinnbeck I don’t know what happened, I didn’t do anything, but it worked

1 like
Tray2's avatar

I would advice against doing that, at least without encrypting the password for the smtp first. It's usually easier to get into the database than the filesystem. Unless of course, you've haven't configured your document root properly and you can access your .env from the url.

2 likes

Please or to participate in this conversation.