skip to Main Content

I am trying to send simple email through mailgun and laravel, but getting a weird error.
I am not using queue just sending a simple welcome email on run time.

following is error:

Serialization of ‘Closure’ is not allowed

Following is mail send code:

$details = array(
        'email' => $request->email,
        'password' => $request->password,
    );

 Mail::send('emails.welcome', $details, function ($message) use ($user) {
        $message->from('[email protected]', 'Admin');
        $message->to($user->email);
 });

When I comment above code, everything works fine.

2

Answers


  1. I think you got the use of the closure wrong. This should work:

    Mail::from('[email protected]', 'Admin')->to($user->email)->send('emails.welcome', $details)
    
    Login or Signup to reply.
  2. So basically, this is how you trigger the email:

    Mail::to("[email protected]")->send(new OrderCreated($order));
    

    OrderCreated class:

    <?php
    
    namespace AppMail;
    
    use IlluminateBusQueueable;
    use IlluminateContractsQueueShouldQueue;
    use IlluminateMailMailable;
    use IlluminateQueueSerializesModels;
    
    class OrderCreated extends Mailable
    
    {
    
    use Queueable, SerializesModels;
    
    public $content;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($content)
    {
        $this->content = $content;
    }
    
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from(config("mail.mail_from"), "Service Desk")
        ->subject("Order Created")
        ->markdown('emails.order-created')->with('content',$this->content);
    }
    }
    

    mail.mail_from is the blade view which would be used to create email that will be shown to the user.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search