John Davidson

php - Laravel not sending email to environment variable


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 and config are 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)

My Answer:
protected $to;

public function __construct()
$this->to = env('MAIL_TO_ADDRESS');

public function sendEmail()
Mail::to($this->to)->send(new TestMail());

In this code, I am setting the recipient email address in the constructor using the `env` function to retrieve the value from the environment variable `MAIL_TO_ADDRESS`. Then, I am using this recipient email address in the `sendEmail` method to send the email.

Make sure you have set the `MAIL_TO_ADDRESS` environment variable in your `.env` file. If the email is still not being sent, check your mail configuration in the `config/mail.php` file to ensure it is correctly set up.

If you are still facing issues, you can try debugging by checking the mail logs or using a service like Mailtrap to capture the emails being sent from your application.

Rate this post

3 of 5 based on 4058 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us