简体   繁体   中英

Laravel Notify doesn't send email to Mailtrap

I want the Laravel Login system to send me emails after logging into a website. Here is my authenticated method from LoginController .

if ($user->hasTwoFactorAuthenticatedEnabled()) {
    auth()->logout();

    $request->session()->flash('auth', [
        'user_id' => $user->id,
        'using_sms' => false,
        'remember' => $request->has('remember')
    ]);

    if ($user->two_factor_type == 'sms') {
        $code = ActiveCode::generateCode($user);
        // Todo Send Sms
        $request->session()->push('auth.using_sms', true);
    }

    return redirect(route('2fa.token'));
}

$user->notify(new LoginToWebsiteNotification());

return false;

As you can see I have added $user->notify(new LoginToWebsiteNotification()); and LoginToWebsiteNotification holds:

class LoginToWebsiteNotification extends Notification
{
    use Queueable;
    
    public function __construct()
    {
        //
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('u Loggedin')
            ->view('emails.login-to-website');
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

The problem here is that, after logging in, nothing sends to the user's email address. Note that mail settings work fine, and I tested it when a new user is registering at the website. So if you know why this notify method does send me an email, please let me know; I would really appreciate any idea from you guys.

I want the Laravel Login system to send me emails after logging into a website. Here is my authenticated method from LoginController .

if ($user->hasTwoFactorAuthenticatedEnabled()) {
    auth()->logout();

    $request->session()->flash('auth', [
        'user_id' => $user->id,
        'using_sms' => false,
        'remember' => $request->has('remember')
    ]);

    if ($user->two_factor_type == 'sms') {
        $code = ActiveCode::generateCode($user);
        // Todo Send Sms
        $request->session()->push('auth.using_sms', true);
    }

    return redirect(route('2fa.token'));
}

$user->notify(new LoginToWebsiteNotification());

return false;

As you can see I have added $user->notify(new LoginToWebsiteNotification()); and LoginToWebsiteNotification holds:

class LoginToWebsiteNotification extends Notification
{
    use Queueable;
    
    public function __construct()
    {
        //
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('u Loggedin')
            ->view('emails.login-to-website');
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

The problem here is that, after logging in, nothing sends to the user's email address. Note that mail settings work fine, and I tested it when a new user is registering at the website. So if you know why this notify method does send me an email, please let me know; I would really appreciate any idea from you guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM