javanile / php-imap2

PHP IMAP with OAUTH2
https://php-imap2.javanile.org/
GNU General Public License v3.0
48 stars 28 forks source link

how to fetch mails headers ? #17

Open darkworks opened 1 year ago

darkworks commented 1 year ago

am trying to fetch mails headers so after connecting to mail box like

$imap = imap2_open($mailbox, $username, $accessToken, OP_XOAUTH2);

when i call imap2_headers like

imap2_headers($this->mail_box_handle);

i get error

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Javanile\Imap2\Message::headers(), 1 passed in 
vendor\javanile\php-imap2\bootstrap.php on line 745 and at least 2 expected in vendor\javanile\php-imap2\src\Message.php:90 Stack trace: #0 vendor\javanile\php-imap2\bootstrap.php(745): Javanile\Imap2\Message::headers(Array) #1 F:\index.php(119): imap2_headers(Array) 

so after checking lib source bootstrap.php

if (!function_exists('imap2_headers')) {
    function imap2_headers($imap)
    {
        return Message::headers($imap);
    }
}

then in Message.php definition of headers() method

    public static function headers($imap, $messageNum, $fromLength = 0, $subjectLength = 0, $defaultHost = null)
    {
        if (is_a($imap, Connection::class)) {
            $client = $imap->getClient();
            #$client->setDebug(true);

            $messages = $client->fetch($imap->getMailboxName(), $messageNum, false, ['BODY['.$section.']']);

            if ($section) {
                return $messages[$messageNum]->bodypart[$section];
            }

            return $messages[$messageNum]->body;
        }

        return imap_headerinfo($imap, $messageNum, $fromLength = 0, $subjectLength = 0);
    }

looks like headers method need at least two parameters however in bootstrap its declared incorrect. although original php imap_headers(); function would work ok with single parameter.

darkworks commented 1 year ago

seems like goal of public static function headers($imap, $messageNum, $fromLength = 0, $subjectLength = 0, $defaultHost = null) is to fetch single message header. so should be define as public static function header($imap, $messageNum, $fromLength = 0, $subjectLength = 0, $defaultHost = null) although imap2_headers should return all bulk headers