dan3460's avatar

Serialization of closure is not allowed, in queuing a mail

I have a simple email that sends files to a customer.

    public function storeReceiver(Request $request)
    {
        $files = Document::whereIn('document_id',explode(',',$request->fileList))->get();
        $receiver = Receiver::find($request->receiverID);

        Mail::to($receiver->entity->entity_contact_email)->queue(new SendDocuments($files));
    }

That works fine, but in the request i also have some text that i want to put in the body of the email. So when i do this:

Mail::to($receiver->entity->entity_contact_email)->queue(new SendDocuments($files,$request));

Gives me the error in the title. If i replace queue by send, works fine. So its clearly complaining about the two parameters in the mail object when sent to the queue. Is there any way around this?

0 likes
2 replies
lostdreamer_nl's avatar

instead of passing the complete Request class, simply add only the request's input as an array:

Mail::to($receiver->entity->entity_contact_email)->queue(new SendDocuments($files,$request->all()));

And change your SendDocuments class to accept an array as its second argument.

dan3460's avatar

For some reason the mail enters on a loop when i do the proposed solution. To test i'm not using the $request anywhere, i only passing it as shown by @lostdreamer_nl to the SendDocument Mailable. I took all references also from the SendDocument.blade.php. Here is the Mailalble:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\File;
use App\Jobs\DeleteFiles;
use App\Document;

class SendDocuments extends Mailable
{
    use Queueable, SerializesModels;

    public $files;
    public $request;

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

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

        $message = $this->markdown('mails.sendDocuments');
        if ($this->files) {
            foreach ($this->files as $file) {
                File::copy(storage_path('app/' . $file->file_id), public_path($file->file_id));
                $message->attach(public_path($file->file_id), [
                    'as' => $file->type->document_type_name . '-' . $file->audit . '.pdf',
                    'mime' => 'application/pdf'
                ]);
                DeleteFiles::dispatch(public_path($file->file_id))->delay(now()->addSeconds(config('system.deleteTime')));
            }
        }

        return $message;
    }
}

Please or to participate in this conversation.