2
votes

I try to send email to user using templates. So,before the email sent it should check that the user received email on that day or not. It's like One user should received one mail only in a day so i add if statement which is

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

after i add this line the mail is not sending can anyone help me on this. Im new to laravel.

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

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


                if( $user->created_at < Carbon::now()->subDays($mail->days)){  //check when the acc create

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

                        if (EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->count()< 1){  //sent email one time only

                            if (EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->whereDate('created_at', '>', Carbon::now()->addDays(30))) { //mail sent again after 30 days  

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

                            }  
                        }
                    }
                }
            }
          }
1
Instead of EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->count()< 1 you can use a email_status column in your database and set its value to 0. Once mail sent to that user change the value of that column to 1. And in your query you can fetch all users where email_status = 0Sehdev
so i will create column in EmailSave Table is it @SehdevNavanitha Moorthy
yes, then you can check your email sent status from that column.Sehdev
how i will do the checking status for that can you tell me more detail? @SehdevNavanitha Moorthy

1 Answers

2
votes

Wondering if you have already setup your email credential at .env, if not you can try to use mailtrap.io for email sandbox.

Register your mailtrap account at https://mailtrap.io/ , then go to SMTP Setting tab to get your username and password.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=ENTER_YOUR_MAILTRAP_USERNAME_AT_SMTP_SETTING_PAGE(should be 14 characters long)
MAIL_PASSWORD=ENTER_YOUR_MAILTRAP_PASSWORD_AT_SMTP_SETTING_PAGE(should be 14 characters long)

There is a logic error to correct, try this kind of codes if this meets your requirement.

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

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


                if( $user->created_at < Carbon::now()->subDays($mail->days)){  //check when the acc create

                    $ableToSendMail = false;
                    if (!EmailSave::where('user_id',$user_id)->whereDate('created_at', Carbon::today())->exists()) {

                        // looks like this logic need to break down into two separate checking cause the requirement is different
                        // one is email to send one time only when there is everyday checking
                        // another one will breach first checking, which see the created_at date which more than 30 days

//                        if (EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->count()< 1){  //sent email one time only
//
//                            if (EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->whereDate('created_at', '>', Carbon::now()->addDays(30))) { //mail sent again after 30 days  
//
//                                $mails = new EmailSave;
//                                $mails->user_id = $user->id;
//                                $mails->email_id =$mail->id;
//                                Mail::to($user->email)->send(new Automail($mail));
//                                $mails->save();
//
//                            }
//                        }

                        // Email does not sent before, proceed to email send
                        if (EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->count()< 1){  //sent email one time only
                            $ableToSendMail = true;
                        }


                        if (EmailSave::where('email_id',$email_id)->where('user_id',$user_id)->whereDate('created_at', '>', Carbon::now()->addDays(30))) { //mail sent again after 30 days
                            $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();
                    }
                }
            }
        }