0

I want to fetch all emails in inbox and store them in realtime but the way i m doing it is a little bit wrong : it's something like this:

class Kernel extends ConsoleKernel
 ...
   
$schedule->call(function () {

// connect using credentials

//get all emails

//copy emails

//delete emails when they get copied correctly
  
})->everyMinute(); // repeat

this Guarantees that the emails in the database will not be copied because the emails are no longer in the inbox . But now we have a case where we need to keep these messages so we replaced this with :


// connect using credentials

//get all emails 

//filter only unseen

//copy emails

//mark these emails as seen

But the problem of all of these solution is that we get emails . The solution we did is working but added another problem of reloading same emails over and over .

I reread the Documentation and found this possible solution : Using events that triggers in the entire package and capture the new event witch gets triggered when a new email is received. In this example i found some useful methods and classes .

class CustomMessageNewEvent extends Webklex\PHPIMAP\Events\MessageNewEvent {

    /**
     * Create a new event instance.
     * @var \Webklex\PHPIMAP\Message[] $messages
     * @return void
     */
    public function __construct($messages) {
        $this->message = $messages[0];
        echo "New message: ".$this->message->subject."\n";
    }
}


But i m not sure how to implement them in Laravel , juste where and how should i register / capture this `new` Event !
**Especially that we have multiple Client instances not only one ( foreach user ... )**
Thank you so much for reading all this and hopefully people find this question useful.
Yasser CHENIK
  • 370
  • 1
  • 6
  • 17

1 Answers1

0

When I've done similar tasks in the past I've used the Message-ID defined in the email spec to identify each email uniquely.

You can use your IMAP library to get a list of emails with their Message-IDs, then compare those Message-IDs to your database. Just make sure you add a new field to your Email model (or whatever you've called it) to store the Message-ID.

Joundill
  • 6,828
  • 12
  • 36
  • 50
  • some inboxes have more than 5000 emails , so getting all emails is not a good solution and is exactly what we did ! so imagine getting 5000 emails every minute and check if they are created or not in the database !! **FOREACH** User . – Yasser CHENIK Aug 19 '22 at 08:22
  • You can knock down the number of emails you query with heuristics, like if you check for the number of emails which have come in since a certain date. – Joundill Aug 19 '22 at 09:03
  • You've essentially got two options. First, you can mark on the email server whether or not emails have been downloaded. Second, you can mark on your web server whether or not they've been downloaded. – Joundill Aug 19 '22 at 09:05
  • Alright i will try filtering by time and saving current time in `checked_at` feild on db . So in next iteration i get all emails only between `checked_at` and current time and then fill the the value of `checked_at` with current time , foreach user ... – Yasser CHENIK Aug 19 '22 at 12:21
  • LoL instant problem, i juste found this : `The IMAP protocol does not allow to search for datetime keywords.` in the [documentation](https://www.php-imap.com/api/query) – Yasser CHENIK Aug 19 '22 at 12:29
  • so i have to mark them as reed or listen for the `new` message event ... – Yasser CHENIK Aug 19 '22 at 12:30
  • Just use chunks/pages and change the fetch order... – Joundill Aug 20 '22 at 00:15
  • i found a potential answer using `Message-ID` yes but in a diffirent way i ll try to implement it this week and post the answer/concept when i works – Yasser CHENIK Sep 05 '22 at 18:32
  • using [this answer](https://stackoverflow.com/a/10151366/14629458) – Yasser CHENIK Sep 06 '22 at 09:21