Laravel Email Verification Queue
November 4, 2024 · AI Conversations
Here's how you can set it up:
### Step 1: Configure the Mail Queue in `config/queue.php`
Make sure your queue connection is set up in `config/queue.php`. For example, you might use a `database`, `redis`, or `sqs` connection. Here's a basic setup for a `database` queue:
// config/queue.php
'default' => env('QUEUE_CONNECTION', 'database'),
In your `.env` file, ensure you have:
QUEUE_CONNECTION=database
Run the migration if you haven't set up the queue table yet:
php artisan queue:table
php artisan migrate
### Step 2: Create a Queued Verification Notification
You can override the default email verification notification to make it queued. Create a new notification, like `QueuedVerifyEmail`.
1. Run the command to generate the notification:
php artisan make:notification QueuedVerifyEmail
2. In `QueuedVerifyEmail.php`, add the `ShouldQueue` interface and implement the notification with the `MailMessage` for email verification.
<?php
namespace AppNotifications;
use IlluminateNotificationsNotification;
use IlluminateContractsQueueShouldQueue;
use IlluminateAuthNotificationsVerifyEmail as BaseVerifyEmail;
use IlluminateNotificationsMessagesMailMessage;
class QueuedVerifyEmail extends BaseVerifyEmail implements ShouldQueue
{
/**
* Build the mail representation of the notification.
*
* @param mixed $notifiable
* @return IlluminateNotificationsMessagesMailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Verify Your Email Address')
->line('Please click the button below to verify your email address.')
->action('Verify Email Address', $this->verificationUrl($notifiable))
->line('If you did not create an account, no further action is required.');
}
}
### Step 3: Use the Queued Notification in Your User Model
Override the `sendEmailVerificationNotification` method in your `User` model to use the `QueuedVerifyEmail` notification.
1. Open `User.php`.
2. Add this method to specify the new queued notification.
public function sendEmailVerificationNotification()
{
$this->notify(new AppNotificationsQueuedVerifyEmail);
}
### Step 4: Test the Queued Email Verification
Make sure your queue worker is running:
php artisan queue:work
Now, when a new user registers or an existing user requests email verification, the notification should be queued and processed by your queue worker.
1. **Make the class implement `ShouldQueue`.**
2. **Adjust the content to include the email verification URL** for the specific user.
3. **Override the `sendEmailVerificationNotification`** method in your `User` model to use this custom `VerifyEmailQueued` notification.
Here’s how to implement these changes:
### Step 1: Update the `VerifyEmailQueued` Notification
Make sure the notification uses `ShouldQueue` and includes a proper verification link. You can use the `verificationUrl` method from the base `VerifyEmail` notification to generate the URL.
Update the `VerifyEmailQueued` notification like this:
<?php
namespace AppNotifications;
use IlluminateAuthNotificationsVerifyEmail as BaseVerifyEmail;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
use IlluminateNotificationsNotification;
class VerifyEmailQueued extends Notification implements ShouldQueue
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
$verificationUrl = $this->verificationUrl($notifiable);
return (new MailMessage)
->subject('Verify Your Email Address')
->line('Please click the button below to verify your email address.')
->action('Verify Email Address', $verificationUrl)
->line('If you did not create an account, no further action is required.');
}
/**
* Generate the verification URL for the given notifiable.
*
* @param object $notifiable
* @return string
*/
protected function verificationUrl($notifiable)
{
return URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $notifiable->getKey(), 'hash' => sha1($notifiable->getEmailForVerification())]
);
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [];
}
}
### Step 2: Use the Custom Notification in the `User` Model
In your `User` model, override the `sendEmailVerificationNotification` method to send the `VerifyEmailQueued` notification:
<?php
namespace AppModels;
use AppNotificationsVerifyEmailQueued;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
// Other model properties and methods...
/**
* Send the email verification notification.
*/
public function sendEmailVerificationNotification()
{
$this->notify(new VerifyEmailQueued());
}
}
### Step 3: Start the Queue Worker
To process queued notifications, make sure your queue worker is running:
php artisan queue:work
With these changes, the email verification notification will be queued and processed by your queue worker, reducing the load on your main application thread and improving the user experience during registration.