Skip to content
Advertisement

Laravel : why mail sending to all modules?

I’m trying to send mail to user to used the module that provided. Actually the mail is sending to the user but its sending all the modules. I want the mail should sent by referring the module_id and last_used_module from the AutoEmailTemplate to the specfic user. How to do that. Can i anyone help with this. Im new laravel.

public function create() 
    {
        $users = User::where('user_type', 2)->get();
        $auto_email_templates=AutoEmailTemplate::all();
        $ModuleAutoMail=module_auto_mail::all();

        foreach($auto_email_templates as $mail) {
        foreach ($users as $user) {

               // foreach($ModuleAutoMail as $mod) {

                $email_id = $mail->id;
                $user_id = $user->id;
                $module_name = $mail->module_name;


                if (!EmailSave::where('user_id',$user_id)->where('email_id',$email_id)->whereDate('created_at', Carbon::today())->exists()) {
                    if($mail->condition_id=='1'){ 

                            if(is_null($user->last_sign_in)){

                                $ableToSendMail = true;
                            // break;
                            if ((EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->count()< 1)) {
                                $ableToSendMail = true;
                            }

                            if ($ableToSendMail) { //saving and sent email

                                $mails = new EmailSave;
                                $mails->user_id = $user->id;
                                $mails->email_id =$mail->id;
                                Mail::to($user->email)->send(new Automail($mail));
                                $mails->save();
                            }
                       }
                    }
                    elseif ($mail->condition_id=='2'){

                        $last_sign_in = Carbon::parse($user->last_sign_in);
                        $DeferenceInDays = Carbon::parse(Carbon::now())->diffInDays($last_sign_in);

                        if($DeferenceInDays > 7){
                            $ableToSendMail = true;


                            if ((EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->count()< 1)){  

                                $ableToSendMail = true;

                            }    
                            if ($ableToSendMail) {
                                $mails = new EmailSave;
                                $mails->user_id = $user->id;
                                $mails->email_id =$mail->id;
                                Mail::to($user->email)->send(new Automail($mail));
                                $mails->save();
                            }
                        }
                    }
                    else { 
                        foreach($ModuleAutoMail as $module) {
                            $module_id=$module->module_id;

                            if($mail->condition_id=='3' ){   

                                $last_used_module = Carbon::parse($module->last_used_module);
                                $DeferenceInDays = Carbon::parse(Carbon::now())->diffInDays($last_used_module);

                                AutoEmailTemplate::whereNotNull('module_name')->get(); 
                                module_auto_mail::where('module_id')->get();

                                if($module_name == $module_id){

                                    if(is_null($module->last_used_module)){ 

                                        $ableToSendMail = false;

                                    }

                                    elseif ($DeferenceInDays > 7) {
                                        $ableToSendMail = false;
                                    }
                                    else {
                                        if ((EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->count()< 1)){  

                                            $ableToSendMail = false;
                                        }
                                    }

                                        if ($ableToSendMail) {

                                            $mails = new EmailSave;
                                            $mails->user_id = $user->id;
                                            $mails->email_id =$mail->id;
                                            Mail::to($user->email)->send(new Automail($mail));
                                            $mails->save();
                                        }
                                   }     
                                }
                            } 
                        }          
                    }
                }
            }
        }

Advertisement

Answer

try this i have changed look carefully

public function create() 
    {
        $users = User::where('user_type', 2)->get();
        $auto_email_templates = AutoEmailTemplate::all();
        $ModuleAutoMail=module_auto_mail::all();

        foreach($auto_email_templates as $mail) {
            foreach ($users as $user) {

                    $ableToSendMail  = false;
                    $email_id = $mail->id;
                    $user_id = $user->id;
                    $module_name = $mail->module_name;
                    $emailCheck = EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->count();

                    if (!EmailSave::where('user_id',$user_id)->where('email_id',$email_id)->whereDate('created_at', Carbon::today())->exists()) {

                        if($mail->condition_id=='1'){ 

                            if(is_null($user->last_sign_in)){

                                $ableToSendMail = true;

                                //this condition no need here - hamelraj
                                if ($emailCheck < 1) {
                                    $ableToSendMail = true;
                                }
                            }
                        }
                        elseif ($mail->condition_id=='2'){

                            $last_sign_in = Carbon::parse($user->last_sign_in);
                            $DeferenceInDays = Carbon::parse(Carbon::now())->diffInDays($last_sign_in);

                            if($DeferenceInDays > 7){
                                $ableToSendMail = true;

                                //this condition no need here - or you have to merge above if like  if($DeferenceInDays > 7 && $emailCheck < 1)
                                if ($emailCheck < 1){  
                                    $ableToSendMail = true;
                                } 
                            }
                        }
                        else { 
                            foreach($ModuleAutoMail as $module) {
                                $module_id=$module->module_id;

                                if($mail->condition_id=='3' ){   

                                    $last_used_module = Carbon::parse($module->last_used_module);
                                    $DeferenceInDays = Carbon::parse(Carbon::now())->diffInDays($last_used_module);

                                    //why this ??
                                    //AutoEmailTemplate::whereNotNull('module_name')->get(); 
                                    //module_auto_mail::where('module_id')->get();

                                    if($module_name == $module_id){

                                        if(is_null($module->last_used_module)){ 
                                            $ableToSendMail = false;
                                        }
                                        elseif ($DeferenceInDays > 7) {
                                            $ableToSendMail = false;
                                        }
                                        else {
                                            if ($emailCheck< 1){  
                                                $ableToSendMail = false;
                                            }
                                        }
                                    }     
                                    }
                                } 
                            }          
                        }

                        if ($ableToSendMail) { //saving and sent email

                            $mails = new EmailSave;
                            $mails->user_id = $user->id;
                            $mails->email_id =$mail->id;
                            Mail::to($user->email)->send(new Automail($mail));
                            $mails->save();
                        }
                    }
            }
        }
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement