代码之家  ›  专栏  ›  技术社区  ›  user94743

使用Laravel 4.2一次发送多封电子邮件

  •  0
  • user94743  · 技术社区  · 8 年前

    我设法做到了一次只向一个用户发送电子邮件,而不是一次向多个用户发送。虽然我已经尝试了很多方法,但下面是一个示例代码,我尝试使用它发送多封电子邮件。

     if (isset($_POST['searchbutton'])) {
            $data = $query->where('blood_type', '=', "$search")
                            ->Where('state', '=', "$state")->get();

            if (isset($POST['emailbutton'])) {
                foreach ($data as $row)
                    Mail::send('emails.notify', array('name' => 'Name'), function($message) {

                        $message->to($row->email, $row->name)->subject('Hello');
                    });
            }
        }

    

    非常感谢。

    2 回复  |  直到 8 年前
        1
  •  0
  •   har2vey    8 年前

    以下是一些更改建议,假设所有用户都将收到相同的电子邮件,这意味着您不会使用用户名自定义每个电子邮件:

    if (isset($_POST['searchbutton'])) {
      $data = $query->where('blood_type', '=', "$search")
        ->Where('state', '=', "$state")->get();
    
      $emails = $query->where('blood_type', '=', "$search")
        ->Where('state', '=', "$state")->lists('email');
    
      if (isset($POST['emailbutton'])) {
        Mail::send('emails.notify', array(), function($message) use ($emails) {
            $message->to($emails)->subject('Hello');
          });
      }
    }
    
        2
  •  0
  •   Chibueze Opata    8 年前

    这里的实际错误是变量 $row 不在您的关闭范围内。要更正此问题,您需要使用 use 关键字。即

    foreach ($data as $row){
        Mail::send('emails.notify', array('name' => 'Name'),
            function($message) use ($row) {
                $message->to($row->email, $row->name)->subject('Hello');
            }
        );
    }
    

    你可以阅读更多 about PHP closures here .