35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
import smtplib
|
|
from email.mime.text import MIMEText
|
|
from email.mime.multipart import MIMEMultipart
|
|
import logging
|
|
from app.config.settings import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class EmailService:
|
|
|
|
@staticmethod
|
|
def send_otp(to_email: str, otp: str):
|
|
try:
|
|
msg = MIMEMultipart()
|
|
msg['From'] = settings.EMAIL_FROM
|
|
msg['To'] = to_email
|
|
msg['Subject'] = "Password Reset OTP"
|
|
|
|
body = f"Your OTP for password reset is: {otp}. It expires in 10 minutes."
|
|
msg.attach(MIMEText(body, 'plain'))
|
|
|
|
if settings.SMTP_SECURE:
|
|
server = smtplib.SMTP_SSL(settings.SMTP_HOST, settings.SMTP_PORT)
|
|
else:
|
|
server = smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT)
|
|
server.starttls()
|
|
|
|
server.login(settings.SMTP_USER, settings.SMTP_PASSWORD)
|
|
text = msg.as_string()
|
|
server.sendmail(settings.EMAIL_FROM, to_email, text)
|
|
server.quit()
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"Failed to send email: {e}")
|
|
return False |