Monday, April 10, 2017

How to create a job for queue:work

I have a job to send emails when the user registers in the application.

SendWelcomeEmail.php

<?php

namespace App\Jobs;

use App\Jobs\Job;
use App\User;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendWelcomeEmail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $user;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Mailer $mailer)
    {
        $user = &$this->user;

        $message = sprintf('Hello %s', $user->name);

        $mailer->raw($message, function ($m) use ($user){
            $m->from('lucas.nuck@gmail.com', 'Lucas Lopes');
            $m->to($user->email, $user->name);
        });
    }
}

I would like to create a job to execute the php artisan queue: work command every minute to send the emails that are in the queue.



via Lucas Lopes

Advertisement