0
votes

I would like to send mail to user after creating account on my website and I would like to use queues to send them. I'm using PHP Laravel framework.

My controller handles the request after clicking on "Create account":

class LoginController extends Controller
{
   ...
   public function register(Request $request) {
      ...
      $mail = (new RegisterRequest($user))->onConnection("database")->onQueue("emailsQueue");
      Mail::queue($mail);
      ...
   }
}

Then I have this RegisterRequest (mailable) class:

class RegisterRequest extends Mailable
{
    use Queueable, SerializesModels;

    protected $user;

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

    public function build()
    {

        return $this->from('[email protected]')
            ->to($this->user->email)
            ->subject("Confirm your Email Address")
            ->view('emails.register.request')
            ->with("registration_token", $this->user->registration_token);
    }
}

As you can see, I am using relational database to store jobs. And really, after calling LoginController's register method, a job is saved to database. But it can't be processed. I also start php artisan queue:work but nothing is done with jobs in database. Any help?

EDIT:

So I just found out that picking jobs from queue is done by SQL selecting the 'default' queue name. But I'm sending mails to queue 'emailsQueue'. So I'm now running Queue Worker like this: php artisan queue:work --queue=emailsQueue and everything's working fine for now. But how can I pick jobs from every queue in database? It's probably not the best attempt, right? It wouldn't make any sense to have named queues, right? But let's say I have one queue for processing register account requests, another queue for changing password requests and so on... So I think it does make sense to process every queue. So how can I do this? Can it be done just by listing the queues like this?

php artisan queue:work --queue=registerAccountEmailQueue,changePasswordEmailQueue...

What exactly does running php artisan queue:work? I thought it's the command to run all queues.

1
is there any entry in failed jobs? - Osama Alvi
No, everything what is done is saving a record to database table 'jobs'. Nothing more is done. - P.N.
whats your queue driver in env database or sync? - Osama Alvi
it's "database" - P.N.
try this approach: Mail::to($user)->send(new RegisterRequest); - Osama Alvi

1 Answers

0
votes

Use queue driver database.

In controller you should write

    $this->dispatch(new SendNotifyMail($data));

This will pass your $data to queue. here SendNotifyMail is used as Job Class. So you should also use this in Controller like use App\Jobs\SendNotifyMail;.

Then create a file in Folder Jobs, named SendNotifyMail

    <?php

namespace App\Jobs;

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

class SendNotifyMail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;
    public $timeout = 300; // default is 60sec. You may overwrite like this
    protected $data;
    public function __construct($data)
    {
        $this->data = $data;
    }

public function handle(Mailer $mailer)
{
    $data = $this->data; // retrieve your passed data to variable
    // your mail code here
  }
}

In your command you need to write

php artisan queue:listen

or

php artisan queue:work

Then execute the code.