catlander's avatar

Help needed: Migrating smtp server

Hi everyone,

I'm encountering a puzzling issue with my Laravel application's email configuration and would appreciate some insights from the community.

I'm trying to move to a new SMTP server in my production environment. The problem is that once the changes are made in the .env file, emails are still being sent via the old server.

I've already performed a comprehensive set of troubleshooting steps:

  • Verified .env configuration: I've repeatedly checked my .env file to ensure that MAIL_HOST, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD, MAIL_ENCRYPTION, and MAIL_FROM_ADDRESS are all correctly set for the new server (I'm already using this email configuration successfully on other applications).

  • Confirmed config/mail.php usage: I've confirmed that config/mail.php is correctly referencing these environment variables (e.g., env('MAIL_HOST')).

  • Server restarts: I've restarted the web server (Apache/Nginx, PHP-FPM) multiple times.

  • Cache clearing: I've cleared all Laravel caches using php artisan config:clear, php artisan cache:clear, php artisan view:clear, and php artisan route:clear.

  • No hardcoding: I've thoroughly checked my application's code to ensure neither the old email server nor the old email user is hardcoded anywhere.

  • No Apache environment variables set: I've also confirmed that there are no Apache environment variables overriding the .env values with the old email server details.

I know I must be missing something incredibly obvious, but I haven't been able to pinpoint it. Any hint or idea would be greatly appreciated.

Thanks in advance for your time and help!

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It sounds like you've already covered most of the common pitfalls, so let's dig a bit deeper. Here are some additional steps and checks that might help resolve the issue:

1. Double-Check the Deployed .env File

Sometimes, the .env file you’re editing isn’t the one being used by your application (especially if you have multiple deployment environments or symlinked directories). SSH into your production server and run:

cat /path/to/your/project/.env | grep MAIL_

Make sure the values shown are the new ones.


2. Clear the Config Cache (Again)

Laravel caches the configuration, and sometimes config:clear alone doesn’t do the trick. Try running:

php artisan config:clear
php artisan cache:clear
php artisan config:cache

Then, restart your web server and PHP-FPM again.


3. Debug the Actual Config Values

Add a temporary debug route to check what Laravel is actually using at runtime:

Route::get('/mail-debug', function () {
    return config('mail.mailers.smtp');
});

Visit /mail-debug in your browser. This will show you the SMTP config Laravel is using. If you still see the old values, the config cache is likely not clearing, or the wrong .env file is being loaded.


4. Check for Environment Variable Overrides

  • Server Environment Variables: Sometimes, environment variables set at the server (e.g., in Apache’s .htaccess, Nginx config, or systemd service files) can override .env values. Run printenv | grep MAIL_ on your server to see if any are set.
  • Deployment Tools: If you use Envoyer, Forge, or similar, check their environment variable management.

5. Queue Workers

If you’re sending emails via queued jobs, remember that queue workers may have been started before the .env change and are still using the old config. Restart all queue workers:

php artisan queue:restart

6. Double-Check for Multiple .env Files

Rare, but sometimes there are multiple .env files (e.g., .env.production, .env.backup). Ensure you’re editing the correct one.


7. Check for Custom Mail Drivers

If you have a custom mail driver or a package that overrides the mail configuration, check your config/services.php and any service providers for hardcoded values.


8. Test with php artisan tinker

Try sending a test email from Tinker to see if the new config is picked up:

php artisan tinker

Then:

\Mail::raw('Test', function($msg) { $msg->to('[email protected]'); });

Summary Checklist

  • Confirm .env changes are on the correct server and directory
  • Run php artisan config:clear and php artisan config:cache
  • Restart web server, PHP-FPM, and queue workers
  • Debug config at runtime
  • Check for server-level environment variable overrides

If after all this, the problem persists, please share the output of your /mail-debug route and any relevant server environment variable dumps. That will help narrow down the issue further!

1 like
Snapey's avatar

restart queue workers seems an obvious one. Also check that your queues are empty.

also, use artisan tinker to check config. This bypasses the need to check env, caches etc etc. You see exactly what the app sees (without adding a route).

1 like
catlander's avatar

The solution was indeed to restart the queue workers using php artisan queue:restart. I wasn't aware that was a necessary step after updating the .env file when using queues.

Really appreciate the help!

Please or to participate in this conversation.