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 App\Mail;
use App\Models\Listing;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\URL;
use Throwable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Address;
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:
MAIL_TO_ADDRESS=admin@myweb.com,
what can I do?
I found the reason,a developer added this to the mail config file
'to' => [
'address' => env('MAIL_TO_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_TO_NAME', 'Name'),
]
Because in the beginning the website was not supposed to send emails to users, only one person would be notified. this was overwritten the Mail::to($user->email) Now everything works fine