OTP Generation and validation to mail using python with GUI
Hello Everyone…
Today, I’ll demonstrate how to write a Python program with a Graphical User Interface (GUI) to send an OTP to a Gmail account. Let’s start with an overview diagram of the system.
First one is initial screen. What we will do on it?
- We have welcome screen with one user field called mail_id.
- Here user will enter the mail id.
- It also have one button called proceed.
- when the users click proceed, the control goes to next part which has responsibility of send otp.
Second one is send OTP. what we will do on it?
5. We have to collect email id from previous program what user enters it.
6. Generate otp.
7. Storing that otp on one notepad file.(Why I will tell later..)
8. Send otp to respective mail.
9. That’s Solve call the next program Get otp screen and validate.
Third one is Get Otp and Validate
10. We have second screen with one field Enter Your OTP. Here user enters the otp.
11. We have one button called validate. When the user clicks the validate, it will validate the otp.
Wait we have to verify it. So first we know what is sented otp. So for that reference only I stored in notepad.
12. So here things become complex by setting some condition.
First condition is Users are limited to enter only 3 times. After that it will come like attempts are over.
Second condition is Users are limited to enter within 2 minutes of time. After that time although, users enter correct otp, it will comes like session time out.
So these are real time scenarios, I was exprienced in netbanking. So I added that.
Another thing is creating second notepad file named status and use of this, I will tell later in program.
- Ok, now dive onto progam….. First one is initial screen. The code for that is
This is a simple program, so I won’t go into detail about each line. If anything is unclear, you can search “Tkinter beginner” on Google — there are tons of blogs and tutorials available.
However, I want to explain lines 9 and 10, as they play a major role in calling another program. The command for executing a Python script in the CMD window is
python filename.py arg1 arg2 …..
In our case python sendmail.py sendermailid(sender mail id is stored in variable cmd at line 8. So command be like)
python sendmail.py cmd.
In line 9, I construct the command and store it in a string variable called temp
.
In 10th line I Call the another program by using os.system(temp). Now the output of first screen be like..
2. Next we go to second program called sendmail.py
Collect the user entered mail id from previous program using line 5. sys.argv[1] means first argument. Our first argument is email id.
Next sending otp to your gmail. First generate OTP by using math library which are from line 5 to line 9. Then store it on text file which is from line 10 to line 13. Next we have to send it. After sending it we call next program
You have to generate app password for sender account. And that account may be two step-verification enabled. If you want how to create app password please refer this youtube link. Otherwise you may search it.
3. Next is second.py and verify.py. It both works combinely. Because when each time user enters otp and click validate it go to verify.py. At that place entered otp is checked with otp stored in notepad.
If the otp matched, then we will write “success” in status.txt file, otherwise write “failure” in status.txt file.
These two programs are take more time to write the logic. But it is simple if you understand the logic. So instead of images for better understanding , I attached the video of output with all possibilities. So please watch it and refer the code.
That’s solve.. I did not explain the code line by line for previous two program. Because it takes much time and too long for blogging. It is easy to understand once you try it and do little modification.. Please Google for syntax and it’s uses.
If you have any doubt about coding put in comments. I will try to explain..
Thank you
KeyNotes: 4 python file and 2 text file
Edited on 04–05–2022
Please Find the Github Repository Link.