How to create global rules for when to send an email

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to create global rules for when to send an email



Currently in our system I send emails by using the laravel mailables like so



Mail::to($user)->send(new AccountCreation($user))


Mail::to($user)->send(new AccountCreation($user))



This works as intended however in our system we have some complex global rules for when to send an email to a person that should be used everywhere, for example:


if ($user->isActive)
Mail::to($user)->send(new AccountCreation($user));



I do not want to check the user every time and would rather the mailable logic handle this.



Is there a clean way to handle global rules for when to send an email?





Duplicate of stackoverflow.com/questions/39647329/…
– hdifen
Aug 10 at 21:38




1 Answer
1



Make a mail() function in the User model and insert your global rules for all users. So instead of calling:


Mail::to($user)->send(new AccountCreation($user))



You'll be calling:


$user->mail(new AccountCreation($user))



You can make your mail function as follows:


class User extends Model
(...)
public function mail(Mailer $mailer)
if ($this->isActive)
Mail::to($this)->send($mailer);


(...)





Hi Andre. Thanks for your answer however looking at the link I commented it seems overwriting the the MailServiceProvider is a more elegant way to handle this problem.
– hdifen
Aug 13 at 15:05


MailServiceProvider





Hi @hdifen I don't agree with you. As a rule that only addresses the User model, it should be inside it.
– André Piva
Aug 15 at 4:30



User






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard