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

vincent15000's avatar

Best way to save the email environment variables

Hello,

Hmmm ... Not sure if it's a good idea ... tell me ;).

My client is using an email service provided by the webhosting. I'm using this one to save emails from the application.

I have suggested that he uses an external service like Brevo, but he doesn't want ... well ...

If he needs to change the email contact and/or password, port, encryption, ..., I'd like that he can change the environment variables himself.

What's the best way ? Store the email parameters in the database ? But this way I have to update some configuration files ... I thought about a command accessible directly from the UI in order to update the .env file and cache the configuration, but it's probably too risky ... another idea ?

Furthermore, for such services like an SMTP server, is there any way to check the availability of the service ? How to do that ?

Thanks for your help.

V

0 likes
3 replies
LaryAI's avatar

Certainly! Here’s a detailed answer tailored for the Laracasts forum:


If you want your client to be able to update email settings (like SMTP host, port, username, password, etc.) without touching the .env file directly, you have a few options. Each comes with pros and cons:

1. Store Email Settings in the Database

How it works:

  • Create a table (e.g., email_settings) to store all necessary email configuration values.
  • Build a simple UI for your client to update these values.
  • At runtime, override Laravel's mail configuration using these database values.

Example:

First, create a migration for your settings:

php artisan make:migration create_email_settings_table

Migration example:

Schema::create('email_settings', function (Blueprint $table) {
    $table->id();
    $table->string('mail_mailer')->default('smtp');
    $table->string('mail_host');
    $table->string('mail_port');
    $table->string('mail_username');
    $table->string('mail_password');
    $table->string('mail_encryption')->nullable();
    $table->string('mail_from_address');
    $table->string('mail_from_name');
    $table->timestamps();
});

Then, in a service provider (e.g., AppServiceProvider), override the mail config at runtime:

public function boot()
{
    if (Schema::hasTable('email_settings')) {
        $settings = \App\Models\EmailSetting::first();
        if ($settings) {
            config([
                'mail.mailers.smtp.host' => $settings->mail_host,
                'mail.mailers.smtp.port' => $settings->mail_port,
                'mail.mailers.smtp.username' => $settings->mail_username,
                'mail.mailers.smtp.password' => $settings->mail_password,
                'mail.mailers.smtp.encryption' => $settings->mail_encryption,
                'mail.from.address' => $settings->mail_from_address,
                'mail.from.name' => $settings->mail_from_name,
            ]);
        }
    }
}

Pros:

  • Safe for non-technical users.
  • No need to touch the .env file.
  • Changes take effect immediately (no need to clear config cache).

Cons:

  • Slightly more complex to set up.
  • You must ensure the config is loaded before sending emails (usually in a service provider).

2. Directly Update the .env File from the UI

How it works:

  • Build a UI that writes to the .env file and runs php artisan config:cache.

Cons:

  • Risky: Writing to .env can break your app if not handled carefully.
  • Not recommended for security and stability reasons.

3. Use a Package

There are packages like spatie/laravel-settings that help manage settings in the database. You can use these to store and retrieve email settings easily.


Recommendation

Storing email settings in the database and overriding config at runtime is the safest and most flexible approach.
This way, your client can update settings via a UI, and you avoid the risks of editing the .env file directly.


Summary:

  • Store email settings in the database.
  • Override Laravel's mail config at runtime using a service provider.
  • Provide a simple UI for your client to update these settings.

Let me know if you need code examples for the UI or more details on any step!

1 like
Glukinho's avatar

I would take spatie/laravel-settings package + UI for changing settings by customer.

To monitor SMTP availability, you can:

  1. check if mail server responds on SMTP port;
  2. check if mail server response has valid greeting (something like 220 mail.company.com Ok 1750264011-pQQFXT0Lb8c0)
  3. send test message and check if it is accepted by SMTP server and (optionally) was delivered. You can schedule artisan command with test sending and do some actions in case of failure:
// routes/console.php

Schedule::command(SendTestEmailCommand::class)
	->hourly()
	->onFailure(function () {
        // email test failed, send signal to admin...
    });

Don't schedule it too often or you will be banned by mail server.

1 like
martinbean's avatar

@vincent15000 I wouldn’t. A non-technical user shouldn’t be making technical changes, such as email credentials.

What happens if they change it, and then three weeks later they start moaning at you because their “emails stopped working”?

Please or to participate in this conversation.