Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

catabozan's avatar

Dispatch events from separate php processes

I'm using the php-imap package to allow the users of my app to connect to their mailbox. I want to listen to incoming emails.

I call the idle() function from php-imap - link to idle() docs - for each user (to listen for incoming emails) and I want to stop the execution of it when, for example, a user deletes its account.

The problem is that the idle() function never finishes executing, because of that my code never finishes running and gets stuck.

Is there a way to run that idle() function in a separate PHP process so that the Laravel code doesn't get stuck, but with the possibility of dispatching en event within the idle() function (when a new email is received)?

0 likes
3 replies
catabozan's avatar

@sr57 I connect to an IMAP server using php-imap, then if I want to listen for incoming emails I need to do something like this:

/** @var \Webklex\PHPIMAP\Folder $inboxFolder */
$inboxFolder->idle(function (\Webklex\PHPIMAP\Message $email){
		\App\Events\NewEmailReceived::dispatch($email);
})
// the code here will never be executed

And because each user connects to its own email account (i.e. IMAP server). I need to listen for incoming emails for all my users. So for each user there must be an instance of $inboxFolder->idle().

I know about laravel-mailbox but unfortunately does not support multiple connections at once, and you must use a service like Mailgun or SendGrid, you cannot connect via IMAP, to listen for incoming emails.

Please or to participate in this conversation.