import paramiko
import sys

HOST = "135.125.102.180"
USER = "ubuntu"
PASS = "BotPascal2026!"

PHP_TEST = """<?php
echo "--- IMAP TEST ---\\n";
\$server = '{imap.free.fr:993/imap/ssl}INBOX';
\$user = 'heures@free.fr';
\$pass = 'Google75014@9350';

echo "Connecting to \$server...\\n";
\$conn = imap_open(\$server, \$user, \$pass);

if (\$conn) {
    echo "✅ Success! Connected.\\n";
    \$check = imap_check(\$conn);
    echo "Messages: " . \$check->Nmsgs . "\\n";
    imap_close(\$conn);
} else {
    echo "❌ Failed: " . imap_last_error() . "\\n";
}
?>"""

def main():
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HOST, username=USER, password=PASS)
        
        # Write test file
        sftp = ssh.open_sftp()
        with sftp.file("/var/www/html/repondeur_mail_grok/scripts/test_imap_simple.php", "w") as f:
            f.write(PHP_TEST)
            
        # Execute
        print("Running test...")
        stdin, stdout, stderr = ssh.exec_command("php /var/www/html/repondeur_mail_grok/scripts/test_imap_simple.php")
        print(stdout.read().decode())
        print(stderr.read().decode())
        
        ssh.close()
    except Exception as e:
        print(e)

if __name__ == "__main__":
    main()
