diff --git a/backend/auth.py b/backend/auth.py index 52945b0..9bf4924 100644 --- a/backend/auth.py +++ b/backend/auth.py @@ -8,10 +8,10 @@ from fastapi import Depends from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from email_service import ( create_verification, + send_verification_email, send_password_reset_email, create_password_reset, hash_token, - send_verification_email, ) import secrets diff --git a/backend/email_service.py b/backend/email_service.py index afe0d94..431bced 100644 --- a/backend/email_service.py +++ b/backend/email_service.py @@ -222,6 +222,63 @@ def verify_email(token: str): finally: conn.close() +def send_verification_email(email: str, token: str): + smtp_host = os.getenv("SMTP_HOST") + smtp_port = int(os.getenv("SMTP_PORT", "587")) + smtp_username = os.getenv("SMTP_USERNAME") + smtp_password = os.getenv("SMTP_PASSWORD") + smtp_from = os.getenv("SMTP_FROM") + + if not all([ + smtp_host, + smtp_username, + smtp_password, + smtp_from, + ]): + raise RuntimeError("SMTP configuration is incomplete") + + verification_url = ( + f"https://ben.de-roo.org/api/verify-email?token={token}" + ) + + message = EmailMessage() + + message["From"] = smtp_from + message["To"] = email + message["Subject"] = "Verify your Calendar email address" + + message.set_content( + f"""Hello, + +You requested to use this email address for your Calendar account. + +Verify your email address using this link: + +{verification_url} + +This link expires after 24 hours. + +If you did not request this, you can ignore this email. +""" + ) + + print(f"Sending verification email to {email}") + + with smtplib.SMTP(smtp_host, smtp_port) as smtp: + print("SMTP connected") + + smtp.starttls() + print("TLS started") + + smtp.login( + smtp_username, + smtp_password, + ) + print("SMTP login successful") + + smtp.send_message(message) + print("Verification email sent") + def send_password_reset_email(email: str, token: str): smtp_host = os.getenv("SMTP_HOST") smtp_port = int(os.getenv("SMTP_PORT", "587"))