Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29745

Laravel : why mail sending to all modules?

$
0
0

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();
                                        }
                                   }     
                                }
                            } 
                        }          
                    }
                }
            }
        }

Viewing all articles
Browse latest Browse all 29745

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>