How to Send OTP in Email in Laravel 10

  Feb 2024
  Category: Laravel
How to Send OTP in Email in Laravel 10

Welcome To ITSolutionsGuides,

Sending OTP in mail is widely used technique for reseting any passwords since sending SMS are all paid service mail was widely used , So its necessary to know how to send a OTP mail in laravel. Sending OTP via email in Laravel 10 is crucial for user authentication and security. To implement this, generate a random OTP using Laravel's rand function , create a blade template for the email containing the OTP, and use Laravel's Mail class to send the email to the user's address after configuring email settings. Generate a mailable class to customize the email content and facilitate passing the OTP. Upon receiving the OTP, implement the verification process by comparing it with the stored OTP. Thoroughly test the functionality in different scenarios to ensure its effectiveness.

To send OTP in email in Laravel 10, utilize Laravel's built-in Mail functionality. Generate a unique OTP, then craft an email template with the OTP included. Use Laravel's Mail facade to send the email with the OTP securely to the user's email address.

Let's Setup Mail Configuration

For sending the mail we need to add the mail configuration details like driver, host, port, username, password, encryption in the .env file.



Let's Create the Controller

Lets create the controller using the following artisan command,
php artisan make:controller OtpController

For detailed explanation about creating the controller Creating Controller . In the controller we will create the random 4 digits number using the rand() function in php and then send the generated random 4 digits in the users mail ID.



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class OtpController extends Controller
    public function sendOtp(Request $request)
        $otp = rand(1000, 9999);

        Mail::send('emails.otp', ['otp' => $otp], function ($message) use ($request) {
            $message->to($request->input('email'))->subject('Your OTP');

        return response()->json(['message' => 'OTP sent successfully']);

Let's Create Blade File

Lets create the layout of the mail in which the OTP will be sent. Creating blade files using artisan command

resources/views/emails /otp.blade.php

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sending OTP in Email Using Laravel 10 - Techsolutionstuff</title>
    <p>Dear User,</p>
    <p>Your One-Time Password (OTP) is: <strong>{{ $otp }}</strong></p>

    <p>Please use this OTP to complete your authentication process.</p>

    <p>Thank you,</p>
    <p>Your Application Team</p>

Let's Create Routes


use App\Http\Controllers\OtpController;

Route::post('/send-otp', [OtpController::class, 'sendOtp']);

Let's Run the Project

Lets run the project using the following artisan command below , and then click the url in the browser

php artisan serve

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.


ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides