Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand All @@ -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];
}
Expand Down Expand Up @@ -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];
}
Expand Down