Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Mail Queue is Not Working

I am trying to queue emails in Laravel 5.2 but I keep getting empty payloads in the database (As Below)

Empty Payload

My config\queue.php

'connections' => [

    ...

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'expire' => 60,
    ],

    ...
]

My code for Queuing:

if(Input::get('email-admin')) {
    $admin_pdf = PDF::loadView('emails.reporting.checkin-report', ['content' => $admin_email])->inline();
    Mail::queue('emails.reporting.checkin-email', [], function ($m) use ($admin_pdf, $start) {
        //Admin should have User ID of '1'
        $admin = User::find(1);
        $report_name = $start->format('F') . '-report.pdf';
        $m->attachData($admin_pdf, $report_name);
        $m->to($admin->email, $admin->first_name)->subject('flexxifit ' . $start->format('F') . ' Report');
    });
}

I have also tried Mail::later() with no success.

like image 953
Michael Smith Avatar asked Aug 03 '16 07:08

Michael Smith


People also ask

How does Laravel queue work?

Laravel queues provide a unified queueing API across a variety of different queue backends, such as Amazon SQS, Redis, or even a relational database. Laravel's queue configuration options are stored in your application's config/queue.php configuration file.


2 Answers

You should run the listener in console:

php artisan queue:listen

Read more here

like image 197
AlSan Avatar answered Oct 20 '22 04:10

AlSan


Apparently, the serializer does not like byte-strings ($admin_pdf in this case).

You can fix it by base64_encoding the byte-string data before queueing it, then decoding it again in the closure like this:

$adminPdf = base64_encode($pdfData); //Encoded here
Mail::queue('emails.reporting.admin-report', $emailData, function (Message $m) use ($adminPdf) {
    $m->attachData(base64_decode($adminPdf), $reportName); //Decoded here
    $m->to($adminEmail)->subject('Admin Report');
});
like image 37
Erik Morén Avatar answered Oct 20 '22 04:10

Erik Morén