OTP (One-Time Password) Verification is like getting a secret code on your phone to make sure it’s really you doing something important online, like paying for something or resetting a password. You’ve probably seen it when you get a text with a code to type in. The code is unique and can only be used once.
To make an OTP verification app with Python, we follow these steps:
- Generate a random 6-digit number.
- Save the number.
- Write a program to send emails.
- Use the OTP as the message.
- Ask the user for their email and the OTP they received.
That’s how you can create an OTP verification app using Python.
Let’s get started with the coding
First, we will generate a random number and store it in a variable which I will be using while sending emails to the users:
digits="**********" // you can add your number here
OTP=""
for i in range(6):
OTP+=digits[math.floor(random.random()*10)]
otp = OTP + " is your OTP"
msg= otp
Before we continue, you’ll need a special password for your Gmail account to send emails from your Python program. This password is called an “app password.” You can get one by following these steps. Once you have your app password, copy it and paste it into the code below to send emails for OTP verification using Python.
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login("Your Gmail Account", "You app password")
emailid = input("Enter your email: ")
s.sendmail('&&&&&&&&&&&',emailid,msg)
a = input("Enter Your OTP >>: ")
if a == OTP:
print("Verified")
else:
print("Please Check your OTP again")
Whole code so that you can see the output on your own:
import math
import random
import smtplib
digits = "0123456789"
OTP = ""
for i in range(6):
OTP += digits[math.floor(random.random() * 10)]
otp = OTP + " is your OTP"
msg = otp
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login("Your Gmail Account", "Your app password")
emailid = input("Enter your email: ")
s.sendmail('Your Email Address', emailid, msg.encode('utf-8'))
a = input("Enter Your OTP >>: ")
if a == OTP:
print("Verified")
else:
print("Please Check your OTP again")
s.quit()
Output will come like this:
Enter your email:
Enter your OTP:
Once you run this code you enter an email where you want to send an OTP and then enter the OTP that you have received in the email you will get a prompt to Verified if the OTP is correct.
In conclusion, creating an OTP verification system using Python can greatly enhance the security of your applications by adding an extra layer of verification. The source code provided in this guide demonstrates how to generate a random OTP, send it to a user via email, and verify it when the user enters it back. This system can be used in various scenarios where user verification is required, such as during account registration, password reset, or online transactions.
Implementing this OTP verification system requires understanding the basics of Python programming, including string manipulation, random number generation, and working with email libraries like smtplib. By following the steps outlined in this guide and customizing the code to fit your specific requirements, you can create a robust OTP verification system for your applications.
Overall, this project showcases the practical application of Python in enhancing the security and user experience of web and mobile applications through the implementation of an OTP verification system.





Leave a Reply