gpt4 book ai didi

php - Laravel : email send multiples times to one user

转载 作者:行者123 更新时间:2023-12-04 10:36:01 25 4
gpt4 key购买 nike

我想一次向许多用户发送电子邮件,但在这种情况下,邮件本身向一个用户发送了多次。

尝试只给每个人发送一次电子邮件(不要向用户发送垃圾邮件)
它对这种方法不起作用,任何人都可以再次帮助这件事。

    public function create() 
{
$users = User::where('user_type', 2)->get();
$auto_email_templates = AutoEmailTemplate::all();

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

if( $user->created_at < Carbon::now()->subDays($mail->days)){

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

public function create()
{
$users = User::where('user_type', 2)->get();
$auto_email_templates=AutoEmailTemplate::all();


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


if( $user->created_at < Carbon::now()->subDays($mail->days)){

if (EmailSave::where('email_id', '=', Input::get('email_id'))->exists()) {
Mail::to($user->email)->send(new Automail($mail));
}
else {
return false;
}

$mail = new EmailSave;
$mail->user_id = $user->id;
$mail->email_id =$mail->id;
$mail->save();

}

最佳答案

因为您使用的是嵌套的 foreach 循环,这就是您面临此问题的原因。如果您想将每个模板发送给每个用户,那么您可以简单地交换循环,例如:

public function create() 
{
$users = User::where('user_type', 2)->get();
$auto_email_templates=AutoEmailTemplate::all();



foreach($auto_email_templates as $mail){
foreach ($users as $user) { // add this to here

if( $user->created_at < Carbon::now()->subDays($mail->days)){

Mail::to($user->email)->send(new Automail($mail));


$mail = new EmailSave;
$mail->user_id = $user->id;
$mail->email_id =$mail->id;
$mail->save();

}
}

希望这会有所帮助!

关于php - Laravel : email send multiples times to one user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60180813/

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