Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Laravel中发送电子邮件通知之前,请检查用户设置_Laravel_Email_Notifications - Fatal编程技术网

在Laravel中发送电子邮件通知之前,请检查用户设置

在Laravel中发送电子邮件通知之前,请检查用户设置,laravel,email,notifications,Laravel,Email,Notifications,我在Laravel应用程序的用户模型中有一个receiveEmail布尔字段。如何确保邮件通知尊重此字段,并且仅在该字段为真时才向用户发送电子邮件 我想要的是这个代码: $event = new SomeEvent($somedata); Auth::user()->notify($event); 其中SomeEvent是一个扩展通知并在via()方法上实现“mail”的类,仅当用户允许发送电子邮件时才发送电子邮件。尝试在用户模型中创建这样的新方法 用户模型文件 public funct

我在Laravel应用程序的用户模型中有一个
receiveEmail
布尔字段。如何确保邮件通知尊重此字段,并且仅在该字段为真时才向用户发送电子邮件

我想要的是这个代码:

$event = new SomeEvent($somedata);
Auth::user()->notify($event);

其中SomeEvent是一个扩展通知并在
via()
方法上实现“mail”的类,仅当用户允许发送电子邮件时才发送电子邮件。

尝试在用户模型中创建这样的新方法

用户模型文件

public function scopeNotifyMail() {
        if($this->receiveEmail == true) { //if field is enable email other wise not send..
            $event = new SomeEvent($somedata);
            $this->notify($event);
        }    
}
现在在控制器中这样调用

Auth::user()->notifyMail();


我最终创建了一个新的通道来实现检查。在应用程序/频道中,添加您的频道,如下所示:

namespace-App\Channels;
使用App\User;
使用Illumb\Notifications\Channels\MailChannel;
使用照明\通知\通知;
使用照明\支持\ Arr;
类UserCheckMailChannel扩展了MailChannel
{
/**
*发送给定的通知。
*
*@param混合$应呈报
*@param\Lightning\Notifications\Notification$Notification
*@返回无效
*/
公共函数发送($notifiable,Notification$Notification)
{
//检查用户是否应该收到电子邮件。在这里做任何需要的检查。
if($notifiable instanceof User&!$notifiable->receiveemail){
返回;
}
//是的,转换为邮件并发送
$message=$notification->toMail($notifiable);
如果(!$消息){
返回;
}
父项::发送($notifiable,$notification);
}
}
然后将
Providers/AppServiceProvider.php上的类绑定到旧邮件类:

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

        $this->app->bind(
            \Illuminate\Notifications\Channels\MailChannel::class,
            UserCheckMailChannel::class
        );
    }

让任何人通过()方法检查此选项:


我希望这在向多个用户发送通知时也能起作用。谢谢

这取决于您的电子邮件逻辑。您是直接向电子邮件发送电子邮件,还是先查询与该电子邮件匹配的用户?您需要在这里提供一些代码;我们帮不了什么忙。我有几件事,一件也没有。它将向用户模型中添加大量方法,并且不会在类似于
Notification::send($users$event)
的方式中处理用户集合;
App\User::where('id',1)->first()->notifyMail();
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()

        $this->app->bind(
            \Illuminate\Notifications\Channels\MailChannel::class,
            UserCheckMailChannel::class
        );
    }
public function via($notifiable)
{
        // $notifiable object is User instance for most cases
        $wantsEmail = $notifiable->settings['wants_email']; // your own logic
        if(!$wantsEmail){
            // no email only database log
            return ['database'];
        }
        return ['database', 'mail'];
}