-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotp_sender.py
38 lines (29 loc) · 1.16 KB
/
otp_sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import base64
import smtplib
from email.mime.text import MIMEText
from pyotp import TOTP
import secrets
def send_otp_email(email, otp):
# SMTP settings for Gmail
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
SMTP_USERNAME = "[email protected]"
SMTP_PASSWORD = "wdvscwdctjggmtvo"
SENDER_NAME = "FruBay OTP" # Sender's name
SENDER_EMAIL = "[email protected]"
# Generate an OTP using pyotp
# random_secret_bytes = secrets.token_bytes(20)
# otp_secret = base64.b32encode(random_secret_bytes).decode("utf-8")
otp = TOTP(otp)
one_time_password = otp.now()
message = f"Dear Customer,\n\nYour One Time Password (OTP) for FruBay is {one_time_password}, and is valid for 10 minutes. Please do not share with anyone."
# Create the email content
msg = MIMEText(message)
msg['Subject'] = 'OTP Verification'
msg['From'] = f"{SENDER_NAME} <{SENDER_EMAIL}>"
msg['To'] = email
# Send the email using SMTP
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SMTP_USERNAME, SMTP_PASSWORD)
server.sendmail(SENDER_EMAIL, [email], msg.as_string())