Webklex / laravel-imap

Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app.
https://www.php-imap.com
MIT License
641 stars 182 forks source link

New message event #446

Closed Waiker closed 2 years ago

Waiker commented 2 years ago

Thanks for your project! I need to perform some actions when a new message arrives.

In /app/Providers/EventServiceProvider.php file I added a MessageNewEvent according to your documentation and added a NewEmailNotification::class listener to it, but nothing happens when a new message arrives in the mailbox.

EventServiceProvider.php

<?php

namespace App\Providers;

use App\Listeners\NewEmailRegistered;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        MessageNewEvent::class => [
            NewEmailNotification::class,
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

NewEmailNotification::class

<?php

namespace App\Listeners;

use App\Events\WebklexIMAPEventsMessageNewEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class NewEmailNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  MessageNewEvent  $event
     * @return void
     */
    public function handle(MessageNewEvent $event)
    {
        //
        Log::info('New Email');
    }
}

p/s the test code from the page successfully connects to mail and reads all messages.

Webklex commented 2 years ago

Hi @Waiker , thanks for the detailed question. The MessageNewEvent only gets dispatched if a new message got fetched. It looks to me as if you aren't fetching any messages.

If you want to continuously watch for new messages, checkout the IDLE command: https://www.php-imap.com/frameworks/laravel/commands

I hope this helps :)

Best regards & happy coding,

Waiker commented 2 years ago

Thank you. I will use the command