Search code examples
phplaravel-5queuelaravel-artisan

Laravel mail queue not posting to mailtrap


I've been trying to make Laravel's Queuing work for hours and to no avail I have not Idea what's going on; I think the queues are working since they are posting to the database but what I don't understand is why aren't they executing and posting to mailtrap.

I've set my .env file to database

QUEUE_DRIVER=database

My controller:

$mailQueue  =   (new SendNotification($contact))
                     ->delay(5);

dispatch($mailQueue);

My Send Notification Job:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use Mail;
use App\Mail\Notification;

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

    protected $contact;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $notification   =   (new Notification($contact));

        Mail::to('email')
                ->queue($notification);
    }
}

Finally, my Mailable:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class Notification extends Mailable
{
    use Queueable, SerializesModels;


    protected $contact;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {

        return $this->view('mail.notify')
                    ->with([
                        'notifyName'    =>   $this->request->name,
                        'notifySubject' =>   $this->request->subject
                    ]);
    }
}

Pretty basic really, but I don't understand why its not posting or sending to Mail trap; although my Jobs table is filled with Queues that won't post.

Has anyone ever had this problem? if so anyone know what the solution is - I tried php artisan queue:work and php artisan queue:listen but they don't post anything out on terminal.

UPDATE: I tried php artisan queue:work --queue=high, emails the output was

Processing: App\Mail\Notification

but it still didn't send any mail to Mailtrap.


Solution

  • Doesn't look like you have a public function via() set in your Notification. You need to specify how you want the notification to be delivered.

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

    Also Notifications normally extend Illuminate\Notifications\Notification.

    Nevermind looks like you aren't using the built in notification system. My guess is the problem is with this:

    $mailQueue  =   (new SendNotification($contact))
                         ->delay(5);
    

    If you look in the documentation it passes a Carbon object into the delay.

    $job = (new ProcessPodcast($podcast))
      ->delay(Carbon::now()->addMinutes(10));