我正在发送邮件,视图来自数据库:
$blade_from_db = 'Hello, {{ $something }}!';
$email_data = ['something' => 'World!'];
Mail::send($blade_from_db, $email_data, function ($message) use ($sender)
{
$message->from($sender->from, $sender->from_safe);
$message->to($sender->to, $sender->safe);
$message->subject($sender->subject);
});
这是错误消息:
"message": "View [Hello, {{ $something }}!] not found.",
有人可以帮助我或给我一个建议,因为我需要视图的内容来自数据库。
答案 0 :(得分:1)
$blade_from_db = view('your view',['something' => 'World!'])->render();
Mail::send([], $email_data, function ($message) use ($sender, $blade_from_db)
{
$message->from($sender->from, $sender->from_safe);
$message->to($sender->to, $sender->safe);
$message->subject($sender->subject);
$message->setBody($blade_from_db,'text/html');
});
使用$message->setBody($blade_from_db,'text/html');
通过数据库发送包含模板的电子邮件。
希望这有帮助
修改强>
对于从数据库获取模板,这是我在代码中使用的代码段。
<?php
class EmailTemplate extends Model
{
use SoftDeletes;
public $table = 'email_templates';
protected $dates = ['deleted_at'];
public $fillable = [
'name',
'subject',
'content',
'variables'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'name' => 'string',
'subject' => 'string',
'content' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
// 'name' => 'required',
'subject' => 'required'
];
public function parse($data)
{
$parsed = preg_replace_callback('/{{(.*?)}}/', function ($matches) use ($data) {
list($shortCode, $index) = $matches;
if (isset($data[$index])) {
return $data[$index];
} else {
// throw new Exception("Shortcode {$shortCode} not found in template id {$this->id}", 1);
}
}, $this->content);
return $parsed;
}
}
现在在您的控制器中,您可以
$data = ['something' => 'World!'];
Mail::queue([], [], function ($m) use ($sender, $data) {
$emailTemplate = EmailTemplate::where('name', '=', 'yourtemplate')->first();
$message->from($sender->from, $sender->from_safe);
$message->to($sender->to, $sender->safe);
$message->subject($sender->subject);
$message->setBody($emailTemplate->parse($data), 'text/html');;
});
现在,您可以在数据库中使用{{something}}
来填充这些条目