setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $action = $_GET['action'] ?? 'status'; // TEST SEND WHITELIST $ALLOWED_TEST = ['yacineutt@gmail.com','ymahboub@weval-consulting.com','joecloud@proton.me']; switch($action) { case 'status': $pmta = @fsockopen("10.1.0.2", 25, $errno, $errstr, 3); $pmta_ok = (bool)$pmta; if($pmta) fclose($pmta); $sa = (int)$db->query("SELECT count(*) FROM admin.office_accounts WHERE status='active' AND admin_email LIKE '%@%'")->fetchColumn(); $st = (int)$db->query("SELECT count(*) FROM admin.office_accounts WHERE admin_email LIKE '%@%'")->fetchColumn(); $today = $db->query("SELECT count(*) as total, count(CASE WHEN status='sent' THEN 1 END) as ok, count(CASE WHEN status='failed' THEN 1 END) as fail FROM ethica.send_log WHERE sent_at::date = CURRENT_DATE")->fetch(PDO::FETCH_ASSOC); echo json_encode(["ok"=>true,"pmta_reachable"=>$pmta_ok,"pmta_host"=>"10.1.0.2:25","senders_active"=>$sa,"senders_total"=>$st,"capacity_per_day"=>min($sa,50)*50,"sent_today"=>(int)$today['total'],"sent_today_ok"=>(int)$today['ok'],"sent_today_fail"=>(int)$today['fail'],"sent_total"=>(int)$db->query("SELECT count(*) FROM ethica.send_log")->fetchColumn(),"consent_tokens"=>(int)$db->query("SELECT count(*) FROM ethica.consent_tokens")->fetchColumn(),"timestamp"=>date('c')], JSON_PRETTY_PRINT); break; case 'isp_distribution': $r = $db->query("SELECT count(CASE WHEN email ~* '@gmail\\.com$' THEN 1 END) as gmail, count(CASE WHEN email ~* '@(yahoo\\.|hotmail\\.|outlook\\.|live\\.|msn\\.)' THEN 1 END) as yahoo, count(CASE WHEN email ~* '@.*\\.onmicrosoft\\.com$' THEN 1 END) as microsoft, count(*) as total FROM ethica.medecins_real WHERE email IS NOT NULL AND email!='' AND status='active'")->fetch(PDO::FETCH_ASSOC); $g=(int)$r['gmail'];$y=(int)$r['yahoo'];$m=(int)$r['microsoft'];$t=(int)$r['total']; echo json_encode(["ok"=>true,"distribution"=>["gmail"=>$g,"yahoo"=>$y,"microsoft"=>$m,"other"=>$t-$g-$y-$m,"total"=>$t]], JSON_PRETTY_PRINT); break; case 'senders': $limit = min((int)($_GET['limit'] ?? 20), 50); $accs = $db->query("SELECT admin_email as email, status FROM admin.office_accounts WHERE status='active' AND admin_email LIKE '%@%' ORDER BY RANDOM() LIMIT $limit")->fetchAll(PDO::FETCH_ASSOC); foreach($accs as &$a){$last=$db->prepare("SELECT sent_at FROM ethica.send_log WHERE sender_account=? ORDER BY id DESC LIMIT 1");$last->execute([$a['email']]);$l=$last->fetch(PDO::FETCH_ASSOC);$a['last_used']=$l?substr($l['sent_at'],0,19):null;$a['status']='ok';} echo json_encode(["ok"=>true,"senders"=>$accs], JSON_PRETTY_PRINT); break; case 'send_log': $limit = min((int)($_GET['limit'] ?? 30), 100); $logs = $db->query("SELECT email,sender_account,status,isp,domain,sent_at FROM ethica.send_log ORDER BY id DESC LIMIT $limit")->fetchAll(PDO::FETCH_ASSOC); echo json_encode(["ok"=>true,"logs"=>$logs], JSON_PRETTY_PRINT); break; case 'campaigns': $camps = $db->query("SELECT id,name,status,target_pays,target_count,sent_count,open_count,click_count,bounce_count,brand,created_at FROM ethica.campaigns ORDER BY id DESC LIMIT 10")->fetchAll(PDO::FETCH_ASSOC); echo json_encode(["ok"=>true,"campaigns"=>$camps], JSON_PRETTY_PRINT); break; case 'test_send': $email = $_GET['email'] ?? ''; if(!in_array($email, $ALLOWED_TEST)){echo json_encode(["ok"=>false,"error"=>"Test restricted to: ".implode(', ',$ALLOWED_TEST)]);break;} $template = $_GET['template'] ?? 'consent'; $tpl = $db->query("SELECT html_body,subject_line FROM ethica.email_templates WHERE category='$template' LIMIT 1")->fetch(PDO::FETCH_ASSOC); if(!$tpl) $tpl = $db->query("SELECT html_body,subject_line FROM ethica.email_templates LIMIT 1")->fetch(PDO::FETCH_ASSOC); $sender = $db->query("SELECT admin_email FROM admin.office_accounts WHERE status='active' AND admin_email LIKE '%@%' ORDER BY RANDOM() LIMIT 1")->fetchColumn(); $html = str_replace(['{{PRENOM}}','{{NOM}}','{{SPECIALITE}}','{{CONSENT_URL}}','{{UNSUBSCRIBE_URL}}','{{PIXEL}}'],['Yacine','MAHBOUB','Consultant','https://consent.wevup.app/?token=TEST&action=optin','https://consent.wevup.app/?token=TEST&action=optout',''],$tpl['html_body']); $subject = str_replace(['{{PRENOM}}','{{NOM}}'],['Yacine','MAHBOUB'],$tpl['subject_line']); $sock = @fsockopen("10.1.0.2",25,$errno,$errstr,5); if(!$sock){echo json_encode(["ok"=>false,"error"=>"PMTA fail: $errstr"]);break;} fgets($sock);fputs($sock,"EHLO ethica.local\r\n");$r=fgets($sock);while(substr($r,3,1)=='-')$r=fgets($sock); fputs($sock,"MAIL FROM:<$sender>\r\n");$r=fgets($sock);if(substr($r,0,3)!='250'){fclose($sock);echo json_encode(["ok"=>false,"error"=>"FROM rejected"]);break;} fputs($sock,"RCPT TO:<$email>\r\n");$r=fgets($sock);if(substr($r,0,3)!='250'){fclose($sock);echo json_encode(["ok"=>false,"error"=>"RCPT rejected: $r"]);break;} fputs($sock,"DATA\r\n");fgets($sock); $msgId='<'.bin2hex(random_bytes(12)).'@'.explode('@',$sender)[1].'>'; $msg="From: Ethica Sante <$sender>\r\nTo: <$email>\r\nSubject: [TEST] $subject\r\nMessage-ID: $msgId\r\nDate: ".date('r')."\r\nMIME-Version: 1.0\r\nContent-Type: text/html; charset=utf-8\r\nX-MS-Exchange-Organization-SCL: -1\r\nList-Unsubscribe: \r\n\r\n$html\r\n.\r\n"; fputs($sock,$msg);$r=fgets($sock);fputs($sock,"QUIT\r\n");fclose($sock); $sent=substr($r,0,3)=='250'; $st=$db->prepare("INSERT INTO ethica.send_log (campaign_id,medecin_id,email,sender_account,status,tracking_id,isp,domain,sent_at,created_at) VALUES (0,0,?,?,?,?,'Test','test',NOW(),NOW())"); $st->execute([$email,$sender,$sent?'sent':'failed',bin2hex(random_bytes(8))]); echo json_encode(["ok"=>true,"sent"=>$sent,"sender"=>$sender,"subject"=>"[TEST] $subject","error"=>$sent?null:"PMTA: $r"], JSON_PRETTY_PRINT); break; default: echo json_encode(["ok"=>false,"actions"=>["status","isp_distribution","senders","send_log","campaigns","test_send"]]); }