From 277cd7306aac48cb63321c491bfa464fbf7c6423 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sun, 30 Nov 2025 07:07:09 -0500 Subject: [PATCH] Auto-detect sender email domain from request host or system FQDN When SMTP_FROM_EMAIL is not set, automatically derive the sender domain from: 1. Flask request.host (if not localhost/127.0.0.1) 2. System FQDN hostname (socket.getfqdn()) 3. SMTP_USER or fallback to noreply@opencompletion.local This allows the app to use the correct sender domain (e.g., noreply@ai.foxhop.net) when deployed on different hosts, ensuring proper email relay through mx servers. --- auth.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/auth.py b/auth.py index dd0d730..a0def5c 100644 --- a/auth.py +++ b/auth.py @@ -3,6 +3,7 @@ import os import random import smtplib +import socket from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from datetime import datetime @@ -28,14 +29,40 @@ def send_otp_email(email, otp_code): - SMTP_PORT: SMTP server port (e.g., 587) - SMTP_USER: SMTP username/email - SMTP_PASSWORD: SMTP password or app-specific password - - SMTP_FROM_EMAIL: Email address to send from + - SMTP_FROM_EMAIL: Email address to send from (auto-detected if not set) - SMTP_FROM_NAME: Display name for sender """ smtp_host = os.environ.get('SMTP_HOST') smtp_port = int(os.environ.get('SMTP_PORT', '587')) if smtp_host else 587 smtp_user = os.environ.get('SMTP_USER') smtp_password = os.environ.get('SMTP_PASSWORD') - from_email = os.environ.get('SMTP_FROM_EMAIL', smtp_user or 'noreply@opencompletion.local') + + # Auto-detect sender email domain from request or hostname + def get_default_from_email(): + # Try to get domain from Flask request context + try: + host = request.host + # Skip localhost/127.0.0.1 + if host and not host.startswith('localhost') and not host.startswith('127.0.0.1'): + # Remove port if present + domain = host.split(':')[0] + return f'noreply@{domain}' + except RuntimeError: + # No request context available + pass + + # Fall back to system hostname + try: + hostname = socket.getfqdn() + if hostname and hostname != 'localhost': + return f'noreply@{hostname}' + except Exception: + pass + + # Final fallback + return smtp_user or 'noreply@opencompletion.local' + + from_email = os.environ.get('SMTP_FROM_EMAIL', get_default_from_email()) from_name = os.environ.get('SMTP_FROM_NAME', 'OpenCompletion') # Create message