[英]how to send custom email notifications with laravel?
我想將帖子標題和正文從數據庫發送給 email 中的所有用戶並發送電子郵件,但默認文本顯示在 email 中。 數據庫中的標題未在 email 中發送。
通知 Class
class RepliedToThread extends Notification
{
use Queueable;
public $event;
public function __construct($event)
{
$this->event=$event;
}
public function via($notifiable)
{
return ['database','broadcast', 'mail'];
}
public function toDatabase($notifiable)
{
return[
'event'=>$this->event,
'user'=>auth()->user()
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'event'=>$this->event,
'user'=>auth()->user()
]);
}
public function toMail($notifiable)
{
return new MailMessage([
'data' => [
'event'=>$this->event,
'user'=>auth()->user()
]
]);
}
public function toArray($notifiable)
{
return [
//
];
}
}
Controller 代碼:
public function store(Request $request)
{
$this->validate($request, [
'title'=>'required',
'body'=>'required',
'color' =>'required',
'start_date' =>'required',
'end_date' =>'required',
]);
$event = new Event;
$event->title = $request->title;
$event->body = $request->body;
$event->color = $request->color;
$event->start_date = $request->start_date;
$event->end_date = $request->end_date;
$event->save();
$user = User::where('id', '!=', auth()->user()->id)->get();
\Notification::send($user,new RepliedToThread(Event::latest('id')->first()));
return redirect()->route('events.index');
}
電子郵件已發送,但默認文本顯示在 email 中,數據庫中的帖子標題未在 email 中發送。
我想將帖子標題和正文從數據庫發送給 email 中的所有用戶。
Laravel 通知
在 Laravel 通知中:
public function __construct(array $event)
{
$this->event = $event;
}
public function toMail($event)
{
return (new MailMessage)
->line($this->event->title)
->line($this->event->body);
}
在 Controller 中:
Notification::send($user,new RepliedToThread($event));
通知布局模板存儲在這里/vendor/laravel/framework/src/Illuminate/Notifications/resources/views
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.