Skip to content

[9.x] Allow customised implementation of the SendQueuedMailable job - #45040

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
JayBizzle:patch-1
Nov 21, 2022
Merged

[9.x] Allow customised implementation of the SendQueuedMailable job#45040
taylorotwell merged 1 commit into
laravel:9.xfrom
JayBizzle:patch-1

Conversation

@JayBizzle

Copy link
Copy Markdown
Contributor

What?

Allow us to bind a different implementation of the built-in SendQueuedMailable job.

For example...

app()->bind(\Illuminate\Mail\SendQueuedMailable::class, function($app, $params) {
    return new \App\Jobs\SendQueuedMailable($params['mailable']);
});

Why?

We have a lot of emails that we send by implementing ShouldQueue on the Mailable class. In all instances, we would like to use our own implementation of the handle() method of the built-in SendQueuedMailable job, which will enable us to handle exceptions in a more granular manner.

For example, most of our emails have a backoff() strategy and a retryUntil() value. Sometimes, we receive an exception via our 3rd party email provider which we know means the email is never going to be sent, so there is no point retrying it. We would just like to permanently fail that job at that point.

Something like...

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(MailFactory $factory)
    {
        try {
            $this->mailable->send($factory);
        } catch (\Exception $e) {
            if (Str::contains($e->getMessage(), 'Permanent failure')) {
                $this->fail();
            }
        }
    }

I know we could use our own job to do this, but it requires our team to always remember to dispatch the custom job, rather than just calling send() on the Mailable.

@taylorotwell
taylorotwell merged commit e5a0ee6 into laravel:9.x Nov 21, 2022
@JayBizzle
JayBizzle deleted the patch-1 branch November 21, 2022 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants