You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 7 用户注册后未发送验证邮件问题求助

解决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 registered method in RegisterController, 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_at has a default value of NULL.
  • Double-check your registration code (like the create method in RegisterController) to make sure you're not accidentally setting email_verified_at to now() 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 authenticated method of LoginController to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 06:32:34