From 25adff6ebbbfce00e69c5d39dc29181113105714 Mon Sep 17 00:00:00 2001 From: Carl-Philipp Jung Date: Mon, 28 Nov 2022 15:13:12 +0100 Subject: [PATCH] Handle messages fetched by UID --- src/Functions.php | 18 ++++++++++++++++++ src/Message.php | 12 ++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Functions.php b/src/Functions.php index 458dd73..dbb45fe 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -195,4 +195,22 @@ public static function isRetrofitResource($imap) { return is_resource($imap) && get_resource_type($imap) == 'imap'; } + + public static function keyBy(string $name, array $list): array + { + $keyBy = []; + foreach ($list as $item) { + if (!isset($item->$name)) { + trigger_error('keyBy: key "' . $name . '" not found!', E_USER_WARNING); + continue; + } + if (isset($keyBy[$item->$name])) { + trigger_error('keyBy: duplicate key "' . $name . '" = "' . $item->$name . '"', E_USER_WARNING); + continue; + } + $keyBy[$item->$name] = $item; + } + + return $keyBy; + } } diff --git a/src/Message.php b/src/Message.php index 1391dd0..b0ee205 100644 --- a/src/Message.php +++ b/src/Message.php @@ -165,6 +165,10 @@ public static function body($imap, $messageNum, $flags = 0) $messages = $client->fetch($imap->getMailboxName(), $messageNum, $isUid, ['BODY[TEXT]']); + if ($isUid && is_array($messages)) { + $messages = Functions::keyBy('uid', $messages); + } + return $messages[$messageNum]->bodypart['TEXT']; } @@ -186,6 +190,10 @@ public static function fetchBody($imap, $messageNum, $section, $flags = 0) return false; } + if ($isUid && is_array($messages)) { + $messages = Functions::keyBy('uid', $messages); + } + if ($section) { return $messages[$messageNum]->bodypart[$section]; } @@ -217,6 +225,10 @@ public static function fetchMime($imap, $messageNum, $section, $flags = 0) return ""; } + if ($isUid && is_array($messages)) { + $messages = Functions::keyBy('uid', $messages); + } + if ($section && isset($messages[$messageNum]->bodypart[$sectionKey])) { return $messages[$messageNum]->bodypart[$sectionKey]; }