Add SMTP support for OTK emails
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
spsaefan
2026-06-27 14:17:21 +02:00
parent 4b6ad1ec3d
commit 1019cedb97
3 changed files with 54 additions and 12 deletions
@@ -0,0 +1,29 @@
import smtplib
from email.message import EmailMessage
def send_email(
recipient: str,
subject: str,
body: str,
smtp_server: str = "localhost",
smtp_port: int = 25,
sender: str = "no-reply@smartguide.local",
username: str | None = None,
password: str | None = None,
use_tls: bool = False,
):
msg = EmailMessage()
msg["From"] = sender
msg["To"] = recipient
msg["Subject"] = subject
msg.set_content(body)
with smtplib.SMTP(smtp_server, smtp_port) as server:
if use_tls:
server.starttls()
if username and password:
server.login(username, password)
server.send_message(msg)
+17 -12
View File
@@ -27,7 +27,8 @@ from inse.auth.model import (
)
from inse.config import get_settings
from inse.database import get_session
from sqlmodel import Session, func, select
from sqlmodel import Session, select, func
from inse.auth.email_service import send_email
ID_TOKEN_EXPIRATION_MINUTES = 60 * 24 * 30 # 30 days
REFRESH_TOKEN_EXPIRATION_MINUTES = 15 # 15 minutes
@@ -210,19 +211,23 @@ def request_otk(session: Session, email: str, password: str):
session.commit()
if settings.environment == "prod":
logger.info(
"Saved OTK for account %s with expiration at %s",
account.id,
expires_at,
if settings.send_otk_email:
send_email(
recipient=account.email,
subject="Your SmartGUIDE login code",
body=f"Your login code is: {otk_code}",
smtp_server=settings.smtp_server,
smtp_port=settings.smtp_port,
sender=settings.smtp_sender,
username=settings.smtp_username,
password=settings.smtp_password,
use_tls=settings.smtp_use_tls,
)
logger.info("Sent OTK email for account %s", account.id)
elif settings.environment == "dev":
logger.info("OTK code for account %s: %s", account.id, otk_code)
else:
logger.info(
"OTK code for account %s: %s, expires at %s",
account.id,
otk_code,
expires_at,
)
logger.info("OTK generated for account %s", account.id)
return {"status": "ok", "account_id": str(account.id)}
+8
View File
@@ -68,6 +68,14 @@ class Settings(BaseSettings):
token_hmac_key: Optional[str] = None
skip_otk_validation: bool = True
# SMTP / email config
send_otk_email: bool = False
smtp_server: str = "localhost"
smtp_port: int = 25
smtp_sender: str = "no-reply@smartguide.local"
smtp_username: Optional[str] = None
smtp_password: Optional[str] = None
smtp_use_tls: bool = False
settings = Settings()