I would like to send an email using Laravel, but I am getting an error, I am sending it this way:
UsersController.php
....
$user_created = (new UserCreated($user))
->onQueue('emails')->afterCommit();
Mail::to($user->email)
->queue($user_created);
.....
This is the mailable: UserCreated.php
<?php
namespace AppMail;
use AppModelsListing;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesLog;
use IlluminateSupportFacadesURL;
use Throwable;
use IlluminateMailMailablesContent;
use IlluminateMailMailablesEnvelope;
use IlluminateMailMailablesAddress;
class UserCreated extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(protected User $user) {}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.users.created',
with: [....]
);
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
from: new Address(config('mail.from.address'), config('mail.from.name')),
subject: 'test'
);
}
}
I noticed the email is not being sent to the user’s email address, it is sent to the email address that is in my .env file:
what can I do?
2
Answers
If
MAIL_TO_ADDRESS
is set in env, Laravel will override the recipient address and send all emails to this address. You can keep MAIL_TO_ADDRESS for your development environment and for testing but in production remove it and after that run the following commands to make sure the changes work:The
MAIL_TO_ADDRESS
should beMAIL_FROM_ADDRESS
, as far as I understand you want to send mail from [email protected].