Email OTP Verification - Python Flask
app.py
#Email Varification Using OTP in Flask
from flask import Flask,render_template,request
from flask_mail import Mail,Message
from random import *
app=Flask(__name__, template_folder= "templates")
mail=Mail(app)
app.config["MAIL_SERVER"]='smtp.gmail.com'
app.config["MAIL_PORT"]=465
app.config["MAIL_USERNAME"]='******@gmail.com'
app.config['MAIL_PASSWORD']='************' #you have to give your password of gmail account
app.config['MAIL_USE_TLS']=False
app.config['MAIL_USE_SSL']=True
mail=Mail(app)
otp=randint(000000,999999)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/verify',methods=["POST"])
def verify():
email=request.form['email']
msg=Message(subject='OTP',sender='kdina715@gmail.com',recipients=[email])
msg.body=str(otp)
mail.send(msg)
return render_template('verify.html')
@app.route('/validate',methods=['POST'])
def validate():
user_otp=request.form['otp']
if otp==int(user_otp):
return "<h3>Email verification succesfull</h3>"
return "<h3>Please Try Again</h3>"
if __name__ == '__main__':
app.run(debug=True)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<form action="/verify" method="post">
Email:<input type="email" name="email">
<input type="submit" value="Submit">
</form>
</body>
</html>
verify.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OTP VERIFY</title>
</head>
<body>
<form action="/validate" method="post">
<h1>One time password has been sent to the mail id. please check </h1>
<p>by <b>Dinakaran</b></p>
Enter OTP: <input type="text" name="otp">
<input type="submit" value="Submit">
</form>
</body>
</html>
But the OTP will be same for everyone and it will last forever
ReplyDeleteYou can create variable otp on function
Deletedef otp():
return randint(000000,999999)