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
|
|
|
} |
102
|
|
|
|