Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getMessage does not work anymore with UID #32

Closed
SDCRoman opened this issue Oct 20, 2020 · 7 comments
Closed

getMessage does not work anymore with UID #32

SDCRoman opened this issue Oct 20, 2020 · 7 comments

Comments

@SDCRoman
Copy link

I have recently updated the package from v1.4 -> v.2.2

In v1.4 I had this code to get a certain message:

$message = $oClient->getFolder($data->folder)->getMessage($data->muid, $fetch_body=false, $fetch_flags=false);

I have updated the syntax for v2

$message = $oClient->getFolder($data->folder)->query()->getMessage($msgn=$data->muid, $fetch_body=false, $fetch_flags=false);

But I get always an error: unique id not found

The UID is the position in the current folder. I check that the folder is correct. In my case $data->folder was INBOX.
I tried using the message id, but this also did not work. I tried getting the number getMessageNo, but it is null for all of my messages.

While I am here asking, what is the difference between UID and Msgn?

@Webklex
Copy link
Owner

Webklex commented Oct 20, 2020

Hi @SDCRoman ,
thanks for your report. Unfortunately I can't reproduce the described problem. Therefor a few questions:
1.) Is your message available, if you fetch all messages?

$client->getFolder($data->folder)->query()->all()->get()->each(function($message) use($uid_to_be_found){
    if ($message->uid == $uid_to_be_found){
        var_dump($message);
    }
});

2.) Assuming you are using imap and not legacy-imap as protocol. If you fetch an uid overview, is your uid included?

var_dump($client->getFolder($data->folder)->query()->getConnection()->getUid());

The difference between UID and Msgn isn't clear and depends on your mail provider / server. Sometimes they are identical, sometimes the UID is unique and never changes, sometimes it is not and changes with every expunged change. However in general I believe it is save to say, that the UID stands for unique id and Msgn for the message "index" number. For example Msgn=10 if it's the tenth message...

Best regrards,

@mynamespace
Copy link

From my experience, Uid is not working because of a wrong loop inside ImapProtocol.php, function getUid().

public function getUid($id = null) {
	$uids = $this->fetch('UID', 1, INF);
	if ($id == null) {
		return $uids;
	}

	foreach ($uids as $k => $v) {
		if ($k == $id) {
			return $v;
		}
	}

	throw new RuntimeException('unique id not found');
}

if you dump $uids, you get something like this:

array:61 [▼
1 => "20674"
2 => "20675"
3 => "20676"
4 => "20677"
5 => "20678"
6 => "20679"
7 => "20680"
8 => "20681"
9 => "20682"
10 => "20683"
11 => "20684"
12 => "20685"
13 => "20686"
14 => "20687"
15 => "20688"
16 => "20689"
...

but the loop that searches for the uid is this

	foreach ($uids as $k => $v) {
		if ($k == $id) {
			return $v;
		}
	}

and should be

	foreach ($uids as $k => $v) {
		if ($v == $id) {
			return $v;
		}
	}

this way it finds the id from the list of uids. But then I get another error "no headers found".
Unfortunately I'm experiencing many problems with this new release of laravel-imap/php-imap and I think I'll go back to version 1 :(
Another one is the sort order of messages inside a folder. Maybe I'll open another issue.
Thanks anyway for your work.

Best regards

@SDCRoman
Copy link
Author

@mynamespace I was able to adjust the sort order by going into vendor->webklex->laravel-imap->src->config->imap.php and setting the options there: 'fetch_order' => 'desc',

@mynamespace
Copy link

@SDCRoman thanks, I've already tried that (clearing config cache) without success :(

@SDCRoman
Copy link
Author

SDCRoman commented Oct 21, 2020

@Webklex Thanks again for the fast reply.

This way of getting the message seems to work. I tried to use this to change flags, but the behaviour is weird. It will change the flag, but when I try to change the flag of a second message it will unflag all other messages. For example I have 3 seen messages. I mark msg 1 as unseen. Now I have 2 seen and 1 unseen. When I try to mark the next msg I will have again 2 seen and 1 unseen, but I should have 2 unseen and 1 seen.

$oClient->getFolder($data->folder)->query()->all()->get()->each(function($message) use($uid_to_be_found, $read_status) {
                if ($message->uid == $uid_to_be_found) {
                    if ($read_status == 0) {
                        $message->setFlag('Seen');
                    } else {
                        $message->unsetFlag('Seen');
                    }

                    return true;
                }
            });

Also executing var_dump($client->getFolder($data->folder)->query()->getConnection()->getUid()); gives me:

Method Webklex\PHPIMAP\Query\WhereQuery::whereGetConnection() is not supported

I used composer show webklex/php-imap & composer show webklex/laravel-imap to verify that I am using the newest versions: 2.2.2 & 2.2.0

Maybe I messed up here? That is how I create the Client:

use Webklex\IMAP\Facades\Client;

protected $oClient;

public function __construct() {
    $this->oClient = Client::make([
        'host'          => env('MAIL_HOST_IN'),
        'port'          => env('MAIL_PORT_IN'),
        'encryption'    => env('MAIL_ENCRYPTION_IN'),
        'validate_cert' => true,
        'username'      => env('MAIL_USERNAME'),
        'password'      => env('MAIL_PASSWORD'),
        'protocol'      => 'imap',
        'authentication' => null
    ]);
    $this->oClient->connect();
}

EDIT

It seems UID is not needed anymore? I manually changed the msgn to 1 $msgn=1 and it works.

$message = $oClient->getFolder($data->folder)->query()->getMessage($msgn=1, $fetch_body=false, $fetch_flags=false)

But I can't get the message number, for me it is null:

foreach($aMessage as $oMessage) {
    $result[$message_id]['Msgn'] = $oMessage->getMessageNo(); // this is null
}

Also $msgn=1 is not the first message in the INBOX folder, that is also weird.

EDIT

I am using now a combination of other identifiers to select a message (messageId + subject). This works for me:


$message = $oClient->getFolder($data->folder)->query()->subject($data->elsub)->messageId($data->mid)->get(); 

// Failcheck if could not identify a single message
if (count($message) != 1) {
    return ['success' => false, 'message' => 'Could not identify message or no message found'];   
}

$message = $message[0];

@Webklex
Copy link
Owner

Webklex commented Oct 23, 2020

Hi @SDCRoman ,
you are correct. I messed up my example - I wrote Query::getConnection() instead of Query::getClient()->getConnection().

I'm a bit out of ideas. The fact that the uid can be found if you extend the search criteria is beyond my understanding. I'm also not sure if this is a bug or some other strange behavior. Please feel free to add any new insights.

Best regards,

P.s.: is the used account used by any other application / client at the same time? Could they mess up the session (very unlikely - but currently my best guess).

@SDCRoman
Copy link
Author

Thanks for the reply webklex. I am currently happy with my workaround. Actually it is even better to build your own id.

I will update the topic if I find out why the issue happened. Maybe I messed something up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants