php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

How to get the previous and the next message??

Open familiaorlando100 opened this issue 3 years ago • 5 comments

I have an specific message, so I want to know the id of the previous and the next message..

familiaorlando100 avatar Aug 10 '22 17:08 familiaorlando100

Hi @familiaorlando100 , I'm not sure what you are asking. Are you looking for a threaded conversation?

If so, you can try something like this:

/** @var \Webklex\PHPIMAP\Message $message */
/** @var \Webklex\PHPIMAP\Support\MessageCollection $messages */

$messages = $message->thread($sent_folder = null);

Please visit https://www.php-imap.com/api/message for some additional documentation..

Best regards,

Webklex avatar Aug 10 '22 18:08 Webklex

Nope… I would refer when you get the list of messages and see the information of a specific message to get the message received before that message. For example: In the image If I click on the message : Inkpact how to get the MESSAGE ID of the New York Times and Stephanie at Copy blo

test1

familiaorlando100 avatar Aug 10 '22 20:08 familiaorlando100

Ah I see, you can use the UID or SequenceID (depending on your configuration: https://github.com/Webklex/php-imap/blob/master/src/config/imap.php#L147).

For example:

/** @var \Webklex\PHPIMAP\Message $message */
/** @var \Webklex\PHPIMAP\Folder $folder */

$other_message = $folder->query()->getMessageByMsgn($message->getMsgn() + 1)
$other_message = $folder->query()->getMessageByUid($message->getUid() + 1)

However you should also surround either of these operations with a try catch block, since you don't know if the message exists. Increasing / Decreasing the UID or SequenceID could fail if any of those has changed during the session or simply doesn't exist in the first place.

I would suggest to use the email date instead and work with a range.. But this would also come with some implications as well. I guess it depends on the actual use case and using UID or SequenceID might works just fine :)

Best regards & happy coding,

Webklex avatar Aug 10 '22 21:08 Webklex

Thank you for your answer! How can I verify if the message exists through the UID

familiaorlando100 avatar Aug 11 '22 01:08 familiaorlando100

You're welcome :) There is no method to test if a message exists. But like I mentioned above, you can utilize a try catch block:

try {
    /** @var \Webklex\PHPIMAP\Folder $folder */
    /** @var \Webklex\PHPIMAP\Message $message */
    $other_message = $folder->query()->getMessageByUid($message->getUid() + 1);
} catch (\Webklex\PHPIMAP\Exceptions\MessageNotFoundException $e) {
    // Message doesn't exist
}

Best regards,

Webklex avatar Aug 11 '22 02:08 Webklex