0

I'm using the php-imap package and I want to listen for incoming emails.

The way to achieve this with php-imap is to call an idle() function, like this:

$emailFolder->idle(function (Message $email){
   // dispatch event
});

The problem is that the idle() function never finishes running and the rest of the code is never executed.

I need to call the idle() function for each user, since I must listen for incoming emails for all of my users.

T've tried to set up a job for each user and dispatch it to a queue, but then the job never ends (and since the numbers of users changes, the number of workers must change), and I can't find a way to stop the job when needed.

I also need a way to stop the execution of the idle() function for a specific user, for example if the user deletes its account.

Is there a way to run that piece of code in the background, without it interfering with the code execution, with the possibility to stop the execution of a specific instance of the process?

Cata
  • 29
  • 4
  • Question: Have you considered about the "webhooks" or "polling" methods in combination with cron-job ? That's the way I would deal such case. – aspirinemaga Feb 28 '22 at 14:19
  • @aspirinemaga I cannot implement webhooks since every user connects with its own IMAP credentials and not all users might have the possibility to add a webhook to their mailbox (ex: Roundcube Webmail does not support webhooks). I want to avoid constant "polling" of the emails to avoid sending too many requests to the IMAP server, since a lot of them have request limits. – Cata Feb 28 '22 at 14:29
  • Does a job and queue suit your needs?, as answered in [https://stackoverflow.com/a/31452573/10579934](https://stackoverflow.com/a/31452573/10579934) – kaizen Feb 28 '22 at 16:00
  • @javi Because the `idle()` function never stops running, the queued job never ends, and I'd have to create a worker for each instance of the `idle()` function (basically for each user, because each user has its own email credentials). Also, I need to be able to stop a job from running, for example if a user deletes its account, I must stop listening (through the `idle()` function) for incoming emails for that user. – Cata Mar 01 '22 at 13:51

1 Answers1

0

I found a solution.

Here it is: https://github.com/Webklex/php-imap/issues/206

Cata
  • 29
  • 4