从 MySQL 数据库访问电子邮件以用于发送电子邮件

Accessing email from MySQL db to be use for sending email

我使用 email.Why 的消息通知有一个问题,它在 foreach 中生成错误。 错误是这部分 "$all_ngo" Undefined variable: all_ngo.

$pend = AddRequest::where('ship_id','=',$ship_id)->get();   
$all_ngo = [];
foreach ($pend as $id) {
    array_push($all_ngo, $id->ngo_id);
}

$orga_email  =  Auth::User()->orgainfo->orga_email;
$staffName  =   Auth::User()->orgainfo->inchargelname.' '. Auth::User()->orgainfo->inchargefname;  
$name = $scholars->scholar_fname.' '.$scholars->scholar_mname.' '.$scholars->scholar_lname;
$input =  array(
    'name' => $staffName,
    'email' => $orga_email,  
    'msgs' => 'asd' .' '. $name.'. '.'Hoping for your favorable response. Thank you!'
);
Mail::send('emails.mailMessage', $input,  function($message){
    $message->from('Somename@gmail.com');
    foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
        $user = User::find($id3);
        $ngo_email2 = $user->ngo_email;
        $message->to($ngo_email2)->subject('Request For Sponsorship');
    }   
});

因为 $all_ngo 超出范围。您可以通过在函数中添加 global $all_ngo; 来解决此问题:

Mail::send('emails.mailMessage', $input,  function($message) {
  global $all_ngo;

  $message->from('Somename@gmail.com');
  foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
    $user = User::find($id3);
    $ngo_email2 = $user->ngo_email;
    $message->to($ngo_email2)->subject('Request For Sponsorship');
  }   
});

或者允许匿名函数访问变量:

Mail::send('emails.mailMessage', $input,  function($message) use ($all_ngo) {
  $message->from('Somename@gmail.com');
  foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
    $user = User::find($id3);
    $ngo_email2 = $user->ngo_email;
    $message->to($ngo_email2)->subject('Request For Sponsorship');
  }   
});