Skip to content

Instantly share code, notes, and snippets.

@anubra266
Last active September 14, 2024 12:57
Show Gist options
  • Save anubra266/6c488f3cc52735185076182d01777ddc to your computer and use it in GitHub Desktop.
Save anubra266/6c488f3cc52735185076182d01777ddc to your computer and use it in GitHub Desktop.
Laravel Queue Email Verify and Password Reset Mails

Laravel Queue Email Verify and Password Reset Mails

First step is to overwrite the default methods in your User Model.

NB: We are still using the default Laravel Notifications.

# App/Models/User.php

<?php

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Auth\Notifications\ResetPassword;
class User extends Authenticatable implements MustVerifyEmail
{
 ...
 
  /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmail());
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }
    
 }

If you understand Queing, you can understand the rest from there. Finding those methods was the problem for me. If not, let's move on.

Create Custom Notification Classes that Extend the default Classes, and Queue the custom classes.

php artisan make:notification Auth/VerifyEmail
php artisan make:notification Auth/ResetPassword

For Email Verification

# App\Notifications\Auth\VerifyEmail.php

<?php

namespace App\Notifications\Auth;

use Illuminate\Bus\Queueable;
use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailNotification;
use Illuminate\Contracts\Queue\ShouldQueue;

class VerifyEmail extends VerifyEmailNotification implements ShouldQueue
{
    use Queueable;    
    
    public function __construct()
    {
        //specifying queue is optional but recommended
        $this->queue = 'auth';
    }
}

For Password Reset

# App\Notifications\Auth\ResetPassword.php

<?php

namespace App\Notifications\Auth;

use Illuminate\Bus\Queueable;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
use Illuminate\Contracts\Queue\ShouldQueue;

class ResetPassword extends ResetPasswordNotification implements ShouldQueue
{
    use Queueable;   
    
    public $token;
    
    public function __construct($token)
    {
        //required to persist the token for the queue
        $this->token = $token;
        //specifying queue is optional but recommended
        $this->queue = 'auth';
    }
}

Now Update the methods in your User Model to use your custom classes

# App/Models/User.php

<?php

class User extends Authenticatable implements MustVerifyEmail
{
 ...
 
  /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new \App\Notifications\Auth\VerifyEmail());
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new \App\Notifications\Auth\ResetPassword($token));
    }
    
 }

Now, you may need to run a worker to check if everything’s playing out.

php artisan queue:work

NB: If you specified a queue name in your constructor

php artisan queue:work --queue=auth

You should Edit The passwords config and change the throttling message to a better one.

resources/lang/en/passwords.php

Example

'throttled' => 'You\'ve requested a password reset recently, please wait before retrying.',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment