簡體   English   中英

僅在會議中注冊用戶的電子郵件時發送電子郵件通知

[英]Send notification for an email only if its an email of an user registered in the conference

我有一個頁面,用戶可以在其中發送會議中注冊用戶的通知。 在此頁面中,用戶可以選擇想要向會議中注冊的所有與會者發送通知。 但是,用戶還可以選擇他要發送給會議中注冊的特定用戶的電子郵件。 我有下面的代碼來實現這一目標。

在特定會議中注冊的所有與會者的通知都可以正常工作。

問題在於發送用戶在“ participant_email”表單字段中引入的特定電子郵件的通知。 問題是該電子郵件已發送到任何電子郵件,但通知應僅發送給在會議中注冊的用戶,否則應顯示錯誤。

你知道如何實現嗎? 在下面的代碼適用於所有參與者的情況下,即,如果用戶發送了所有參與者的通知,並且沒有注冊任何參與者,則不會發送該通知,並且顯示為“會議中沒有任何參與者注冊”。 。 但是,如果用戶正在發送特定電子郵件的通知,而不是會議中注冊用戶的電子郵件,該如何實現呢?

public function send(Request $request, $id){

    $conference = Conference::find($id);

    $message = $request->message;
    $subject = $request->subject;
     // if the user selected that wants to send email for a specific participant of the conference
    if($request->send_to == "participant"){
        $this->validate(request(), [
            'participant_email' => 'required|email|exists:users,email',                                             
        ]);

        Mail::to($request->participant_email)->send(new Notification($conference, $message, $subject));

        Session::flash('success', 'Notification sent.');

        return redirect()->back();
    }
    // if the user selected that wants to send an email for all participants of the conference
    if($request->send_to == "all"){

        $sendTo = User::whereHas('registrations', function ($query) use($id) {
            $query->where('conference_id', '=', $id);
        })->get();
    }else{
       $sendTo = User::whereHas('registrations.participants.registration_type', function ($query) use ($id, $request) {
              $query->where('id', '=', $request->send_to)
                 ->where('conference_id', '=', $id);
              })->whereHas('registrations', function ($query) use ($id) {
                  $query->where('conference_id', '=', $id);
              })->get(); 
    }

    foreach($sendTo as $user){
        $usersEmail[] = $user->email;
    }

    if(isset($usersEmail)) {
        foreach ($usersEmail as $userEmail) {
            Mail::to($userEmail)->send(new Notification($conference, $message, $subject));
        }
        Session::flash('success', 'Notification sent with success.');
        return redirect()->back();

    }
    else{
        Session::flash('no_participants', 'There are no participants registered in the conference.');
        return redirect()->back();
    }
}

更新的代碼:

class NotificationController extends Controller
{
    public function index($id){

        $conference = Conference::find($id);
        $registrationType = RegistrationType::where('conference_id', $id)->get();


        return view('notifications.index')->with('conference', $conference)->with('registrationType', $registrationType);
    }

    public function send(Request $request, $id){


        $conference = Conference::find($id);


        $this->validate(request(), [
            'send_to' => 'required',
            'subject' => 'required',
            'message' => 'required' // The message field is required.
        ]);


        $message = $request->message;
        $subject = $request->subject;

        if($request->send_to == "participant"){
            $this->validate(request(), $this->participantRules($id));
            $emails[] = $request->participant_email;
        }

        else if($request->send_to == "all"){
            $emails = User::whereHas('registrations', function ($query) use($id) {
                $query->where('conference_id', '=', $id);
            })->pluck('email');
        }


        else{
            $emails = User::whereHas('registrations.participants.registration_type', function ($query) use ($id, $request) {
                $query->where('id', '=', $request->send_to)
                    ->where('conference_id', '=', $id);
            })->whereHas('registrations', function ($query) use ($id) {
                $query->where('conference_id', '=', $id);
            })->get(); // use pluck('email') instead of get to select only email

        }

        if(count($emails) > 0) {
            $this->sendNotification($emails, $conference, $request);
            Session::flash('success', 'Notificação enviada com sucesso.');
            return redirect()->back();
        }else{
            Session::flash('no_participants', 'There are no participants registered in the conference.');
            return redirect()->back();
        }
    }

    protected function participantRules($conferenceID){
        return [
            'email' => [
                'required',
                'email',
                Rule::exists('users')->where(function ($query) use ($conferenceID) {
                    $query->whereHas('registrations',  function ($query) use($conferenceID) {
                        $query->where('conference_id', '=', $conferenceID);
                    });
                }),
            ],
        ];
    }

    protected function sendNotification(array $emails, $conference, $request){
        foreach ($emails as $userEmail) {
            Mail::to($userEmail)->send(new Notification($conference, $request->message, $request->subject));
        }

        Session::flash('success', 'Notificaiton sent with success.');
        return redirect()->back();
    }

}

您可以像這樣向exists驗證添加額外的過濾器

public function send(Request $request, $id){

    $conference = Conference::find($id);

    $message = $request->message;
    $subject = $request->subject;
    $emails = []; 

    if($request->send_to == "participant"){
        $this->validate(request(), $this->participantRules($id));

        $emails = User::whereHas('registrations', function ($query) use($id) {
                       $query->where('conference_id', '=', $id);
                    })->where('email', $request->participant_email)->pluck('email');

    }else if($request->send_to == "all"){
        $emails = User::whereHas('registrations', function ($query) use($id) {
            $query->where('conference_id', '=', $id);
        })->pluck('email');
    }else{
        $emails = User::whereHas('registrations.participants.registration_type', function ($query) use ($id, $request) {
            $query->where('id', '=', $request->send_to)
                  ->where('conference_id', '=', $id);
            })->whereHas('registrations', function ($query) use ($id) {
               $query->where('conference_id', '=', $id);
            })->pluck('email'); 

    }

    if(count($emails) > 0) {
        $this->sendNotification($emails, $conference, $request);
        Session::flash('success', 'Notification sent with success.');
        return redirect()->back();
    }else{
        Session::flash('no_participants', 'There are no participants registered in the conference.');
        return redirect()->back();
    }
}

驗證規則

protected function participantRules($conferenceId){
    return [
        'participant_email' => 'required|email'
    ];
}

發送通知

protected function sendNotification($emails, $conference, $request){
    foreach ($emails as $userEmail) {
        Mail::to($userEmail)->send(new Notification($conference, $request->message, $request->subject));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM