Home > Software engineering >  Laravel not sending email to environment variable
Laravel not sending email to environment variable

Time:07-30

I am using the following working code in Laravel:

Mail::to('[email protected]')->send(new \App\Mail\CheckinInfo([
   'client' => $client,
]));

when I use an email address from the environment, it stopped working:

Mail::to(env('MAIL_FROM_ADDRESS')->send(new \App\Mail\CheckinInfo([
   'client' => $client,
]));

In .env I got:

[email protected]

and I tried

MAIL_FROM_ADDRESS="[email protected]"

The cache is cleared.

The error message is:

[2022-07-29 18:44:26] production.ERROR: An email must have a "To", "Cc", or "Bcc" header. {"exception":"[object] (Symfony\\Component\\Mime\\Exception\\LogicException(code: 0): An email must have a \"To\", \"Cc\", or \"Bcc\" header. at /www/htdocs/app/laravel/vendor/symfony/mime/Message.php:128)

CodePudding user response:

As a note when you do a config:cache the settings will not be grabbed from your .env anymore.

You should create a config/settings.php or similar file and store your env vars there, ie:

config/settings.php

return [
  'mail_from_address' => env('MAIL_FROM_ADDRESS'),
]

and you should reference it as so

config('settings.mail_from_address')

CodePudding user response:

No your mistake on the first line, maybe you didn't close the bracket Try this way

    Mail::to(env('MAIL_FROM_ADDRESS'))->send(new \App\Mail\CheckinInfo([
        'client' => $client,
     ])); //Notice I added the closing parenthesis for To()

You are trying to access the functions of the env methods. This is not possible I hope this is just the problem You can see the documentation for the framework, about this queueing-mail laravel documentation

  • Related