Skip to content

Commit f1ca638

Browse files
committed
Imap idle command added
1 parent 9eb85fb commit f1ca638

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ read and parse existing mails and much more.
2828
- [Idle](#idle)
2929
- [oAuth](#oauth)
3030
- [Events](#events)
31+
- [Commands](#commands)
3132
- [Support](#support)
3233
- [Known issues](#known-issues)
3334
- [Security](#security)
@@ -216,6 +217,122 @@ Additional integration information:
216217
- https://laravel.com/docs/5.2/events#event-subscribers
217218
- https://github.com/Webklex/php-imap#events
218219

220+
221+
# Commands
222+
Let's assume you want to run the imap idle process in the background of your server to automatically handle new
223+
messages. The following examples will show two major ways to archive this:
224+
225+
### Event driven
226+
Start by adding the following to your `app/Console/Kernel.php` file:
227+
```php
228+
/**
229+
* The Artisan commands provided by your application.
230+
*
231+
* @var array
232+
*/
233+
protected $commands = [
234+
\Webklex\IMAP\Commands\ImapIdleCommand::class,
235+
];
236+
```
237+
Now register an event listener as described by [here](https://laravel.com/docs/7.x/events#event-subscribers).
238+
If you don't use the default account, or if you want to add some of your own magic, you'll need to create a
239+
custom command (see next section).
240+
241+
Finally test the command by running:
242+
```bash
243+
php artisan imap:idle
244+
```
245+
246+
### Custom Command
247+
Create a new file like `app/Console/Commands/CustomImapIdleCommand.php` and add the following:
248+
```php
249+
<?php
250+
namespace App\Console\Commands;
251+
252+
use Webklex\IMAP\Commands\ImapIdleCommand;
253+
use Webklex\PHPIMAP\Message;
254+
255+
class CustomImapIdleCommand extends ImapIdleCommand {
256+
257+
/**
258+
* The name and signature of the console command.
259+
*
260+
* @var string
261+
*/
262+
protected $signature = 'custom_command';
263+
264+
/**
265+
* Holds the account information
266+
*
267+
* @var string|array $account
268+
*/
269+
protected $account = "default";
270+
271+
/**
272+
* Callback used for the idle command and triggered for every new received message
273+
* @param Message $message
274+
*/
275+
public function onNewMessage(Message $message){
276+
$this->info("New message received: ".$message->subject);
277+
}
278+
279+
}
280+
```
281+
..and add the following to your `app/Console/Kernel.php` file:
282+
```php
283+
/**
284+
* The Artisan commands provided by your application.
285+
*
286+
* @var array
287+
*/
288+
protected $commands = [
289+
\App\Console\Commands\CustomImapIdleCommand::class,
290+
];
291+
```
292+
293+
Finally test the command by running:
294+
```bash
295+
php artisan custom_command
296+
```
297+
298+
## Service setup
299+
A basic systemd service can be setup by creating a service file like this:
300+
```bash
301+
nano /etc/systemd/system/imap-idle.service
302+
```
303+
..and adding:
304+
```bash
305+
[Unit]
306+
Description=ImapIdle
307+
After=multi-user.target
308+
After=syslog.target
309+
After=network-online.target
310+
311+
[Service]
312+
Type=simple
313+
314+
User=www-data
315+
Group=www-data
316+
317+
WorkingDirectory=/var/www/my_project
318+
ExecStart=/var/www/my_project/artisan fetch:idle
319+
320+
Restart=on-failure
321+
RestartSec=5s
322+
323+
[Install]
324+
WantedBy=multi-user.target
325+
```
326+
327+
You can now test the service by running:
328+
```bash
329+
systemctl start imap-idle.service
330+
systemctl status imap-idle.service
331+
systemctl stop imap-idle.service
332+
systemctl restart imap-idle.service
333+
```
334+
335+
219336
## Support
220337
If you encounter any problems or if you find a bug, please don't hesitate to create a new
221338
[issue](https://github.com/Webklex/laravel-imap/issues).

src/Commands/ImapIdleCommand.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Webklex\IMAP\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Log;
7+
use Webklex\IMAP\Facades\Client as ClientFacade;
8+
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
9+
use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
10+
use Webklex\PHPIMAP\Folder;
11+
use Webklex\PHPIMAP\Message;
12+
13+
class ImapIdleCommand extends Command {
14+
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = 'imap:idle';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Fetch new messages by utilising imap idle';
28+
29+
/**
30+
* Holds the account information
31+
*
32+
* @var string|array $account
33+
*/
34+
protected $account = "default";
35+
36+
/**
37+
* Name of the used folder
38+
*
39+
* @var string $folder_name
40+
*/
41+
protected $folder_name = "INBOX";
42+
43+
/**
44+
* Create a new command instance.
45+
*
46+
* @return void
47+
*/
48+
public function __construct() {
49+
parent::__construct();
50+
}
51+
52+
/**
53+
* Callback used for the idle command and triggered for every new received message
54+
* @param Message $message
55+
*/
56+
public function onNewMessage(Message $message){
57+
$this->info("New message received: ".$message->subject);
58+
}
59+
60+
/**
61+
* Execute the console command.
62+
*
63+
* @return int
64+
*/
65+
public function handle() {
66+
if (is_array($this->account)) {
67+
$client = ClientFacade::make($this->account);
68+
}else{
69+
$client = ClientFacade::account($this->account);
70+
}
71+
72+
try {
73+
$client->connect();
74+
} catch (ConnectionFailedException $e) {
75+
Log::error($e->getMessage());
76+
return 1;
77+
}
78+
79+
/** @var Folder $folder */
80+
try {
81+
$folder = $client->getFolder($this->folder_name);
82+
} catch (ConnectionFailedException $e) {
83+
Log::error($e->getMessage());
84+
return 1;
85+
} catch (FolderFetchingException $e) {
86+
Log::error($e->getMessage());
87+
return 1;
88+
}
89+
90+
try {
91+
$folder->idle(function($message){
92+
$this->onNewMessage($message);
93+
});
94+
} catch (\Exception $e) {
95+
Log::error($e->getMessage());
96+
return 1;
97+
}
98+
99+
return 0;
100+
}
101+
}

0 commit comments

Comments
 (0)