Laravel 8 Email using Laravel Breeze

Laravel 8 Email using Laravel Breeze

ยท

2 min read

Table of contents

  1. Create new Laravel project
  2. Configure Database and Email Credentials
  3. Install Laravel Breeze
  4. Configure Email Verification

1. Create new project.

In your terminal execute

composer create-project laravel/laravel Email-Verification

2. Configure Database and Email Credentials.

Open your .env file and edit your credentials like this

# Database Credentials
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

#Email Credentials
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=example@example.com
MAIL_FROM_NAME="Example"

Note: In this case, I'm using mailtrap to send the emails but if you want to configure your Gmail account to send emails you can check this article How to reset password using Gmail

3. Install Laravel Breeze

To install Laravel Breeze execute this command in your terminal

composer require laravel/breeze

After installing the package, run this command

php artisan breeze:install

To finish this part you need to run

npm install && npm run dev

Note: You need to have installed node.js to execute npm install && npm run dev

Finally we need to run our migrations.

php artisan migrate

Now we can see the registration and login page.

Registration

registration_page.png

Login

login_page.png

Let's move to the next step.

4. Configure Email Verification

First, we need to go to app/Models/User.php and add implements MustVerifyEmail

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens, HasFactory, Notifiable;
}

The next step is to go to routes/web.php, add the middleware verified to the routes

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Now, we can try to register a new user

registration_user.png

After registration, you'll see this page

send_verification_email.png

In your mailtrap account, you should receive an email like this

mailtrap.png

Just click Verify Email Address button and that's it.

dashboard.png

If you are a curious person you can check your database and you'll see the verification date

database.png

Laravel 8 provides us with a very easy way to verify your email.

If you have any doubt leave a message in the comment section.

Happy coding โ˜•. Thanks for reading ๐Ÿ™.

ย