-1

I'm using Queue to send my mails on my application, and it's working great:

class SendMail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $user_mail;
    private $person_data;
    private $title;
    private $company_name;
    private $html;
    private $email_sender;
    private $email_reply;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user_mail, $person_data, $title, $company_name, $html, $email_sender, $email_reply)
    {
        $this->user_mail = $user_mail;
        $this->person_data = $person_data;
        $this->title = $title;
        $this->company_name = $company_name;
        $this->html = $html;
        $this->email_sender = $email_sender;
        $this->email_reply = $email_reply;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::to($this->user_mail)
          ->queue(new DocumentMessage($this->person_data, $this->title, $this->company_name, $this->html,
            $this->email_sender, $this->email_reply));
    }
}

Now I want to get the log of the moment when the emails were sent by the queue and, following the documentation, I put this code in my AppServiceProvider for testing:

class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    Carbon::setLocale('pt_BR');
    setlocale(LC_ALL, 'pt_BR');
    Carbon::setUtf8(true);
    Paginator::useBootstrapThree();
    Blade::withoutDoubleEncoding();

    Queue::after(function (JobProcessing $event) {
      DB::raw("INSERT INTO activity_log (log_name, description, subject_id, subject_type, causer_id, causer_type, properties)
                      VALUES ('email_sent', now(), null, null, 1111,
                              'App\Models\User','');");
    });
  }

  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

But nothing happens after i send mails using my queue. Should i restart my queue job or do something after modify AppServideProvider?

Guilherme do Valle
  • 401
  • 1
  • 4
  • 17

1 Answers1

1

According to the documentation, JobProcessed is the right event class to use. You might want to update this:

Queue::after(function (JobProcessed $event) {
    ...
});

By the way, I suggest a bit cleaner approach that leverage the framework better. Laravel has already includes the Illuminate\Mail\Events\MessageSent out of the box. So you can listen to the mail event.

Kevin Bui
  • 2,754
  • 1
  • 14
  • 15
  • 1
    Does this work for queues? The documentation says "Remember, these events are fired when the mail is being sent, not when it is queued.", what i want to do is record when the queue triggers the email. – Guilherme do Valle Jan 18 '21 at 11:55