Alan
Alan

Reputation: 2639

MessageSending event not being fired when sending an email

I need to execute some code when an email is sent (or is being sent). My app is running Laravel 5.3. I followed the instructions noted here and registered the LogSentMessage event listener.

This is my code so far:

app/Listeners/LogSentMessage.php:

<?php

namespace App\Listeners;

use Illuminate\Mail\Events\MessageSending;

class LogSentMessage
{
    public function __construct()
    {
        //
    }

    public function handle(MessageSending $event)
    {
        logger('Hello World');
    }
}

app/Listeners/EventServiceProvider.php:

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        'Illuminate\Mail\Events\MessageSending' => [
            'App\Listeners\LogSentMessage',
        ],
    ];


    public function boot()
    {
        parent::boot();

        //
    }
}

It's working perfectly in two different development environments, the listener logs 'Hello World' but it's not working in my production environment. What might be causing that? I'm using the database queue driver to store email sendings as jobs but it shouldn't be a problem according to that:

Laravel fires an event just before sending mail messages. Remember, this event is fired when the mail is sent, not when it is queued. You may register an event listener for this event in your EventServiceProvider:

As an extra, my production environment is a Homestead image created through Laravel Forge

Upvotes: 3

Views: 1958

Answers (1)

viarnes
viarnes

Reputation: 2048

The queue worker stores the application in memory. So, if you added the logger after starting you queue listener for the first time, make sure to run php artisan queue:restart to refresh the job:

Remember, queue workers are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers.

Source: Running The Queue Worker

Upvotes: 5

Related Questions