Back

Error on sending mail


blyleven posted 2 weeks ago

Hi all,

On a Laravel 5.4 project I'm working on I'm facing a strange issue. On my local machine everything works as planned, but when testing on a staging server, I'm getting a weird error while sending certain emails. These emails are queued.

This is the error:

exception 'ErrorException' with message 'main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "App\Models\User" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition

I'm getting this error after refactoring all models and placed them inside a Models folder. All models are having the correct namespace, I triple checked that: namespace App\Models for example.

After I deployed these are the commands I ran:

  • composer dumpautoload
  • php artisan config:cache
  • php artisan queue:restart

The user model:

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, SoftDeletes;
    bla bla bla...
}

The stripped down code which queues the email:

  • Importing the User model
<?php

namespace App\Http\Controllers\Voice;

use Mail;
use App\Models\User;
use App\Mail\ClientFilesReadyForDownload;

  • Queue the mail:
$client = User::find($clientId);

$data = new \stdClass();
$data->client = $client;

Mail::to($client->email)->queue(new ClientFilesReadyForDownload($data));

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ClientFilesReadyForDownload extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    /**
     * Create a new message instance.
     *
     * @param mixed $data
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.delivery')->subject('Your files are ready to download');
    }
}

I hope anybody can help me out because I'm out of options here...

Thanks!


Sign in to participate in this thread!



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