benjah
benjah

Reputation: 660

laravel 5.1 trouble with queueing email sending

i'm trying to queue an email sending invoice emails in laravel 5.1, i pass in a variable called invoice, when i dd($invoice->dateString()) in the Job class it's return the correct value but when i pass it in to the view the $invoice variable return empty array (so i get an error about trying to get property from non-object...).

the second problem i have is when i try to add attachment to the job it returns an error : "Serialization of closure failed: Serialization of 'SplFileInfo' is not allowed".

the job class looks like that:

namespace LM2\Jobs;

use Guzzle\Service\Client;
use LM2\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use LM2\Models\User as User;
use LM2\Models\Client as LMClient;

class SendInvoiceEmail extends Job implements SelfHandling, ShouldQueue
{

protected $user;
protected $invoice;
protected $attachment;
protected $update;

public function __construct(User $user, LMClient $client, $invoice,$update)
{
    $this->user = $user;
    $this->client = $client;
    $this->invoice = $invoice;
    $this->update = $update;
}

public function handle()
{
    $attachment = $this->client->invoiceFile($this->invoice->id,['vendor' => 'Test','product' => 'Your Product']);
    $invoice = $this->invoice;
    $data = [
        'invoice' => $this->invoice,
        'update'=> $this->update,
    ];
    $user = $this->user;
    \Mail::queue('emails.invoices', $data , function($m) use ($user,$invoice,$attachment){
        $m->to($user->email)->subject('New payment received')->attach($attachment);
    });
}

}

and my controller function looks like that:

public function sendEmailInvoice($update = false){
        $client = \Auth::client();
        $user = \Auth::user();
        $invoices = $client->invoices();
        $this->dispatch(new SendInvoiceEmail($user,$client,$invoices[0],$update));
        $activity = $data['update'] ? 'updated': 'added';
        return ['success', $activity];
    }

can someone please tell me what am i doing wrong? thanks a lot you all :)

Upvotes: 2

Views: 425

Answers (2)

benjah
benjah

Reputation: 660

so i found the answer thanks to @Michael, i have changed my handle so it's look like this now:

public function handle(Mailer $mailer)
{
    $client = $this->client;
    $invoice = $this->invoice;
    $data = [
        'date' => $invoice->dateString(),
        'amount' => $invoice->dollars(),
        'update'=> $this->update,
    ];
    $user = $this->user;
    return $mailer->queue('emails.invoices', $data , function($m) use ($user,$client,$invoice){
        $attachment = $client->invoiceFile($invoice->id,['vendor' => 'Infogamy','product' => 'Your Product']);
        $m->to($user->email)->subject('New payment received')->attach($attachment);
    });
}

The attachment should be processed inside the mailer callback function, and the function called from the $invoice variable (object) should be called inside the handle function and not in the blade view template.

Upvotes: 1

Michael
Michael

Reputation: 245

Just a guess... but when using Mail::queue() the $data get's converted/cast to an array/you lose your objects inside of the view - hence why you're receiving errors when trying to call methods(), because they don't exist.

Rather than passing invoice + update objects, get what you need from them in the handle method and construct the $data array.

$data = [
    'invoice_foo' => $invoice->getFoo(),
    'invoice_bar' => $invoice->getBar(),
];

*** Apologies if this doesn't help at all!

Upvotes: 2

Related Questions