#!/usr/bin/env python3 """ Test Email Configuration Script for AIMHSA """ import os import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from dotenv import load_dotenv def test_email_config(): """Test the email configuration""" print("=" * 60) print("๐Ÿ“ง AIMHSA - Email Configuration Test") print("=" * 60) # Load environment variables load_dotenv() # Get email configuration smtp_server = os.getenv("SMTP_SERVER", "smtp.gmail.com") smtp_port = int(os.getenv("SMTP_PORT", "587")) smtp_username = os.getenv("SMTP_USERNAME", "") smtp_password = os.getenv("SMTP_PASSWORD", "") from_email = os.getenv("FROM_EMAIL", "noreply@aimhsa.rw") print(f"๐Ÿ“ง SMTP Server: {smtp_server}:{smtp_port}") print(f"๐Ÿ‘ค Username: {smtp_username}") print(f"๐Ÿ“จ From Email: {from_email}") print(f"๐Ÿ”‘ Password: {'*' * len(smtp_password) if smtp_password else 'Not set'}") print() # Check if configuration is complete if not smtp_username or not smtp_password: print("โŒ Email configuration is incomplete!") print("๐Ÿ“‹ Missing:") if not smtp_username: print(" - SMTP_USERNAME") if not smtp_password: print(" - SMTP_PASSWORD") print("\n๐Ÿ’ก Run 'python setup_email.py' to configure email settings.") return False # Test email sending print("๐Ÿงช Testing email configuration...") try: # Create test message msg = MIMEMultipart('alternative') msg['Subject'] = "AIMHSA - Email Configuration Test" msg['From'] = from_email msg['To'] = smtp_username # Send test email to yourself # Create test content html_content = """

AIMHSA

Mental Health Companion for Rwanda

Email Configuration Test

โœ… Your email configuration is working correctly!

This is a test email to verify that the AIMHSA password reset system can send emails.

ยฉ 2024 AIMHSA - Mental Health Companion for Rwanda

""" text_content = """ AIMHSA - Email Configuration Test โœ… Your email configuration is working correctly! This is a test email to verify that the AIMHSA password reset system can send emails. ยฉ 2024 AIMHSA - Mental Health Companion for Rwanda """ # Attach parts part1 = MIMEText(text_content, 'plain') part2 = MIMEText(html_content, 'html') msg.attach(part1) msg.attach(part2) # Send email print("๐Ÿ“ค Connecting to SMTP server...") server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() print("๐Ÿ” Authenticating...") server.login(smtp_username, smtp_password) print("๐Ÿ“ง Sending test email...") server.send_message(msg) server.quit() print("โœ… Email configuration test successful!") print(f"๐Ÿ“จ Test email sent to: {smtp_username}") print("\n๐ŸŽ‰ Your forgot password system is now ready to send real emails!") return True except smtplib.SMTPAuthenticationError: print("โŒ Authentication failed!") print("๐Ÿ’ก Check your username and password.") print(" For Gmail: Make sure you're using an App Password, not your regular password.") return False except smtplib.SMTPConnectError: print("โŒ Connection failed!") print("๐Ÿ’ก Check your SMTP server and port settings.") return False except Exception as e: print(f"โŒ Email test failed: {e}") return False def check_env_file(): """Check if .env file exists and show its contents (without passwords)""" if not os.path.exists('.env'): print("โŒ .env file not found!") print("๐Ÿ’ก Run 'python setup_email.py' to create email configuration.") return False print("๐Ÿ“„ .env file found. Configuration:") print("-" * 40) with open('.env', 'r') as f: for line in f: line = line.strip() if line and not line.startswith('#'): if 'PASSWORD' in line: # Hide password key, value = line.split('=', 1) print(f"{key}=***") else: print(line) print("-" * 40) return True if __name__ == "__main__": print("Choose test option:") print("1. Check .env file") print("2. Test email configuration") print("3. Both") choice = input("Enter choice (1-3): ").strip() if choice == "1": check_env_file() elif choice == "2": test_email_config() elif choice == "3": if check_env_file(): print() test_email_config() else: print("Invalid choice. Running both tests...") if check_env_file(): print() test_email_config()