0

我在 \app\commands\ 创建了 cronjobcommand.php

public function fire()  {
  $dataArray=tb1::select();
  $dataArray->get();
  foreach($dataArray as $show){
    Mail::send('emails.test', $show, function($message){
        $message->to($show['user_address'], $show['email_content']);
        $message->subject($show['email_subject']);
    });
  }
}

我的观点 test.blade.php 如下:

<!DOCTYPE html>
<html lang="en-US">
<head><meta charset="utf-8"></head>
<body>
  <h2>This is testing email. {{{  $dataArray->email_content }}}</h2>
  <div>This is testing email.</div>
</body>
</html>

当我删除所有变量并对电子邮件地址、内容和主题进行硬编码时,它就会起作用。但是,当我将一些变量传递到 Mail::send 部分时,它会失败.....

请帮忙

4

3 回答 3

0

问题是该$show变量在回调中不可用,它仅在您的邮件 html 模板中可用。

尝试这个;

public function fire()  {
    // Assuming this is the database query
    $dataArray=tb1::select()->get();

    foreach($dataArray as $show){
        $mailData = $show->toArray();

        Mail::send('emails.test', $mailData, function($message) use($show){
            $message->to($show->user_address, $show->email_content);
            $message->subject($show->email_subject);
        });
    }
}
于 2016-04-22T06:19:26.757 回答
0

您可以尝试记录$show变量以确保它确实有一些数据然后粘贴日志。尝试这个;

foreach($dataArray as $show) {
    dd($show);

    ... //omitted the rest of the code for brevity
}
于 2016-04-22T07:34:27.590 回答
0
$users = DB::table('table')->get();
foreach($dataArray as $show){
Mail::send('emails.test', array('FIELD1'=>$user->FIELD1, 
                                'FIELD2'=>$user->FIELD2),
           function($message) use ($show) {
 $message->to($show->user_address, $show->email_content)
         ->subject($show->email_subject);
});
于 2016-04-28T11:28:30.620 回答