0

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?

hyphen
  • 2,368
  • 5
  • 28
  • 59

1 Answers1

2

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! :)

Dennis
  • 1,281
  • 13
  • 30
  • Awesome, thank you. I'll try these today. I'm sort of hacking the intended functionality, which could be part of the problem. Rather than having users register themselves, users in my app will be created in the application by an admin, which should trigger the email to go out. It seems like this should be easy to do using the built in functionality, but perhaps I'm assuming too much. I'm new to laravel. – hyphen Nov 17 '21 at 14:19
  • Nice, let me know if it worked out for you, or you have any questions! – Dennis Nov 17 '21 at 14:32
  • I just tried the steps you provided and I'm not seeing anything different. I'm using jetstream, and given my previous comment about creating users vs having them register, I'm wondering if some aspect of that is causing problems. – hyphen Nov 17 '21 at 14:46
  • well, I at least know part of the problem. when I switched to using my test route, I just used User::find(1) to get a user, which is my current user, which is already verified. So once I nulled out the email_verified_at field in the DB, it seems I can get a step further. Now I'm getting an error which is useful, stating that I must have a from address, so that's a step in the right direction. – hyphen Nov 17 '21 at 14:50
  • Ah yeah, the "already verified user" haha, I've had the same problems without directly knowing what the problem was ^^ Good to here you fixed it! – Dennis Nov 17 '21 at 15:13