laravel4.2 将参数传递给电子邮件 blade
laravel4.2 pass parameter to email blade
我在 \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.....
的部分时,它失败了
请帮忙
问题是 $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);
});
}
}
您能否尝试记录 $show
变量以确保它确实有一些数据,然后粘贴日志。试试这个;
foreach($dataArray as $show) {
dd($show);
... //omitted the rest of the code for brevity
}
$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);
});
我在 \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.....
的部分时,它失败了请帮忙
问题是 $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);
});
}
}
您能否尝试记录 $show
变量以确保它确实有一些数据,然后粘贴日志。试试这个;
foreach($dataArray as $show) {
dd($show);
... //omitted the rest of the code for brevity
}
$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);
});