Share this:

Hello, Today we are going to learn Implementing Email Verification in Laravel 10: A Step-by-Step Guide. Email verification is a fundamental security feature for web applications that require user registration. It ensures that the email address provided by the user is valid and that the user has access to it. Laravel, a popular PHP web application framework, provides built-in email verification functionality to make it easier for developers to implement this feature in their applications.

In this step-by-step guide, we’ll walk through the process of implementing email verification in a Laravel 10 application. We’ll start by configuring the mail driver, generating authentication scaffolding, and enabling email verification in the User model. Then, we’ll customize the email templates and modify the registration controller to send verification emails. Finally, we’ll test our implementation to ensure that email verification is working correctly.

By the end of this tutorial, you’ll have a secure Laravel 10 application that verifies the email addresses of new users and prevents spam registrations.

Steps for Email Verification in Laravel 10

  • Step 1: Installing Laravel 10
  • Step 2: Configure Mail Driver
  • Step 3: Generate Authentication Scaffolding
  • Step 4: Enable Email Verification
  • Step 5: Configure Email Templates
  • Step 6: Modify Registration Controller
  • Step 7: Customize Verification Notification
  • Step 8: Test Email Verification
  • Conclusion

Step 1: Installing Laravel 10

Before we begin, make sure you have the latest version of Laravel 10 installed on your system. You can install it by running the following command in your terminal:

composer create-project laravel/laravel my-project

Once you have Laravel 10 installed, we can move on to implementing email verification.

Step 2: Configure Mail Driver

Before we can send verification emails, we need to configure the mail driver in our Laravel application. Open the .env file in your Laravel application and set the MAIL_DRIVER variable to smtp. Then, configure the MAIL_HOST, MAIL_PORT, MAIL_USERNAME, and MAIL_PASSWORD variables with the appropriate values for your email provider.

Step 3: Generate Authentication Scaffolding

Next, we need to generate the authentication scaffolding in our Laravel application. Run the following command in your terminal:

php artisan make:auth

This command will generate the necessary views, routes, and controllers for authentication in your Laravel application.

Step 4: Enable Email Verification

To enable email verification in our Laravel application, we need to modify the App\Models\User model. Open the User model and add the Illuminate\Contracts\Auth\MustVerifyEmail contract to the class definition:

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    // ...
}

This contract tells Laravel that the User model should implement email verification.

Step 5: Configure Email Templates

Next, we need to configure the email templates for verification emails. Laravel provides default templates for email verification, but we can customize them to fit our needs. To customize the email templates, run the following command in your terminal:

php artisan vendor:publish --tag=laravel-notifications

This command will publish the default email templates to the resources/views/vendor/notifications directory in your Laravel application. You can modify these templates as needed to fit your application’s design and branding.

Step 6: Modify Registration Controller

To send verification emails when a user registers, we need to modify the RegisterController controller. Open the RegisterController and add the Illuminate\Auth\Events\Registered event to the register() method:

use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    // ...

    protected function create(array $data)
    {
        // ...
    }

    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }

    // ...
}

This event triggers when a user registers, and it’s responsible for sending the verification email. By default, Laravel sends the verification email using the toMail() method in the Illuminate\Auth\Notifications\VerifyEmail notification. We’ll customize this notification in the next step.

Step 7: Customize Verification Notification

To customize the email content and subject of the verification email, we need to modify the VerifyEmail notification. Open the VerifyEmail notification located at app/Notifications/VerifyEmail.php. You can modify the toMail() method to customize the email subject and content:

use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailBase;
use Illuminate\Notifications\Messages\MailMessage;

class VerifyEmail extends VerifyEmailBase
{
    /**
     * Get the verification mail representation.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    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.');
    }
}

In this example, we’ve customized the email subject and content to fit our needs. You can modify these values to fit your application’s requirements.

Step 8: Test Email Verification

That’s it! You can now test the email verification functionality in your Laravel application. When a user registers, they’ll receive an email with a link to verify their email address. Once they click the link, they’ll be redirected to your application and their email address will be marked as verified.

If you encounter any errors, make sure to check your mail driver configuration and ensure that your email provider allows sending emails from your Laravel application.

Conclusion

In this tutorial, we learned how to implement email verification in Laravel 10 using the built-in email verification feature. We configured the mail driver, generated authentication scaffolding, enabled email verification in the User model, configured email templates, modified the registration controller, and customized the verification notification. With email verification in place, your Laravel 10 application will be more secure and less prone to spam registrations.

Share this:

Categorized in: