Skip to content

Commit

Permalink
Merge pull request #2 from pjio/main
Browse files Browse the repository at this point in the history
from pijo/php-imap2: Handle messages fetched by UID
  • Loading branch information
fatihfth authored Feb 12, 2023
2 parents 89922e8 + 25adff6 commit aad3c01
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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 (class_exists('\IMAP\Connection') && $imap instanceof \IMAP\Connection) || (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

0 comments on commit aad3c01

Please sign in to comment.