php-imap: idle function not returning any message when new mail comes

idle function not returning any message when new mail comes

Config:

        'custom' => [
            'host' => 'outlook.office365.com',
            'port' => 993,
            'encryption' => "ssl",
            'validate_cert' => true,
            'username' => 'email@my_domain.com',
            'password' => get_access_token(),
            'authentication' => "oauth",
        ],
// halper_functions.php
function get_access_token()
    {
        $tenantId = env("TENANT_ID");
        $clientId = env("CLIENT_ID");
        $clientSecret = env("CLIENT_SECRET");
        $guzzle = new \GuzzleHttp\Client();
        $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/v2.0/token';
        $token = json_decode($guzzle->post($url, [
            'form_params' => [
                'client_id' => $clientId,
                'client_secret' => $clientSecret,
                'scope' => 'https://outlook.office.com/IMAP.AccessAsUser.All',
                'grant_type' => 'password',
                'username' => 'email@my_domain.com',
                'password' => 'password',
            ],
        ])->getBody()->getContents());
        $accessToken = $token->access_token;
        return $accessToken;
    }

custom command:

public function handle()
    {
        $client = Client::account("custom");
        try {
            $client->connect();
        } catch (ConnectionFailedException $e) {
            Log::error($e->getMessage());
            return 1;
        }
        try {
            /** @var Folder $folder */
            $folder = $client->getFolder("INBOX");
        } catch (ConnectionFailedException $e) {
            Log::error($e->getMessage());
            return 1;
        } catch (FolderFetchingException $e) {
            Log::error($e->getMessage());
            return 1;
        }
        try {
            dump("listening...");
            $folder->idle(function ($message) {
            dump($message); // this doesn't run
            }, 1200, true);
        } catch (ConnectionFailedException $e) {
            Log::error($e->getMessage());
            return 1;
        }

but when i get messages manually it works

Route::get("test", function () {
    $client = Client::account("custom");
    try {
        $client->connect();
    } catch (ConnectionFailedException $e) {
        throw $e;
    }
    try {
        /** @var Folder $folder */
        $folder = $client->getFolder("INBOX");
    } catch (ConnectionFailedException $e) {
        throw $e;
    } catch (FolderFetchingException $e) {
        throw $e;
    }
    
    dump($client->getFolder("INBOX")->examine());
    dump($client->getFolder("INBOX")->messages()->all()->limit(5)->get());
    dd("done");
});

Screenshot 2022-08-19 034123 Screenshot 2022-08-19 034140

  • OS: CentOs (prod), win 11 (dev)
  • PHP: 8.1
  • webklex/laravel-imap : 2.4
  • Provider: Outlook

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 26 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @syedajmal1998 , nah you’ve did everything right 😃 Thanks for bringing this issue up 👍

At the moment I would like to wait a little bit to see if this “feature” gets fixed by outlook / Microsoft or if other providers behave the same. I don’t think they intended it to work this way. Also removing the “idle” check in general isn’t great. So for now I wont modify the idle methods. However you can use the snipped from above to build your own custom idle implementation.

I hope you’re having a great weekend 😃

Best regards,

and sorry if i said or did something wrong this was my first ever issued a bug in github 😃

here is the full one

<< * OK The Microsoft Exchange IMAP4 service is ready. [some long code]

TAG1 AUTHENTICATE XOAUTH2 [some too long code] << TAG1 OK AUTHENTICATE completed. TAG2 LIST “” “*” << * LIST (\Marked \HasNoChildren) “/” Archive << * LIST (\Marked \HasChildren) “/” Calendar << * LIST (\HasNoChildren) “/” Calendar/Birthdays << * LIST (\HasNoChildren) “/” “Calendar/United States holidays” << * LIST (\HasChildren) “/” Contacts << * LIST (\HasChildren) “/” “Conversation History” << * LIST (\Marked \HasNoChildren \Trash) “/” “Deleted Items” << * LIST (\Marked \HasNoChildren \Drafts) “/” Drafts << * LIST (\Marked \HasNoChildren) “/” INBOX << * LIST (\HasNoChildren) “/” Journal << * LIST (\HasNoChildren \Junk) “/” “Junk Email” << * LIST (\HasNoChildren) “/” Notes << * LIST (\HasNoChildren) “/” Outbox << * LIST (\HasNoChildren) “/” “RSS Subscriptions” << * LIST (\HasNoChildren \Sent) “/” “Sent Items” << * LIST (\HasChildren) “/” “Sync Issues” << * LIST (\HasNoChildren) “/” “Sync Issues/Conflicts” << * LIST (\HasNoChildren) “/” “Sync Issues/Local Failures” << * LIST (\HasNoChildren) “/” “Sync Issues/Server Failures” << * LIST (\HasNoChildren) “/” Tasks << TAG2 OK LIST completed. ^ “listening…” TAG3 LOGOUT << * BYE Microsoft Exchange Server IMAP4 server signing off. << TAG3 OK LOGOUT completed. << * OK The Microsoft Exchange IMAP4 service is ready. [again some long code] TAG1 AUTHENTICATE XOAUTH2 [again some too long code] << TAG1 OK AUTHENTICATE completed. TAG2 CAPABILITY << * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+ << TAG2 OK CAPABILITY completed. TAG3 SELECT “INBOX” << * 1516 EXISTS << * 0 RECENT << * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent) << * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags << * OK [UNSEEN 21] Is the first unseen message << * OK [UIDVALIDITY 14] UIDVALIDITY value << * OK [UIDNEXT 3977] The next unique identifier value << TAG3 OK [READ-WRITE] SELECT completed. TAG4 IDLE << + IDLE accepted, awaiting DONE command.