Pankaj
Pankaj

Reputation: 10095

Send email in background : Laravel 5.4

I am using an inbuilt code in Laravel to send Email Notification. Code is below. I am using smtp to send email

class RegisterNotification extends Notification
{
    use Queueable;

    public function __construct($token)
    {
        $this->token = $token;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                ->line('hi');
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

Here the problem is, it takes around 5 seconds to complete the process and control does not come back. I am assuming that if it come back and do the email sending work in background...it would save a lot of time.

Is there any inbuilt work to do the same? I meant, control should come back and should say email sent...and then it should do the work in background.

Email Sending code in Controller

class apiRegisterController extends Controller
{
    public function Register(RegisterRequest $request) {

        $RegisterNotification = new RegisterNotification($Token);
        $User->notify($RegisterNotification);
    }
}

Code for Queue

Controller Code

$job = (new SendForgotPasswordEmail($Data))->onConnection('database');
dispatch($job);

Job

class SendForgotPasswordEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $Data;

    public $tries = 5;

    public function __construct($Data)
    {
        $this->Data = $Data;
    }

    public function handle()
    {
        $Token = $this->Data["Token"];
        $User = $this->Data["User"];
        $RegisterNotification = new RegisterNotification($Token);
        $User->notify($RegisterNotification);
    }
}

Upvotes: 0

Views: 8104

Answers (3)

Vasileios Pallas
Vasileios Pallas

Reputation: 4877

You can use the build-in API.

$user = User::findOrFail($id);
Mail::queue('emails.welcome', $data, function ($message) use ($user){
      $message->from('[email protected]', 'Your Application');

      $message->to($user->email, $user->name)->subject('Your Reminder!');
});

But first you have to configure the queues.

Add in your .env file the line QUEUE_DRIVER=sync and then write on the terminal php artisan queue:listen.

If you want the queue to run forever on the server use Supervisor. Queues documentation explains how you can use it.

Upvotes: 1

Paras
Paras

Reputation: 9455

Step 1: Change class RegisterNotification extends Notification to class RegisterNotification extends Notification implements ShouldQueue

Step 2: Implement a queue driver. In your config/queue.php make sure your driver is not set to sync like so: 'default' => env('QUEUE_DRIVER', 'sync'), and make sure your .env doesnt have QUEUE_DRIVER=sync. You can look at the Laravel documentation for queues to choose an appropriate queue driver

Upvotes: 4

Vineesh
Vineesh

Reputation: 467

you can use laravel job queue https://laravel.com/docs/5.4/queues

Mail::queue(

Upvotes: 0

Related Questions