I have a laravel project, I need to send email to the author of a ticket when a response is added. I've added mailtrap configuration in .env, ran the command php artisan make:mail NotificationEmail. I want the email to be sent in the background without redirecting to a new page.
In my controller
use App\Mail\NotificationEmail;
use Illuminate\Support\Facades\Mail;
$user=User::where('id',$ticket->user_id)->first();
$details = [
'user_id' => $request->user_id,
'message' => $request->message,
];
Mail::to($user->email)->send(new NotificationEmail($details));
In NotificationEmail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NotificationEmail extends Mailable
{
use Queueable, SerializesModels;
public $details;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('mail@example.com', 'Mailtrap');
}
}