up
This commit is contained in:
@@ -29,10 +29,16 @@ class CorreoImapService
|
|||||||
// ──────────────────────────────────────────────
|
// ──────────────────────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conecta, autentica y devuelve los últimos $limit correos.
|
* Conecta, autentica y devuelve:
|
||||||
* Cada elemento: [uid, from, subject, date, body]
|
* - Los correos de los últimos $minutesBack minutos, con máximo $limit emails.
|
||||||
|
* - Lo que ocurra primero: llegar al límite de tiempo o al límite de cantidad.
|
||||||
|
*
|
||||||
|
* Para tener margen suficiente se descarga un lote mayor ($fetchSize) y
|
||||||
|
* luego se filtra por fecha en PHP.
|
||||||
|
*
|
||||||
|
* Cada elemento: [seq, from, subject, date, body]
|
||||||
*/
|
*/
|
||||||
public function fetchLatest(int $limit = 5): array
|
public function fetchLatest(int $limit = 5, int $minutesBack = 5): array
|
||||||
{
|
{
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->login();
|
$this->login();
|
||||||
@@ -43,16 +49,39 @@ class CorreoImapService
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tomamos los últimos $limit números de secuencia
|
// Descargar un lote suficiente para cubrir la ventana de tiempo.
|
||||||
$from = max(1, $total - $limit + 1);
|
// 20 mensajes es razonable; si el buzón tiene pocos, se ajusta.
|
||||||
|
$fetchSize = min($total, max($limit * 4, 20));
|
||||||
|
$from = max(1, $total - $fetchSize + 1);
|
||||||
$set = "{$from}:{$total}";
|
$set = "{$from}:{$total}";
|
||||||
|
|
||||||
$emails = $this->fetchMessages($set);
|
$emails = $this->fetchMessages($set);
|
||||||
|
|
||||||
$this->logout();
|
$this->logout();
|
||||||
|
|
||||||
// Devolver en orden descendente (más reciente primero)
|
// Más reciente primero
|
||||||
return array_reverse($emails);
|
$emails = array_reverse($emails);
|
||||||
|
|
||||||
|
$cutoff = time() - ($minutesBack * 60);
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
foreach ($emails as $email) {
|
||||||
|
// Parsear la fecha del correo
|
||||||
|
$ts = $email['date'] ? @strtotime($email['date']) : false;
|
||||||
|
|
||||||
|
// Si la fecha es válida y el correo es más antiguo que la ventana → parar
|
||||||
|
if ($ts !== false && $ts < $cutoff) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result[] = $email;
|
||||||
|
|
||||||
|
// Límite de cantidad alcanzado → parar
|
||||||
|
if (count($result) >= $limit) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLastError(): string
|
public function getLastError(): string
|
||||||
|
|||||||
@@ -142,7 +142,13 @@
|
|||||||
<div>
|
<div>
|
||||||
<p class="font-semibold text-green-700 text-sm">Conexión exitosa</p>
|
<p class="font-semibold text-green-700 text-sm">Conexión exitosa</p>
|
||||||
<p class="text-green-600 text-xs mt-1">
|
<p class="text-green-600 text-xs mt-1">
|
||||||
Se encontraron <strong>{{ count($emails) }}</strong> correo(s) recientes.
|
@if(count($emails) === 0)
|
||||||
|
No hay correos en los últimos 5 minutos.
|
||||||
|
@elseif(count($emails) === 5)
|
||||||
|
Se muestran los últimos <strong>5</strong> correos (límite alcanzado).
|
||||||
|
@else
|
||||||
|
<strong>{{ count($emails) }}</strong> correo(s) en los últimos 5 minutos.
|
||||||
|
@endif
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user