Home > Mobile >  I want to send, Validate opt in Laravel on user email as I'm developing backend in Laravel
I want to send, Validate opt in Laravel on user email as I'm developing backend in Laravel

Time:09-20

This is my simple code but not getting an idea of how I send and validate OTP on user email actually, I am new to Laravel and developing the backend for a project I am now stuck on send OTP kindly help me out

public function sendOTP(Request $request){

        $otp = rand(10000,99999);
        
        $std = Student::where('email','=',$request->email)->update(['otp' => $otp]);
        
        if ($std == true) {
            return response([
                "code" => 200,
                "message" => "OTP send Successfully",
                "object" => $std
            ]);
        } else {
            return response([
                "code" => 400,
                "message" => "Email Not Exist"
            ]);
        }

My api.php route

Route::post('sendOTP',[StudentController::class,'sendOTP']);

CodePudding user response:

You need to follow the steps below:

Step 1: Create a template for your email (example: verify_mail.blade.php) with simple content <h1>Vefiry token is: {{$token}}</h1>, notice the $token it will be added and used in the next step

Step 2: Create new Mail in laravel (php artisan make:mail MailVerifyOtp) now you will see a file MailVerifyOtp.php in app/Mail.

class MailVerifyOtp extends Mailable
{
    use Queueable, SerializesModels;

    private  $token;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'))
            ->subject('Forget Password')
            ->view('verify_mail')
            ->with([
                'token' => $this->token,
            ]);
    }
}

Step 3: In controller

$token = // code generate token;
Mail::to($user->email)->send( new MailVerifyOtp($token));
 if (Mail::failures()){
     // logic false here
 }

Hope help u and happy coding !

CodePudding user response:

Function

public function sendOTP(Request $request){

    $otp = rand(10000,99999);
    
    $std = Student::where('email','=',$request->email)->update(['otp' => $otp]);
    
    if ($std == true) {
        // **At the top** use Illuminate\Support\Facades\Mail as Mail;
        Mail::raw('Your OTP: '.$otp, function ($message) use($otp) {
             $message->to("[email protected]")->subject("OTP Email");
        });

        return response([
            "code" => 200,
            "message" => "OTP send Successfully",
            "object" => $std
        ]);
    } else {
        return response([
            "code" => 400,
            "message" => "Email Not Exist"
        ]);
    }

Must you have to configure your .env file for email configuration enter image description here

  • Related