"""
اختبار إرسال إيميل باستخدام Gmail SMTP
لاستخدام Gmail، يجب:
1. تفعيل 2FA على حساب Gmail
2. إنشاء App Password من: https://myaccount.google.com/apppasswords
"""
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Gmail SMTP Configuration
SMTP_HOST = "smtp.gmail.com"
SMTP_PORT = 587  # TLS
# SMTP_PORT = 465  # SSL

# استخدم App Password وليس كلمة المرور العادية
SMTP_USERNAME = "your_gmail@gmail.com"  # غيّر هذا
SMTP_PASSWORD = "xxxx xxxx xxxx xxxx"   # App Password من Google

RECIPIENT_EMAIL = "ashraffarid@gmail.com"

def send_test_email():
    print(f"Testing Gmail SMTP...")
    print(f"Host: {SMTP_HOST}:{SMTP_PORT}")
    print(f"From: {SMTP_USERNAME}")
    print(f"To: {RECIPIENT_EMAIL}")
    
    try:
        msg = MIMEMultipart()
        msg['From'] = f"SmartLife <{SMTP_USERNAME}>"
        msg['To'] = RECIPIENT_EMAIL
        msg['Subject'] = "SmartLife - Gmail SMTP Test"
        
        body = """
        <html>
        <body dir="rtl" style="font-family: Arial, sans-serif;">
            <h2>🎉 تم بنجاح!</h2>
            <p>هذا بريد إلكتروني تجريبي من Gmail SMTP.</p>
            <p>إعدادات البريد الإلكتروني تعمل بشكل صحيح.</p>
            <hr>
            <p style="color: #666; font-size: 12px;">SmartLife Monitoring System</p>
        </body>
        </html>
        """
        msg.attach(MIMEText(body, 'html', 'utf-8'))

        # TLS Connection (Port 587)
        server = smtplib.SMTP(SMTP_HOST, SMTP_PORT, timeout=30)
        server.starttls()
        
        # أو SSL Connection (Port 465):
        # server = smtplib.SMTP_SSL(SMTP_HOST, 465, timeout=30)
        
        print("Logging in...")
        server.login(SMTP_USERNAME, SMTP_PASSWORD)
        print("Sending message...")
        server.send_message(msg)
        server.quit()
        print("✓ Email sent successfully via Gmail!")
        
    except smtplib.SMTPAuthenticationError as e:
        print(f"❌ Authentication failed: {e}")
        print("تأكد من استخدام App Password وليس كلمة المرور العادية")
    except Exception as e:
        print(f"❌ Error: {e}")

if __name__ == "__main__":
    send_test_email()
