gpt4 book ai didi

Laravel:电子邮件未发送

转载 作者:行者123 更新时间:2023-12-04 10:34:02 24 4
gpt4 key购买 nike

我尝试使用模板向用户发送电子邮件。因此,在发送电子邮件之前,应检查用户是否在当天收到电子邮件。就像一个用户应该只在一天内收到一封邮件,所以我添加了 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();

}
}
}
}
}
}

最佳答案

想知道您是否已经在 .env 中设置了电子邮件凭证,如果没有,您可以尝试使用 mailtrap.io 进行电子邮件沙箱。

https://mailtrap.io/ 注册您的 mailtrap 帐户,然后转到 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();
}
}
}
}

关于Laravel:电子邮件未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60274748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com