php-imap
php-imap copied to clipboard
Is it possible to search from overview with Outlook? either by chunks without getting a connection error?
I have implemented this library because I have a command that connects every 15 minutes to the gmail and outlook emails capturing and extracting the emails and registering it in the DB according to the form. All this is done through OVERVIEW and searching from the last UUID registered in the database.
At the time I had the problem that brought me null or brought me from the first message that may occur 5 years ago, and that after a time 3 minutes I threw error because the connection was closed, all this error occurred with OUTLOOK.
This is my current code
$cm = new ClientManager([
'options' => [
'fetch' => \Webklex\PHPIMAP\IMAP::FT_PEEK,
'fetch_order' => 'desc',
'sequence' => \Webklex\PHPIMAP\IMAP::ST_UID,
],
]);
$client = $cm->make([
'host' => $email->server,
'port' => $email->port,
'protocol' => $email->server_type,
'encryption' => $email->encryption,
'validate_cert' => true,
'username' => $email->email,
'password' => $email->password,
]);
//Folder in which to search for emails.
$folder = $client->getFolder($email->folder);
//Email ID where data is captured to be updated.
$idMail = $email->id;
//Get the last uid in case it is outlook
if ($email->server == 'outlook.office365.com') {
$today = Carbon::now();
$currentDay = $today->format('d-m-Y');
$messages = $folder->query()->since($currentDay)->limit(1)->get();
}else{
//Gmail normal process as the overview works.
$rangeMessages = $folder->overview("$email->lastId:*");
echo (count($rangeMessages) > 0 ? "Hay ".count($rangeMessages) ." mensajes.\n" : "No hay mensajes \n");
}
//For Gmail method overview.
if (!is_null($email->lastId) && $email->server == 'imap.gmail.com') {
if ($rangeMessages > 0 || $rangeMessages != null) {
foreach ($rangeMessages as $key => $dat) {
$data = $folder->query()->getMessageByUid($key);
Quote::readByUid($data,$email->id,1);
}
}
//For Oulook method overview
}elseif (!is_null($email->lastId) && $email->server == 'outlook.office365.com') {
//We go back to the most recent data to extract the uid.
foreach ($messages as $key => $data) {
$uidMostRecent = $data->uid;
echo "El email más reciente tiene un UID de: $uidMostRecent \n";
}
$old = $email->lastId;
for ($i=$email->lastId; $i <= $uidMostRecent ; $i+=6) {
if ($uidMostRecent >= $i) {
try {
echo "\n Ejecutando Overview de $old HASTA $i \n";
echo "Estado de conexión: ".$client->isConnected()."\n";
if ($client->isConnected() == false) {
$client->reconnect();
}
$rangeMessages = $folder->overview("$old:$i");
} catch (\Throwable $th) {
Log::channel('mailParse')->error("No existe UID => $i \n");
Log::channel('mailParse')->error("$th \n");
echo "No existe uid => $i \n";
echo "$th \n";
}
if (!is_null($rangeMessages)) {
foreach ($rangeMessages as $k => $v) {
try {
$data = $folder->query()->getMessageByUid($k);
Quote::readByUid($data,$email->id,1);
} catch (\Throwable $th) {
echo "ERROR UID NO EXISTE => $k \n";
echo $th ."\n";
Log::channel('mailParse')->error("ERROR: No existe UID => $i \n");
Log::channel('mailParse')->error("$th \n");
}
}
}else{
echo "No se encuentra semejanza de la busqueda de uid entre $old hasta $i \n";
}
$old = $i;
}
}
//For search by 30 days ago and by date.
}else{
Quote::readEmails($messages,$email->id,1);
}
$client->disconnect();
Is there a way now to work with overview in outlook and chunks?
Hi @alexv96 , thats indeed a good question. It seem like outlook doesn't like long operations. Fetching messages in chunks does exactly that.
Do you need to fetch all messages? Could you limit them with a date (maybe only all messages of the last 30 days)? You could also call a client reconnect within the chunked callback to "renew" your connection.
Best regards,