How to Send OTP Using Email and Redis in Laravel 10?

25-Dec-2023

.

Admin

How to Send OTP Using Email and Redis in Laravel 10?

Hello Dev,

This tutorial will provide an example of how to send OTP using email and Redis in Laravel 10. This article will give you a simple example of how to generate OTP and send it to email in Laravel 10. it's simple example of how to send and receive email in Laravel 10. it's a simple example of how to send an email after registration in Laravel 10. follow the step for how to make email verification in Laravel 10.

One-time passwords (OTPs) offer an additional layer of security, commonly employed for user authentication. This article delves into the process of sending OTPs via email, utilizing Laravel’s email functionality, and harnessing Redis for efficient OTP storage and retrieval.

Required Conditions


Before we commence, please ensure that you have the following set up.

A Laravel application, with a minimum version of 5.8 or above, is required.

Redis installed and configured with your Laravel application.

An email service or SMTP server configured in Laravel.

Step 1: Set Up Laravel Project

If you have not created a Laravel project yet, please use the following command.

composer create-project laravel/laravel send-otp

cd send-otp

Step 2: Configure Mail and Redis

Update your .env file with the required configurations for mail and Redis.

MAIL_MAILER=smtp

MAIL_HOST=your mail host

MAIL_PORT=your mail port

MAIL_USERNAME=your mail username

MAIL_PASSWORD=your mail password

MAIL_ENCRYPTION=tls

REDIS_HOST=127.0.0.1

REDIS_PASSWORD=your redis password

REDIS_PORT=6379

Replace placeholders with your actual mail and Redis configurations.

Step 3: Create a Controller

Generate a controller to handle OTP-related logic.

php artisan make:controller OtpController

Update the controller with the following code.

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Mail;

use Illuminate\Support\Facades\Redis;

use App\Mail\OtpMail;

use Illuminate\Http\Request;

class OtpController extends Controller

{

/**

* write code for.

*

* @param \Illuminate\Http\Request

* @return \Illuminate\Http\Response

* @author <>

*/

public function sendOtp(Request $request)

{

$email = $request->input(‘email’);

$otp = rand(100000, 999999);

Redis::setex(“otp:$email”, 300, $otp);

Mail::to($email)->send(new OtpMail($otp));

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

}

}

This controller is responsible for generating a random 6-digit OTP, storing it in Redis with a 5-minute expiration, and subsequently sending it to the provided email address.

Step 4: Create a Mailable

Generate a Mailable for the OTP email.

php artisan make:mail OtpMail

Please update the generated Mailable (OtpMail.php located in the app/Mail directory) with the following code.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Contracts\Queue\ShouldQueue;

class OtpMail extends Mailable

{

use Queueable, SerializesModels;

public $otp;

/**

* Create a new message instance.

*

* @return void

*/

public function __construct($otp) {

$this->otp = $otp;

}

/**

* Build the message.

*

* @return $this

*/

public function build()

{

return $this->view('emails.otp');

}

}

Step 5: Create an Email View

Create an email view file at resources/views/emails/otp.blade.php and populate it with the following content.

<!DOCTYPE html>

<html>

<head>

<title>One-Time Password</title>

</head>

<body>

<h1>Your OTP is: {{ $otp }}</h1>

</body>

</html>

This view will display the OTP in the email.

Step 6: Update Routes

Update your web.php routes file to include a route for sending OTP.

use App\Http\Controllers\OtpController;

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

Step 7: Test the Implementation

Initiate your Laravel development server by running the following command.

php artisan serve

Send a POST request to http://127.0.0.1:8000/send-otp with an email parameter.

curl -X POST -d “email=test@gmail.com” http://127.0.0.1:8000/send-otp

Please check your email inbox for the OTP.

I hope it can help you...

#Laravel 10