A small-scale and simple OTP sending and verification system built with Python. Easily integrates into login or signup flows. Uses basic SMTP (Simple Mail Transfer Protocol) and FastAPI libraries.
- Generate 4-digit OTPs
- Send OTPs via Gmail SMTP
- Verify OTPs within configurable expiry
- Easy to integrate into hackathons or other projects
- Well-commented code for reference
- Variables easy to spot and modify according to needs
Clone the repo:
git clone <repo-url>
cd <repo-folder>Create a .env file:
SENDER_MAIL=your_email@gmail.com
SENDER_PASSWORD=your_app_password
OTP_EXPIRY=300Install dependencies:
pip install fastapi uvicorn python-dotenvStart FastAPI server:
uvicorn main:app --reloadOpen in your browser: http://127.0.0.1:8000/docs for Swagger UI.
Copy the following files to your project:
logic.py
blueprints.py
config.py
Import OTP functions:
from logic import get_OTP, verify_otpSend OTP:
get_OTP("user@example.com")Verify OTP:
verify_otp("user@example.com", "1234")- OTP is valid for
OTP_EXPIRYseconds - Uses Gmail SMTP (port 587, TLS)
- Always use Gmail App Password, not your regular Gmail password
- For production, consider database or Redis storage for OTPs
- Make sure all the libraries and their complementary files are installed.
- Used for syntactical structure.
- Used for commenting.
- Use of MIMEText for mail formatting.
- Used for README structuring.