Home > other >  Laravel 8 SendVerificationEmail Event Seemingly Not Firing
Laravel 8 SendVerificationEmail Event Seemingly Not Firing

Time:11-18

I've followed the instructions listed in the docs for email verification. My user model implements the MustVerifyEmail interface, and I'm firing the event in my controller after creating the user. I've also verified that I can send a random email via Tinker, which works and comes through in Mailhog.

I've created a test route in my UserController that should fire the event, but it is seemingly not getting fired:

    public function test()
    {
        Log::debug('testing email');
        $user = User::find(1);
        Log::debug($user);
        event(new Registered($user));
        Log::debug('event should have fired');
    }

In EventServiceProvider.php:

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

In my User model:

namespace App\Models;

use Laravel\Sanctum\HasApiTokens;
use Laravel\Jetstream\HasProfilePhoto;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Contracts\Auth\MustVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail
{
...

It's writing to the log as expected, but there's no email. I don't think I'm missing any steps from the docs, and I'm unsure of how to troubleshoot. How can I debug events, or what am I missing in my implementation?

CodePudding user response:

Good question and great to see your steps you already took and the information you provided, lots of people can learn from it!

Back to the question, i've some suggestions you can check/try.

  1. Check if the user you are trying to send a mail to, isn't already verified, the check in the SendEmailVerificationNotification has this check, which confused me too, why I wasn't getting a mail. And make sure to follow these steps from the docs: https://laravel.com/docs/master/verification#the-email-verification-handler

    if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) {
        $event->user->sendEmailVerificationNotification();
    }
    
  2. Double check your logs, check for any errors/exceptions which can give you more information

  3. Since you've changed your provider EventServiceProvider, try running composer dump-autoload, it will rebuild the list of classes which need to be included in your application.

  4. You can send a mail like you said, but to be sure, check that the QUEUE_CONNECTION in your .env file is set to sync, if not, you should run the php artisan queue:work (don't forget the --queue= option!) for database driver and php artisan horizon (if installed) for redis driver, otherwise the event is maybe queued but cannot run. For debugging, the sync driver is the easiest one.

I've just tried your steps with a fresh Laravel application and everything works fine (after following the steps above, because it wasn't working at my system too haha)

Hoping this checklist will help you solving the problem, let me know if you've any questions! :)

  • Related