Laravel:电子邮件未发送

时间:2020-02-18 06:09:16

标签: laravel email swiftmailer

我尝试使用模板向用户发送电子邮件。因此,在发送电子邮件之前,应检查用户当天是否收到了电子邮件。就像一个用户一天只应该收到一封邮件,所以我添加了if语句

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

在我添加此行之后,邮件未发送,任何人都可以帮我解决这个问题。我是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 个答案:

答案 0 :(得分:2)

想知道是否已经在.env上设置了电子邮件凭据,如果没有,则可以尝试将mailtrap.io用于电子邮件沙箱。

https://mailtrap.io/注册您的邮件陷阱帐户,然后转到SMTP设置标签以获取用户名和密码。

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)

存在逻辑错误要纠正,如果符合您的要求,请尝试这种代码。

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