Back

Serialization of closure failed: Serialization of 'Closure' is not allowed


Jamesking56 posted 3 years ago

Hey everyone,

When I try using Mail::queue() I get this error:

ErrorException in SerializableClosure.php line 93:
Serialization of closure failed: Serialization of 'Closure' is not allowed

Any ideas why? Here is the method I use in my own Mailer abstract class:

public function send()
{
	return $this->mailer->queue($this->view, $this->data, function($message){
		$message->to($this->to)->subject($this->subject);
	});
}
Jamesking56 replied 3 years ago Solution

Well turns out I just solved it myself. For future reference the issue is using $this inside of the closure.

$this->to and $this->subject are references to fields on the Class and not in the Closure so to fix the code I had to make them local variables and pass them to my closure like so:

public function send()
{
	$to = $this->getTo();
	$subject = $this->getSubject();

	return $this->mailer->queue($this->getView(), $this->getData(), function($message) use($to, $subject) {
		$message->to($to)->subject($subject);
	});
}
victorboissiere replied 3 years ago

Thanks, it worked ;)

vahid1324 replied 3 years ago

Thank you!

kkiernan replied 2 years ago

Thanks man!

dsampaolo replied 2 years ago

Thank you :)

Matias Barletta replied 3 months ago

In my case was due to defining a closure function in the constructor. Took it out and it worked.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us