Custom Password Reset Email
December 30, 2024 · AI Conversations
### Steps to Customize the Email Template
1. **Create a Custom Notification**
Laravel uses the `IlluminateAuthNotificationsResetPassword` notification to send the password reset link. You need to override this with your custom notification.
2. **Generate a Custom Notification**
Run the following command to create a new notification:
php artisan make:notification CustomResetPasswordNotification
3. **Modify the Notification**
Open the newly created `CustomResetPasswordNotification` file in `app/Notifications` and customize it:
<?php
namespace AppNotifications;
use IlluminateAuthNotificationsResetPassword as BaseResetPassword;
use IlluminateNotificationsMessagesMailMessage;
class CustomResetPasswordNotification extends BaseResetPassword
{
/**
* Build the mail representation of the notification.
*
* @param string $token
* @return IlluminateNotificationsMessagesMailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Custom Password Reset Request')
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', url(route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
->line('If you did not request a password reset, no further action is required.');
}
}
4. **Update the Model to Use the Custom Notification**
In your `Coach` model (or whichever model corresponds to the `coaches` guard), override the `sendPasswordResetNotification` method:
<?php
namespace AppModels;
use IlluminateFoundationAuthUser as Authenticatable;
use AppNotificationsCustomResetPasswordNotification;
class Coach extends Authenticatable
{
// Other model properties and methods
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new CustomResetPasswordNotification($token));
}
}
5. **Test the Implementation**
Ensure that when you request a password reset link for the `coaches` guard, your custom email is sent.
By following these steps, you can fully customize the email template for password reset requests in your Laravel application.
$request->only('email')
);
return $status == Password::RESET_LINK_SENT
? back()->with('status', __($status))
: back()->withInput($request->only('email'))
->withErrors(['email' => __($status)]);
add email template here
Here’s how you can associate the custom email template directly with the password reset functionality:
### Step 1: Customize the Password Notification
Laravel's password reset emails are sent via the `IlluminateAuthNotificationsResetPassword` notification. You can create a custom notification as explained earlier.
### Step 2: Attach the Custom Notification to the Guard
Update the `Password::broker('coaches')->sendResetLink()` logic to ensure the custom notification is used.
#### Full Implementation Example
Modify your `store` method in the `PasswordResetLinkController` to include the notification logic:
use AppNotificationsCustomResetPasswordNotification;
use IlluminateSupportFacadesPassword;
use IlluminateSupportFacadesValidator;
public function store(Request $request): RedirectResponse
{
$request->validate([
'email' => ['required', 'email'],
]);
// Retrieve the user for the 'coaches' guard
$user = Password::broker('coaches')->getUser($request->only('email'));
if ($user) {
// Generate the token
$token = Password::broker('coaches')->createToken($user);
// Send custom notification with the token
$user->notify(new CustomResetPasswordNotification($token));
return back()->with('status', __('A password reset link has been sent to your email.'));
}
return back()->withInput($request->only('email'))
->withErrors(['email' => __('We couldn't find a user with that email address.')]);
}
### Step 3: Custom Notification Template
Ensure your `CustomResetPasswordNotification` uses the desired email template. For example:
namespace AppNotifications;
use IlluminateAuthNotificationsResetPassword as BaseResetPassword;
use IlluminateNotificationsMessagesMailMessage;
class CustomResetPasswordNotification extends BaseResetPassword
{
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Your Custom Password Reset Request')
->view('emails.custom-password-reset', ['token' => $this->token]);
}
}
### Step 4: Create the Custom Email Blade View
Create a new Blade file at `resources/views/emails/custom-password-reset.blade.php`:
<!DOCTYPE html>
<html>
<head>
<title>Password Reset</title>
</head>
<body>
<h1>Reset Your Password</h1>
<p>You requested a password reset. Click the button below to reset your password:</p>
<a href="{{ url(route('password.reset', ['token' => $token, 'email' => $notifiable->email], false)) }}" style="padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none;">Reset Password</a>
<p>If you did not request a password reset, please ignore this email.</p>
</body>
</html>
### Explanation
– **Notification Hook:** The custom notification is triggered manually, bypassing the default Laravel behavior.
– **Custom Blade View:** Allows full control over the email design and content.
– **Dynamic Links:** The email link includes the reset token and user email for proper routing.
This ensures that the custom email template is seamlessly used during the password reset process for the `coaches` guard.