Josh
Josh

Reputation: 1970

Class 'Illuminate\Mail\Events\MessageSent' not found in Laravel 5.4 notification

I need help to resolve an issue with the notification system.

Here is my notification class:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class ApplicationReceived extends Notification
{
    use Queueable;


    private $testCode;
    private $recipientName;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($testCode='',$name='')
    {
        $this->testCode=$testCode;
        $this->recipientName=$name;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                ->greeting("Dear ".$this->recipientName)
                ->subject('Your Application Received for The Entrepreneur')
                ->line("Your application to participate in The Entrepreneur project has been received by us")
                ->line("Follow the link below to take a test to complete your application")
                ->action('Take Test', url('/taketest/'.$this->testCode))
                ->line('Thank you for your interest in The Entrepreneur project!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
      }
}

And this is where I call it:

//Notify the candidate
$candidate->notify(new ApplicationReceived($candidate->testcode, $candidate->othernames));
return redirect('/signup')->with('msg', "<div class='alert alert-success'><strong>Your registration information was successfully submitted!</strong><br /> Please check your email for a link to take a test to complete your enrollment!!!</div>");

When the application runs, it sends the notification but then throws up the following fatal error message

FatalErrorException in Mailer.php line 470: Class 'Illuminate\Mail\Events\MessageSent' not found

the error displayed

I will appreciate any help to point out where I am going wrong here.

Upvotes: 2

Views: 573

Answers (2)

Josh
Josh

Reputation: 1970

Thank you @dparoli for your suggestion.

However, I tried both composer dumpautoload and composer update all to no avail.

Actually, what I did that solved the problem is very close to what you suggested. After creating a new laravel project, I realized that it contains the missing file (MessageSent.php) in the same location as MessageSending.php.

So, I just copied the file (MessageSent.php) to the same in my own project and it worked.

Thank you for your efforts and pointing me in the right direction.

I really hope this helps someone

Upvotes: 1

dparoli
dparoli

Reputation: 9161

The class Illuminate\Mail\Events\MessageSent was added only in the last version of laravel (5.4.18), so you can proceed this way:

Run composer update just in the case the last run was gone wrong.

If the error is still there IMHO it could be a permissions issue so there are two cases:

1) you run composer update with a user that has not writing permissions in the vendor directory, so you have to use another user (see case 2)

2) you run composer update with root and you forgot to chown the laravel directory to the web server user/group, i.e.:

chmod -R nginx:nginx /path/to/laraveldir

Sobstitute nginx:nginx with your webserver user:group.

In either case a stop and start to the webserver service could also help.

Upvotes: 3

Related Questions