How to globally exclude certain email addresses from the mailer? #33751
-
How can I exclude certain email addresses from the Laravel mailer? Scenario I'm working with is when emails are marked as bounced/complaint and should be excluded in future emails. I'd like to handle this automatically where the mailer will exclude those emails for me. Something like: Mail::setGlobalExclusions(function () {
return arrayOfEmailAddressesToExclude();
}); And then if I were to use the public function to($users)
{
return (new PendingMail($this))->to(
$this->removeExclusions($users)
);
} Is this currently achievable in Laravel somehow? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Just had an idea that maybe I could listen for the |
Beta Was this translation helpful? Give feedback.
-
If you control every place you send emails, you could just write a central wrapper for your Sound probably too simple otherwise I guess you would have done that 🤔 😏 |
Beta Was this translation helpful? Give feedback.
-
Think I reached an okay solution: Registered a replacement class MailServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->singleton('mail.manager', function ($app) {
return new MailManager($app);
});
}
} And then |
Beta Was this translation helpful? Give feedback.
Think I reached an okay solution:
Registered a replacement
MailManager
in my ownMailServiceProvider
:And then
MailManager
just extends the framework base, overriding theresolve
method to use a customMailer
class to adds the exclusion logic.