Skip to content

Commit c824fa4

Browse files
committed
IDLE like support added #185
1 parent 1993f33 commit c824fa4

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ Laravel IMAP is an easy way to integrate the native php imap library into your *
2929
- [Attachments](#attachments)
3030
- [Advanced fetching](#advanced-fetching)
3131
- [Masking](#masking)
32-
- [Specials](#specials)
32+
- [Idle](#idle)
3333
- [Events](#events)
34+
- [Specials](#specials)
3435
- [Support](#support)
3536
- [Documentation](#documentation)
3637
- [Client::class](#clientclass)
@@ -552,23 +553,35 @@ Additional examples can be found here:
552553
- [Custom attachment mask](https://github.com/Webklex/laravel-imap/blob/master/examples/custom_attachment_mask.php)
553554

554555

555-
#### Specials
556-
Find the folder containing a message:
556+
#### Idle
557+
Use the `Query::idle()` function to have an instance running which behaves like an idle imap connection.
558+
The callback and `Webklex\IMAP\Events\MessageNewEvent($message)` event get fired by every new incoming email.
557559
``` php
558-
$oFolder = $aMessage->getContainingFolder();
560+
$timeout = 10;
561+
/** @var \Webklex\IMAP\Folder $folder */
562+
$folder->query()->subject('test')->idle(function($message){
563+
/** @var \Webklex\IMAP\Message $message */
564+
dump("new message", $message->subject);
565+
}, $timeout);
559566
```
560567

561568
#### Events
562569
The following events are available:
563570
- `Webklex\IMAP\Events\MessageDeletedEvent($message)` — triggered by `Message::delete`
564571
- `Webklex\IMAP\Events\MessageRestoredEvent($message)` — triggered by `Message::restore`
565572
- `Webklex\IMAP\Events\MessageMovedEvent($old_message, $new_message)` — triggered by `Message::move`
566-
- `Webklex\IMAP\Events\MessageNewEvent($message)` — not triggered
573+
- `Webklex\IMAP\Events\MessageNewEvent($message)` — can get triggered by `Query::idle()`
567574

568575
Additional integration information:
569576
- https://laravel.com/docs/7.x/events#event-subscribers
570577
- https://laravel.com/docs/5.2/events#event-subscribers
571578

579+
#### Specials
580+
Find the folder containing a message:
581+
``` php
582+
$oFolder = $aMessage->getContainingFolder();
583+
```
584+
572585
## Support
573586
If you encounter any problems or if you find a bug, please don't hesitate to create a new [issue](https://github.com/Webklex/laravel-imap/issues).
574587
However please be aware that it might take some time to get an answer.
@@ -676,6 +689,7 @@ if you're just wishing a feature ;)
676689
| getSender | | array | Get the current sender information |
677690
| getBodies | | mixed | Get the current bodies |
678691
| getRawBody | | mixed | Get the current raw message body |
692+
| getToken | | string | Get a base64 encoded message token |
679693
| getFlags | | FlagCollection | Get the current message flags |
680694
| is | | boolean | Does this message match another one? |
681695
| getStructure | | object | The raw message structure |
@@ -741,6 +755,7 @@ if you're just wishing a feature ;)
741755
| bcc | string $value | WhereQuery | Select messages matching a given receiver (BCC:) |
742756
| count | | integer | Count all available messages matching the current search criteria |
743757
| get | | MessageCollection | Fetch messages with the current query |
758+
| idle | mixed $callback($message), int $timeout = 10 | | Simulate an idle connection to receive new messages "live" |
744759
| limit | integer $limit, integer $page = 1 | WhereQuery | Limit the amount of messages being fetched |
745760
| setFetchOptions | boolean $fetch_options | WhereQuery | Set the fetch options |
746761
| setFetchBody | boolean $fetch_body | WhereQuery | Set the fetch body option |

src/IMAP/Query/Query.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Carbon\Carbon;
1616
use Webklex\IMAP\Client;
17+
use Webklex\IMAP\Events\MessageNewEvent;
1718
use Webklex\IMAP\Exceptions\GetMessagesFailedException;
1819
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
1920
use Webklex\IMAP\IMAP;
@@ -240,6 +241,37 @@ public function paginate($per_page = 5, $page = null, $page_name = 'imap_page'){
240241
return $this->get()->paginate($per_page, $this->page, $page_name);
241242
}
242243

244+
/**
245+
* Create an idle like instance to catch incoming messages
246+
* @param callable|null $callback
247+
* @param int $timeout
248+
* @throws GetMessagesFailedException
249+
* @throws \Webklex\IMAP\Exceptions\ConnectionFailedException
250+
*/
251+
public function idle(callable $callback = null, $timeout = 10){
252+
$known_messages = [];
253+
$this->get()->each(function($message) use(&$known_messages){
254+
/** @var Message $message */
255+
$known_messages[] = $message->getToken();
256+
});
257+
while ($this->getClient()->isConnected()){
258+
$this->getClient()->expunge();
259+
$this->get()->each(function($message) use(&$known_messages, $callback){
260+
/** @var \Webklex\IMAP\Message $message */
261+
$token = $message->getToken();
262+
if(in_array($token, $known_messages)){
263+
return;
264+
}
265+
$known_messages[] = $token;
266+
MessageNewEvent::dispatch($message);
267+
if ($callback){
268+
$callback($message);
269+
}
270+
});
271+
sleep($timeout);
272+
}
273+
}
274+
243275
/**
244276
* Get the raw IMAP search query
245277
*

0 commit comments

Comments
 (0)