barbushin / php-imap

Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
MIT License
1.66k stars 457 forks source link

Need to get header information of latest mail with particular body content #668

Open hiralmavani opened 2 years ago

hiralmavani commented 2 years ago

I need to get header information using below function :

I have 2 mail with body content like : abcd1234

I need to get header information of letest mail contan abcd1234 content.

$mailbox  = new PhpImap\Mailbox(connection info)
$mailFolders = $mailMailbox->getMailboxes('*');
$mailids = [];
foreach($mailFolders as $folder) {
    $mailMailbox->switchMailbox($folder['shortpath']);
     $mail_ids = $mailMailbox->searchMailbox('BODY "abcd1234"');
     //some string array function and get max $mailboxIdArray
}
$recentMailId = $mailboxIdArray[0]; 
$mail = $mailMailbox->getMail(
    $recentMailId,
    true
);
print_r($mail->headersRaw);

Here I did not get result. As I also got proper value $recentMailId = 990 Got below error : Next UnexpectedValueException: Could not fetch message header from mailbox! Can you please help me out

Sebbo94BY commented 2 years ago

In theory, it should work like you showed it.

You get the error from the getMail() function call, right?

Can you please do a try-catch and check, if you get further information there?

try {
    $mail = $mailMailbox->getMail(
        $recentMailId,
        true
    );
} catch (UnexpectedValueException $e) {
    print_r($e->getMessage());
}