@@ -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
220337If 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 ) .
0 commit comments