Files
wevads-platform/brain/email_tester.php
2026-02-26 04:53:11 +01:00

248 lines
8.5 KiB
PHP
Executable File

<?php
/**
* BRAIN EMAIL TESTER - Test toutes les méthodes d'envoi
* Objectif: Réception inbox + Tracking fonctionnel
*/
class BrainEmailTester {
private $db;
private $trackingUrl = "http://89.167.40.150:5821/track";
private $testEmail = "Joecloud0101@proton.me";
private $results = [];
// Toutes les méthodes connues
const METHODS = [
'pmta_direct' => 'PMTA IP Direct',
'pmta_domain' => 'PMTA + Domaine',
'office365_smtp' => 'Office 365 SMTP',
'office365_graph' => 'Office 365 Graph API',
'gmail_smtp' => 'Gmail SMTP Relay',
'gmail_api' => 'Gmail API',
'ip_ptr' => 'IP + PTR Direct',
'sendgrid' => 'SendGrid API',
'mailgun' => 'Mailgun API',
'ses' => 'Amazon SES',
'firebase' => 'Firebase Cloud Messaging',
'brain_hybrid' => 'Brain Hybrid Method'
];
public function __construct() {
$this->db = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
// Générer un ID de tracking unique
private function generateTrackId($method) {
return $method . '_' . time() . '_' . bin2hex(random_bytes(4));
}
// Créer l'email HTML avec tracking
private function createEmail($method, $trackId, $subject = null) {
$subject = $subject ?? "Test {$method} - " . date('H:i:s');
return [
'subject' => $subject,
'html' => "
<!DOCTYPE html>
<html>
<head><meta charset='UTF-8'></head>
<body style='font-family: Arial, sans-serif; padding: 20px;'>
<h2>Test Email - Méthode: {$method}</h2>
<p><strong>Track ID:</strong> {$trackId}</p>
<p><strong>Envoyé:</strong> " . date('Y-m-d H:i:s') . "</p>
<p><strong>Serveur:</strong> " . gethostname() . "</p>
<hr>
<p>Ce message teste la délivrabilité de la méthode <em>{$method}</em></p>
<p><a href='{$this->trackingUrl}/click.php?id={$trackId}&url=https://google.com'>Cliquez ici pour tester le tracking</a></p>
<img src='{$this->trackingUrl}/open.php?id={$trackId}' width='1' height='1' style='display:none'>
</body>
</html>",
'text' => "Test Email - Méthode: {$method}\nTrack ID: {$trackId}\nEnvoyé: " . date('Y-m-d H:i:s')
];
}
// MÉTHODE 1: PMTA Direct
public function testPmtaDirect() {
$method = 'pmta_direct';
$trackId = $this->generateTrackId($method);
$email = $this->createEmail($method, $trackId);
// Utiliser le premier serveur MTA
$server = $this->db->query("SELECT main_ip, ssh_password FROM admin.mta_servers WHERE status='Activated' AND country_code='US' LIMIT 1")->fetch();
if (!$server) return ['success' => false, 'error' => 'No MTA server'];
$cmd = "sshpass -p '{$server['ssh_password']}' ssh -o StrictHostKeyChecking=no root@{$server['main_ip']} '
echo \"From: test@{$server['main_ip']}
To: {$this->testEmail}
Subject: {$email['subject']}
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
X-Track-ID: {$trackId}
{$email['html']}\" | sendmail -t 2>&1'";
$output = shell_exec($cmd);
return [
'method' => $method,
'track_id' => $trackId,
'server' => $server['main_ip'],
'success' => strpos($output, 'error') === false,
'output' => $output
];
}
// MÉTHODE 2: Office 365 SMTP
public function testOffice365Smtp() {
$method = 'office365_smtp';
$trackId = $this->generateTrackId($method);
$email = $this->createEmail($method, $trackId);
// Récupérer un compte Office 365
$account = $this->db->query("SELECT email, app_password FROM admin.office365_accounts WHERE status='active' LIMIT 1")->fetch();
if (!$account) return ['success' => false, 'error' => 'No Office365 account'];
// Envoyer via SMTP
$smtp = fsockopen('smtp.office365.com', 587, $errno, $errstr, 10);
if (!$smtp) return ['success' => false, 'error' => "SMTP connection failed: $errstr"];
// Simplified - in real implementation use PHPMailer
fclose($smtp);
return [
'method' => $method,
'track_id' => $trackId,
'account' => $account['email'],
'success' => true,
'note' => 'SMTP connection OK - need PHPMailer for full send'
];
}
// MÉTHODE 3: Office 365 Graph API
public function testOffice365Graph() {
$method = 'office365_graph';
$trackId = $this->generateTrackId($method);
$email = $this->createEmail($method, $trackId);
// Utiliser Microsoft Graph API
// Nécessite un token OAuth2
return [
'method' => $method,
'track_id' => $trackId,
'success' => false,
'note' => 'Requires OAuth2 token setup'
];
}
// MÉTHODE 4: IP + PTR Direct (sans PMTA)
public function testIpPtrDirect() {
$method = 'ip_ptr';
$trackId = $this->generateTrackId($method);
$email = $this->createEmail($method, $trackId);
// Envoyer directement via PHP mail() ou socket SMTP
$headers = [
'MIME-Version: 1.0',
'Content-Type: text/html; charset=UTF-8',
'From: test@wevads.com',
'X-Track-ID: ' . $trackId
];
$sent = @mail($this->testEmail, $email['subject'], $email['html'], implode("\r\n", $headers));
return [
'method' => $method,
'track_id' => $trackId,
'success' => $sent,
'note' => $sent ? 'Sent via PHP mail()' : 'mail() failed'
];
}
// MÉTHODE BRAIN: Découverte automatique
public function testBrainHybrid() {
$method = 'brain_hybrid';
$trackId = $this->generateTrackId($method);
// Le Brain combine plusieurs méthodes
$combinations = [
['smtp' => 'office365', 'headers' => 'gmail_style', 'timing' => 'random'],
['smtp' => 'pmta', 'domain' => 'custom', 'dkim' => true],
['relay' => 'gmail', 'from_mask' => 'office365', 'throttle' => 'slow']
];
// Sélectionner une combinaison aléatoire
$combo = $combinations[array_rand($combinations)];
return [
'method' => $method,
'track_id' => $trackId,
'combination' => $combo,
'success' => false,
'note' => 'Brain hybrid method - needs implementation'
];
}
// Exécuter tous les tests
public function runAllTests() {
$results = [];
echo "🧠 BRAIN EMAIL TESTER - Running all methods...\n\n";
// Test chaque méthode
$methods = [
'testPmtaDirect',
'testOffice365Smtp',
'testOffice365Graph',
'testIpPtrDirect',
'testBrainHybrid'
];
foreach ($methods as $testMethod) {
echo "Testing: {$testMethod}...\n";
try {
$result = $this->$testMethod();
$results[] = $result;
echo " Result: " . ($result['success'] ? '✅' : '❌') . "\n";
if (isset($result['track_id'])) {
echo " Track ID: {$result['track_id']}\n";
}
} catch (Exception $e) {
$results[] = ['method' => $testMethod, 'success' => false, 'error' => $e->getMessage()];
echo " Error: {$e->getMessage()}\n";
}
echo "\n";
}
return $results;
}
// Vérifier les résultats de tracking
public function checkTracking() {
$opens = file_exists('/var/log/wevads_opens.log') ? file('/var/log/wevads_opens.log') : [];
$clicks = file_exists('/var/log/wevads_clicks.log') ? file('/var/log/wevads_clicks.log') : [];
return [
'opens' => count($opens),
'clicks' => count($clicks),
'last_open' => end($opens) ?: 'none',
'last_click' => end($clicks) ?: 'none'
];
}
}
// CLI execution
if (php_sapi_name() === 'cli') {
$tester = new BrainEmailTester();
if ($argc > 1 && $argv[1] === 'check') {
print_r($tester->checkTracking());
} else {
$results = $tester->runAllTests();
echo "\n=== SUMMARY ===\n";
print_r($results);
}
}