File size: 5,924 Bytes
c024705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/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 = """
        <html>
        <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333;">
            <div style="max-width: 600px; margin: 0 auto; padding: 20px;">
                <div style="text-align: center; margin-bottom: 30px;">
                    <h1 style="color: #2c5aa0;">AIMHSA</h1>
                    <p style="color: #666;">Mental Health Companion for Rwanda</p>
                </div>
                
                <div style="background-color: #f8f9fa; padding: 30px; border-radius: 8px; margin-bottom: 20px;">
                    <h2 style="color: #2c5aa0; margin-top: 0;">Email Configuration Test</h2>
                    <p>βœ… Your email configuration is working correctly!</p>
                    <p>This is a test email to verify that the AIMHSA password reset system can send emails.</p>
                </div>
                
                <div style="text-align: center; color: #666; font-size: 12px;">
                    <p>Β© 2024 AIMHSA - Mental Health Companion for Rwanda</p>
                </div>
            </div>
        </body>
        </html>
        """
        
        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()