【问题标题】:Accessing email from MySQL db to be use for sending email从 MySQL db 访问电子邮件以用于发送电子邮件
【发布时间】:2017-08-02 12:24:23
【问题描述】:

我使用电子邮件的消息通知出现问题。为什么它会在 foreach 中生成错误。 错误是这部分“$all_ngo”未定义变量: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');
    }   
});

【问题讨论】:

    标签: php email foreach laravel-5.1


    【解决方案1】:

    因为$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');
      }   
    });
    

    【讨论】:

    • 先生,谢谢你,现在我使用第二个选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2015-06-07
    • 1970-01-01
    • 2016-04-17
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多