146 lines
4.9 KiB
PHP
Executable File
146 lines
4.9 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* BRAIN AUTO TEST - Envoi automatique vers seeds + vérification
|
|
* Usage: php brain_auto_test.php [config_id]
|
|
*/
|
|
|
|
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// Fonction envoi PMTA
|
|
function sendViaPMTA($to, $from, $fromName, $subject, $bodyHtml, $headers = []) {
|
|
$socket = @fsockopen('127.0.0.1', 25, $errno, $errstr, 10);
|
|
if (!$socket) return ['success' => false, 'error' => "$errstr ($errno)"];
|
|
|
|
fgets($socket, 512);
|
|
|
|
foreach (["EHLO wevads.com\r\n", "MAIL FROM:<$from>\r\n", "RCPT TO:<$to>\r\n", "DATA\r\n"] as $cmd) {
|
|
fwrite($socket, $cmd);
|
|
fgets($socket, 512);
|
|
}
|
|
|
|
// Construire le message
|
|
$msg = "From: $fromName <$from>\r\n";
|
|
$msg .= "To: $to\r\n";
|
|
$msg .= "Subject: $subject\r\n";
|
|
$msg .= "MIME-Version: 1.0\r\n";
|
|
$msg .= "Content-Type: text/html; charset=UTF-8\r\n";
|
|
$msg .= "Date: " . date('r') . "\r\n";
|
|
|
|
// Headers personnalisés
|
|
foreach ($headers as $name => $value) {
|
|
if ($value) $msg .= "$name: $value\r\n";
|
|
}
|
|
|
|
$msg .= "\r\n$bodyHtml\r\n.\r\n";
|
|
|
|
fwrite($socket, $msg);
|
|
$response = fgets($socket, 512);
|
|
fwrite($socket, "QUIT\r\n");
|
|
fclose($socket);
|
|
|
|
return ['success' => strpos($response, '250') !== false, 'response' => trim($response)];
|
|
}
|
|
|
|
// Récupérer une config à tester
|
|
$configId = $argv[1] ?? null;
|
|
|
|
if ($configId) {
|
|
$config = $pdo->query("SELECT * FROM admin.brain_configs WHERE id = $configId")->fetch(PDO::FETCH_ASSOC);
|
|
} else {
|
|
// Prendre une config qui n'a pas été testée récemment
|
|
$config = $pdo->query("
|
|
SELECT * FROM admin.brain_configs
|
|
WHERE total_sent < 10 OR last_tested_at < NOW() - INTERVAL '1 hour'
|
|
ORDER BY total_sent ASC, RANDOM()
|
|
LIMIT 1
|
|
")->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
if (!$config) {
|
|
echo "❌ Aucune config à tester\n";
|
|
exit(1);
|
|
}
|
|
|
|
echo "🧪 TEST CONFIG #{$config['id']}: {$config['isp_target']} - {$config['send_method']}\n";
|
|
echo str_repeat("=", 60) . "\n";
|
|
|
|
// Récupérer les seeds actifs avec password
|
|
$seeds = $pdo->query("
|
|
SELECT email, isp FROM admin.brain_seeds
|
|
WHERE is_active = true AND password IS NOT NULL AND password != ''
|
|
LIMIT 5
|
|
")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (empty($seeds)) {
|
|
echo "⚠️ Aucun seed avec password disponible\n";
|
|
// Utiliser quand même les seeds sans password pour l'envoi
|
|
$seeds = $pdo->query("SELECT email, isp FROM admin.brain_seeds WHERE is_active = true LIMIT 3")->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
echo "📧 Envoi vers " . count($seeds) . " seeds...\n\n";
|
|
|
|
// Préparer le contenu
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$testId = uniqid('brain_');
|
|
|
|
$fromEmail = $config['from_email'] ?: 'test@wevads.com';
|
|
$fromName = $config['from_name'] ?: 'WEVADS Test';
|
|
$subject = $config['subject_template'] ?: "Test {$config['isp_target']} - $timestamp";
|
|
$subject = str_replace(['{timestamp}', '{date}'], [$timestamp, date('Y-m-d')], $subject);
|
|
|
|
$body = $config['body_template'] ?: "
|
|
<html>
|
|
<body style='font-family: Arial; padding: 20px; background: #f5f5f5;'>
|
|
<div style='max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px;'>
|
|
<h2>🧪 Brain Engine Test</h2>
|
|
<p><strong>Config:</strong> #{$config['id']}</p>
|
|
<p><strong>ISP Target:</strong> {$config['isp_target']}</p>
|
|
<p><strong>Method:</strong> {$config['send_method']}</p>
|
|
<p><strong>Test ID:</strong> $testId</p>
|
|
<p><strong>Time:</strong> $timestamp</p>
|
|
<hr>
|
|
<p style='color: #666; font-size: 12px;'>This is an automated deliverability test.</p>
|
|
</div>
|
|
</body>
|
|
</html>";
|
|
|
|
// Headers personnalisés
|
|
$customHeaders = [];
|
|
if (!empty($config['x_mailer'])) $customHeaders['X-Mailer'] = $config['x_mailer'];
|
|
if (!empty($config['list_unsubscribe'])) $customHeaders['List-Unsubscribe'] = $config['list_unsubscribe'];
|
|
|
|
$sent = 0;
|
|
$failed = 0;
|
|
|
|
foreach ($seeds as $seed) {
|
|
echo " → {$seed['email']} ({$seed['isp']})... ";
|
|
|
|
$result = sendViaPMTA($seed['email'], $fromEmail, $fromName, $subject, $body, $customHeaders);
|
|
|
|
if ($result['success']) {
|
|
echo "✅\n";
|
|
$sent++;
|
|
|
|
// Log le test
|
|
$pdo->prepare("
|
|
INSERT INTO admin.brain_test_log (config_id, seed_email, seed_isp, test_id, sent_at, status)
|
|
VALUES (?, ?, ?, ?, NOW(), 'sent')
|
|
")->execute([$config['id'], $seed['email'], $seed['isp'], $testId]);
|
|
|
|
} else {
|
|
echo "❌ {$result['response']}\n";
|
|
$failed++;
|
|
}
|
|
|
|
usleep(300000); // 300ms entre chaque
|
|
}
|
|
|
|
// Mettre à jour la config
|
|
$pdo->exec("UPDATE admin.brain_configs SET last_tested_at = NOW() WHERE id = {$config['id']}");
|
|
|
|
echo "\n" . str_repeat("=", 60) . "\n";
|
|
echo "📊 Résultat: $sent envoyés, $failed échoués\n";
|
|
echo "\n⏳ Vérifie les boîtes email et marque INBOX/SPAM dans le dashboard!\n";
|
|
echo " → http://89.167.40.150:5821/brain-dashboard.php\n";
|