目前,在我们的系统中,我使用laravel邮件发送电子邮件,如下所示
Mail::to($user)->send(new AccountCreation($user))
然而,在我们的系统中,对于何时发送电子邮件给一个人,我们有一些复杂的全局规则,这些规则应该在任何地方都使用,例如:
if ($user->isActive) {
Mail::to($user)->send(new AccountCreation($user));
}
我不想每次都检查用户,而是更愿意使用mailable逻辑来处理这个问题。
有没有一种干净的方法来处理何时发送电子邮件的全球规则?
发布于 2018-08-11 05:57:36
在用户模型中创建mail()函数,并为所有用户插入您的全局规则。因此,与其调用:
Mail::to($user)->send(new AccountCreation($user))
你会打给我:
$user->mail(new AccountCreation($user))
你可以让你的邮件按如下方式工作:
class User extends Model {
(...)
public function mail(Mailer $mailer) {
if ($this->isActive) {
Mail::to($this)->send($mailer);
}
}
(...)
}
https://stackoverflow.com/questions/51794159
复制相似问题