├── README.md └── python_otp ├── __init__.py └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # python_otp_sender 2 | This is a python mudule by which one can easily send otp to any email. 3 | This is very helpful for web development purpose 4 | 5 | ## Prerequisites : -- 6 | 1. install python module name as python-decouple 7 | ```bash 8 | pip install python-decouple 9 | ``` 10 | 2. Create a gmail account for testing purpose and enable [less secure app](https://myaccount.google.com/lesssecureapps?pli=1&rapt=AEjHL4NKHRfdHXxmxYgD6LATUhs6N6ww0sBX4aegeZFXtLmr_eZnEznzem-MKdS-PWBon8Nxo0ocZ3UZYJsm5aqb9VhvKlxayg) of your testing google account. 11 | 3. Make `.env` file in your main directory and add these two lines in it. 12 | ``` 13 | EMAIL_ADDRESS=example@gmail.com 14 | PASSWORD=super_secret_password 15 | ``` 16 | 4. Keep the `python_otp` folder in your main directory. 17 | 18 | ## Demo code : - 19 | ```python 20 | from python_otp import otp_for 21 | 22 | email = "singh.rohitsingh2k@gmail.com" 23 | otp = otp_for(email) 24 | n = input("Enter recieved otp : ") 25 | 26 | if n == otp: 27 | print("OTP IS CORRECT") 28 | else: 29 | print("INCORRECT OTP") 30 | ``` 31 | ## Additionals : - 32 | * If you want to customize your email markup of OTP which is to send, then just go to `python_otp` directory and edit `index.html` file. 33 | * Don't remove `{% OTP %}` from `index.html`. Because your otp will be displayed in place of `{% OTP %}`. 34 | ## Note : - 35 | 36 | The above module takes email where you want to send email as a string & it will return otp value which is sended to reciever's email. 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /python_otp/__init__.py: -------------------------------------------------------------------------------- 1 | # Prerequisits : - 2 | # 1) pip install python-decouple 3 | # 2) create .env file into the directory where your main file exist 4 | # 3) Add these two lines into .env file: - 5 | # 6 | # EMAIL_ADDERSS="your email address" 7 | # PASSWORD="your email password" 8 | # 9 | # 4) add index.html and edit it as you like it just replace otp section with {% OTP %} 10 | 11 | """ 12 | Author : Rohit Singh 13 | Purpose : Made OTP sending easy. 14 | """ 15 | 16 | from email.message import EmailMessage 17 | from decouple import config 18 | import smtplib, ssl, random 19 | 20 | # read html file 21 | data = """ """ 22 | with open("./python_otp/index.html") as file: 23 | data = file.read() 24 | 25 | 26 | def otp_for(reciever_email): 27 | email = config('EMAIL_ADDRESS') 28 | email_password = config('PASSWORD') 29 | 30 | msg = EmailMessage() 31 | msg['Subject'] = 'OTP test' 32 | msg['From'] = email 33 | msg['To'] = reciever_email 34 | 35 | # generate otp 36 | otp = str(random.randint(1000,9999)) 37 | 38 | # adding otp to html 39 | page = data.replace('{% OTP %}',otp) 40 | 41 | msg.add_alternative(page, subtype='html') 42 | 43 | # creating secure ssl 44 | context = ssl.create_default_context() 45 | 46 | # creating server 47 | smtp = smtplib.SMTP('smtp.gmail.com', 587) 48 | smtp.ehlo() 49 | smtp.starttls(context=context) 50 | smtp.ehlo() 51 | smtp.login(email, email_password) 52 | smtp.send_message(msg) 53 | smtp.quit() 54 | 55 | return otp 56 | 57 | -------------------------------------------------------------------------------- /python_otp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |