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