import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

print('Testing direct SMTP connection...')
try:
    server = smtplib.SMTP_SSL('mail.ai4ksa.com', 465, timeout=30)
    print('Connected to SMTP server')
    server.login('ithelpdesk@ai4ksa.com', 'ithelpdesk@123')
    print('Logged in successfully')
    
    msg = MIMEMultipart()
    msg['From'] = 'SmartLife <ithelpdesk@ai4ksa.com>'
    msg['To'] = 'ashraffarid@gmail.com'
    msg['Subject'] = 'Test Alert - Room3 Temperature'
    
    body = '''
    <html>
    <body dir="rtl" style="font-family: Arial, sans-serif;">
        <h2 style="color: #f59e0b;">⚠️ تنبيه SmartLife</h2>
        <p><strong>الجهاز:</strong> Room3</p>
        <p><strong>نوع القراءة:</strong> الحرارة</p>
        <p><strong>القيمة الحالية:</strong> 17°C</p>
        <p><strong>الحد:</strong> أكبر من 10°C</p>
        <p><strong>الشدة:</strong> تحذير</p>
    </body>
    </html>
    '''
    msg.attach(MIMEText(body, 'html', 'utf-8'))
    
    server.send_message(msg)
    server.quit()
    print('✓ Alert email sent successfully!')
except Exception as e:
    print(f'✗ Error: {e}')
