Webklex / php-imap

PHP-IMAP is a wrapper for common IMAP communication without the need to have the php-imap module installed / enabled. The protocol is completely integrated and therefore supports IMAP IDLE operation and the "new" oAuth authentication process as well.
https://www.php-imap.com
MIT License
320 stars 151 forks source link

No body parts when opening a Message from icloud #510

Open lamigne opened 3 months ago

lamigne commented 3 months ago

Describe the bug When fetching a Message object from a icloud imap server, the message body part is empty, while the headers loads totally fine. The Structure raw property is "" and parts is []

Used config imap server : icloud composer "webklex/php-imap": "dev-master#6d999438d29ed0bb920cd897b200a3a5fd6b6380" (to avoid composer requirements and other libraries check, #498 )

Code to Reproduce The troubling code section which produces the reported bug.

 $cm = new ClientManager();
  $params = [
      'host'          => 'imap.mail.me.com',
      'port'          => 993,
      'encryption'    => 'ssl',
      'validate_cert' => true,
      'username'      => $username,
      'password'      => $password,
      'protocol'      => 'imap',
  ];

  $client = $cm->make($params);
  $client->connect();

  $folder = $client->getFolderByName('INBOX');

 if ($folder === null) {
            return false;
        }

        $messages = $folder
            ->messages()
            ->setFetchOrderAsc()
            ->all()
            ->since(new DateTimeImmutable('-10 years'))
            ->limit(10)
            ->get();

        for ($i = 0; $i < $messages->count(); ++$i) {
             $message = $messages[$i];
             print_r($message->getStructure());
        }

Expected behavior $message->getStructure() should return a correct raw value with parsed parts array.

Desktop / Server (please complete the following information):

Additional context Only the body parts are empty, all the other features tested are working. The package works fine when used with google (with OAuth2 of course)

As a temporary solution i use Ddeboer/imap for this icloud mailbox as it is not requiring OAuth token (yet)

ybizeul commented 3 months ago

I'm troubleshooting an issue with iCloud and Freescout which is also using this module.

Some messages, not all, do not get the body fetched as part of the IMAP transaction, and it seems to be a known issue with iCloud. The workaround is to use BODY[TEXT] instead of RFC822.TEXT.

I don't see a variable in this module to alter the IMAP command used, so my fix was to change :

        return $this->fetch(["$rfc.TEXT"], is_array($uids) ? $uids : [$uids], null, $uid);

to

        return $this->fetch(["BODY[TEXT]"], is_array($uids) ? $uids : [$uids], null, $uid);

https://github.com/Webklex/php-imap/blob/6d999438d29ed0bb920cd897b200a3a5fd6b6380/src/Connection/Protocols/ImapProtocol.php#L814