Laravel 7 用户注册后未发送验证邮件问题求助
Hey there, let's work through this issue together— I've dealt with similar Laravel 7 email verification glitches before, so here are the key troubleshooting steps:
1. 确保User模型实现了MustVerifyEmail接口
This is the most common oversight! Laravel relies on this interface to trigger the verification email flow. Open your app/User.php file and make sure it implements the interface:
use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements MustVerifyEmail { // ... 你的模型代码 }
If you skip this, Laravel won't recognize that the user needs email verification, so no email will be sent.
2. 检查注册控制器的触发逻辑
Laravel's default RegisterController uses the RegistersUsers trait, which automatically sends the verification email after creating a user. But if you've customized the registration process:
- Make sure you're calling
$user->sendEmailVerificationNotification();right after saving the user in your registration logic. - If you've overridden the
registeredmethod inRegisterController, ensure you haven't removed the parent call or the notification trigger. For example:
protected function registered(Request $request, $user) { // 你的自定义逻辑 $user->sendEmailVerificationNotification(); // 必须保留这一行来发送验证邮件 return redirect($this->redirectPath()); }
3. 确认email_verified_at字段未被意外赋值
If the email_verified_at column in your users table is being set to a non-null value during registration (either via default value or custom code), Laravel will treat the user as already verified and skip sending the email:
- Check your migration or database schema to ensure
email_verified_athas a default value ofNULL. - Double-check your registration code (like the
createmethod inRegisterController) to make sure you're not accidentally settingemail_verified_attonow()or another value.
4. 清除Laravel缓存
Sometimes cached configurations or routes can prevent new settings from taking effect. Run these commands in your terminal:
php artisan cache:clear php artisan route:clear php artisan config:clear
This ensures the Auth::routes(['verify' => true]) configuration is loaded correctly.
5. 调整自动登录行为(可选)
If you want to prevent unverified users from logging in immediately after registration (instead of letting them log in but restricting access to certain routes), you can customize the login flow:
- Add a check in the
authenticatedmethod ofLoginControllerto log out unverified users:
// 在LoginController中 protected function authenticated(Request $request, $user) { if (!$user->hasVerifiedEmail()) { auth()->logout(); return redirect()->route('verification.notice') ->with('message', '请先验证您的邮箱再登录!'); } return redirect()->intended($this->redirectPath()); }
最后验证
After making these changes, test the registration flow again. Since your MAIL_MAILER=log, check the storage/logs/laravel.log file— you should see the verification email entry if everything works correctly.
内容的提问来源于stack exchange,提问作者Meaulnes

