我在\ app \ commands \
创建了cronjobcommand.phppublic 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 .....
部分时,它会失败请帮忙
答案 0 :(得分: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);
});
}
}
答案 1 :(得分:0)
您是否可以尝试记录$show
变量,以确保它实际上有一些数据然后粘贴日志。试试这个;
foreach($dataArray as $show) {
dd($show);
... //omitted the rest of the code for brevity
}
答案 2 :(得分: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);
});