bookmyservice-ums / app /utils /email_utils.py
MukeshKapoor25's picture
Refactor OTP handling to continue with email fallback on SMS failure; add timeout parameter to email OTP function and improve SMS client initialization
9e0fc4a
raw
history blame contribute delete
609 Bytes
import smtplib
from email.mime.text import MIMEText
from app.core.config import settings
async def send_email_otp(to_email: str, otp: str, timeout: float = 10.0):
msg = MIMEText(f"Your OTP is {otp}. It is valid for 5 minutes.")
msg["Subject"] = "Your One-Time Password"
msg["From"] = settings.SMTP_FROM
msg["To"] = to_email
server = smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT, timeout=timeout)
server.connect(settings.SMTP_HOST, settings.SMTP_PORT)
server.starttls()
server.login(settings.SMTP_USER, settings.SMTP_PASS)
server.send_message(msg)
server.quit()