user3242861
user3242861

Reputation: 1929

Laravel - Email queued doesn't work

I have an application that send several email, sometimes it returns me a timeout error. So I think the solution for that is sending the emails in background with queued but I'm having problems to implement that.

For example in my controller I have that and works:

$data = array(
            'name' => $tecnic->name,
            'email' => $tecnic->email,
            'code' => $code
          );
Mail::send('email-to-tecnico', $data, function($message) use ($data)
        {
          $message->from('[email protected]' , 'Title');
          $message->to($data['email'], $data['name'])->subject('subject');

        });

Next I create a Job SendStartPatEmail and add this:

use Mail;
class SendStartPatEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Mailer $mailer)
    {
      Mail::send('email-to-tecnico', $data, function($message) use ($data)
      {
        $message->from('[email protected]' , 'Title');

        $message->to($data['email'], $data['name'])->subject('Subject');

      });
    }
}

And in controller I remove Mail::send and add this:

SendStartPatEmail::dispatch($data);

When I run this the job was created in job table but don't send the email and the attempts column is 0 .

This don't return any error.

What I'm doing wrong?

Upvotes: 1

Views: 1269

Answers (1)

Ole Haugset
Ole Haugset

Reputation: 3797

To run the queued jobs in the jobs table, you need to run php artisan queue:work.

I recommend reading the documentation regarding supervisor to make sure the worker always runs on projects where you need it:

Laravel Supervisor Configuration

Upvotes: 3

Related Questions