Files
html/api/opus5-check-sent-kaouther.php
2026-04-17 16:40:02 +02:00

94 lines
3.2 KiB
PHP

<?php
header('Content-Type: application/json');
$R = ['ts' => date('c')];
// Config from email-sources.json
$server = 'server105.web-hosting.com';
$port = 993;
$email = 'ymahboub@weval-consulting.com';
$pass = base64_decode('WUBAY2luZTE5ODVAQA==');
// Tenter d'accéder au dossier "Sent" (envoyés)
$folders_to_try = ['INBOX.Sent', 'Sent', 'INBOX.Sent Items', 'Sent Items', 'INBOX.Envoyés'];
$R['attempts'] = [];
foreach ($folders_to_try as $folder) {
$mb = "{" . $server . ":" . $port . "/imap/ssl/novalidate-cert}" . $folder;
$start = microtime(true);
$conn = @imap_open($mb, $email, $pass, 0, 1);
$ms = round((microtime(true)-$start)*1000);
if ($conn) {
$check = @imap_check($conn);
$total = $check ? $check->Nmsgs : 0;
// Search: messages envoyés aujourd'hui à kaouther
$today = date('d-M-Y');
$criteria = 'SINCE "' . $today . '" TO "kaouther.najar@groupe-ethica.com"';
$search = @imap_search($conn, $criteria);
$found = [];
if ($search && count($search) > 0) {
foreach (array_slice($search, 0, 5) as $msgno) {
$header = @imap_headerinfo($conn, $msgno);
if ($header) {
$found[] = [
'msgno' => $msgno,
'date' => $header->date ?? '',
'subject' => $header->subject ?? '',
'to' => isset($header->to[0]) ? ($header->to[0]->mailbox . '@' . $header->to[0]->host) : '',
'cc_count' => isset($header->cc) ? count($header->cc) : 0,
'size' => $header->Size ?? 0,
];
}
}
}
// Aussi: derniers 3 messages envoyés (tous destinataires) pour validation
$recent_all = @imap_search($conn, 'SINCE "' . $today . '"') ?: [];
$recent_today = [];
foreach (array_slice(array_reverse($recent_all), 0, 5) as $msgno) {
$h = @imap_headerinfo($conn, $msgno);
if ($h) {
$recent_today[] = [
'date' => $h->date ?? '',
'subject' => $h->subject ?? '',
'to' => isset($h->to[0]) ? ($h->to[0]->mailbox . '@' . $h->to[0]->host) : '',
];
}
}
$R['attempts'][] = [
'folder' => $folder,
'status' => 'OK',
'ms' => $ms,
'total_messages' => $total,
'kaouther_today_found' => count($found),
'kaouther_messages' => $found,
'all_today_messages' => $recent_today,
];
imap_close($conn);
// Si on a trouvé Kaouther aujourd'hui, on s'arrête
if (count($found) > 0) {
$R['verdict'] = 'EMAIL SENT TODAY TO KAOUTHER FOUND';
break;
}
} else {
$R['attempts'][] = [
'folder' => $folder,
'status' => 'FAIL',
'ms' => $ms,
'error' => imap_last_error(),
];
}
}
if (!isset($R['verdict'])) {
$R['verdict'] = 'Aucun email Kaouther envoye aujourd hui detecte dans Sent folders';
}
echo json_encode($R, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);