#!/usr/bin/env python3 """ Send a test email through the EmailService to verify SMTP configuration. Run from the backend directory: cd backend && python ../scripts/test_email.py [recipient] Without SMTP credentials in backend/.env this runs in SIMULATION mode (the message is logged, nothing is delivered). With credentials set it performs a real send. Note: EMAIL_OVERRIDE_TO still force-routes the recipient. """ import sys import logging from pathlib import Path # Add backend to path (same pattern as ingest.py) sys.path.insert(0, str(Path(__file__).parent.parent / "backend")) logging.basicConfig( level=logging.INFO, format="%(asctime)s | %(levelname)-8s | %(message)s", ) from app.config import settings # noqa: E402 from app.services.email_service import get_email_service # noqa: E402 recipient = sys.argv[1] if len(sys.argv) > 1 else (settings.email_override_to or settings.smtp_user) print("-" * 62) print(f"SMTP host : {settings.smtp_host}:{settings.smtp_port}") print(f"SMTP user : {settings.smtp_user or '(empty)'}") print(f"Mode : {'REAL SEND' if settings.smtp_configured else 'SIMULATION - set SMTP_USER/SMTP_PASSWORD in backend/.env'}") print(f"Override to : {settings.email_override_to or '(none - real recipient is used)'}") print("-" * 62) result = get_email_service().send( to=recipient, subject="Negoptim AI - SMTP test", body=( "Hello,\n\n" "This is a test email from the Negoptim AI backend confirming the " "SMTP configuration works end-to-end.\n\n" "- Negoptim AI" ), ) if result.sent and result.simulated: print("[OK] Pipeline works - but SIMULATION only, no real email was delivered.") elif result.sent: print(f"[OK] Real email delivered to {result.to} - check the inbox (and Spam folder).") else: print(f"[FAIL] {result.error}") sys.exit(1)