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

Kartsa's avatar

Problem with sending mail, credentials are not read from .env-file

I'm trying to send mail in my Laravel app. I have done everything according to the tutorials available, and I'm pretty sure everything should be right.

I am using Google's SMTP server, and have the correct settings in .env-file. But in my local Windows environment, the app will fail always with error "554 5.5.1 Error: no valid recipients".

When I pull my app to a Linux environment, I instead get another error "530-5.5.1 Authentication Required".

However, I found out that when I view the cache file "bootstrap/cache/config.php", the username and password fields are set to NULL. For some reason it won't read the from .env.

When I edit the cache-file and add the credentials, everything works fine. But it's not a good solution. If I run "php artisan config:cache" they are gone again.

So my question is, why they are not cached correctly? And also, it would be nice to get it working in my local Windows environment as well.

0 likes
15 replies
Yorki's avatar

What version of laravel do you use and what names for variables are you using in your .env?

Kartsa's avatar

Laravel version: 5.6.33

.env variables (also have the same credentials in "config/mail.php" just in case):

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=xxxxxxxx'
MAIL_ENCRYPTION=tls

And how it will look in "bootstrap/cache/config.php":

array (
    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => '587',
    'from' =>
    array (
      'address' => '[email protected]',
      'name' => 'xxxxxxxx',
    ),
    'encryption' => 'tls',
    'username' => NULL,
    'password' => NULL,
    'sendmail' => '/usr/sbin/sendmail -bs',
    'markdown' =>
    array (
      'theme' => 'default',
      'paths' =>
      array (
        0 => '/var/www/html/resources/views/vendor/mail',
      ),
    ),
  ),

"php artisan config:clear" will clear the cache, and will get the "530-5.5.1 Authentication Required" again.

bloodykheeng's avatar

@Kartsa the solution is change the terminal if u were using vscode then terminal close it and open terminal through windows serach then then php artisan serve

Kartsa's avatar

Yes, I have the exactly same problem. But from your link I found a temporary solution. Adding this to my code before every time I send mail will work:

Config::set('mail.username', '[email protected]');
Config::set('mail.password', 'xxxxxxxx');
Cruorzy's avatar

Maybe some useless comment here, but don't you have to go to your gmail account and authorize something like 3rd party tools to send emails

Kartsa's avatar

@Cruorzy I have done that and it's not the issue. The issue is clearly that the username and password variables are not read correctly or are overwritten somewhere with NULL values, as setting them manually before sending mail works.

stefan94_sd's avatar

Try setting config/mail variables like this:

'username' => env('MAIL_USERNAME'),

'password' => env('MAIL_PASSWORD'),

Afterwards call: php artisan config:clear, php artisan cache:clear

Snapey's avatar

That thread on Github is pretty much dismissed as not a framework issue.

If it were, there would be significant noise about it.

Check your config in tinker. Just open tinker and enter config('mail') and review the response.

Cruorzy's avatar

The issue with the .env file is that it won't clear and set the right values for everyone easly.

IF env('mail') or config('mail') does return null try one of the commands below and after each command check the value of the env again with code.

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

It is kind of a puzzle I experienced

1 like
Kartsa's avatar

Oops, I feel embarrassed. I found out that I had replaced the lines of username and password in mail.php incorrectly, like this:

'username' => env('[email protected]'),
'password' => env('xxxxxxxx'),

Surely there are no such env-variables, obviously it was supposed to be:

'username' => env('MAIL_USERNAME', '[email protected]'),
'password' => env('MAIL_PASSWORD', 'xxxxxxxx'),

Or even without the default values, as I had configured them in .env. How was I so blind...

Thanks for the help everyone anyways...

Cruorzy's avatar

@Kartsa Idk how you structured it now but putting personal info email/password in a config file and then commit is a general bad idea.

Kartsa's avatar

@Cruorzy In this project it didn't matter much and the gMail-account was just made for this. But yes, you're right, generally it's a bad idea. I removed the default values for the credentials so they are only in .env right now.

Cruorzy's avatar

Great make sure you dind't commi the info with git, else it will be saved forever.

Please or to participate in this conversation.