sending and verifying emails in laravel 10 with mailtrap.io

In this tutorial, we'll learn how to use Mailtrap in Laravel 10 for sending and verifying emails, and we'll make use of Laravel Breeze to simplify the process.


Step 1: Create Laravel Project

First, install Laravel if you haven't already. Use Composer to create a new Laravel project.

composer create-project laravel/laravel laravel-mail


Step 2: Set Up Mailtrap

Sign up for a Mailtrap.io account if you don't already have one. After creating an account, set up a new inbox, and Mailtrap will provide you with SMTP settings, including the host, port, username, and password.

laravel 10 mailtrap

laravel 10 mailtrap

Step 3: Configure .env for Mailtrap

Edit your .env file with Mailtrap settings.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS[email protected]
MAIL_FROM_NAME="${APP_NAME}"


Step 4: Install Laravel Breeze

Install Laravel Breeze using Composer.

composer require laravel/breeze --dev

Then, install Breeze's scaffolding.

php artisan breeze:install

After installing Breeze, compile your assets.

npm install
npm run dev


Step 5: Migrate the Database

Run the migrations to create the necessary tables.

php artisan migrate


Step 6: Enable Email Verification

In your User model (app/Models/User.php), implement the MustVerifyEmail interface.

use Illuminate\Contracts\Auth\MustVerifyEmail;

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


Step 7: Update Routes

Update your routes in routes/web.php to enforce email verification.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\YourController;

Route::get('/', function () {
  return view('welcome');
});

Route::middleware(['auth', 'verified'])->group(function () {
  // Your authenticated and verified routes
});

require __DIR__.'/auth.php';


Step 8: Test the Application

Run your application and test the registration process.

php artisan serve

Register a new user and check your Mailtrap inbox for the verification email.

laravel 10 register new user send mail with mailtrap

laravel 10 register new user send mail with mailtrap

Check Verify Email Address In your Mailtrap Account.

laravel 10 email verification code with mailtrap

laravel 10 email verification code with mailtrap