如何在 Laravel 和 MailTrap 中发送电子邮件通知
How to send email notification in Laravel and MailTrap
我正在尝试使用此方法向 MailTrap 发送一个 HTML 模板
public function send($result_id)
{
$result = Result::whereHas('user', function ($query) {
$query->whereId(auth()->id());
})->findOrFail($result_id);
\Mail::to('test@eam.com')->send(new ResultMail);
return view('client.result', compact('result'))->withStatus('Your test result has been sent successfully!');
}
与 result.blade.file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test No. {{ $result->id }}</title>
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
<style type="text/css">
html {
margin: 0;
}
body {
background-color: #FFFFFF;
font-size: 10px;
margin: 36pt;
}
</style>
</head>
<body>
<p class="mt-5">Total points: {{ $result->total_points }} points</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Question Text</th>
<th>Points</th>
</tr>
</thead>
<tbody>
@foreach($result->questions as $question)
<tr>
<td>{{ $question->question_text }}</td>
<td>{{ $question->pivot->points }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
但是我收到一个错误
Undefined variable: result (View: C:\Users\USER\Documents\Laravel-Test-Result-PDF-master\Laravel-Test-Result-PDF-master\resources\views\client\result.blade.php)
好吧,据我所知,您需要 Mailable class 并且从 mailable class,您需要 return 视图并在那里传递数据。
你可以邮寄 class 应该
class ResultMail extends Mailable
{
use Queueable, SerializesModels;
public $result;
/**
* Create a new message instance.
*
*/
public function __construct($result)
{
$this->result = $result;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('client.result');
}
}
应该是 this.And 那么你需要将数据传递给 ResultMail
\Mail::to('test@eam.com')->send(new ResultMail($result));
我正在尝试使用此方法向 MailTrap 发送一个 HTML 模板
public function send($result_id)
{
$result = Result::whereHas('user', function ($query) {
$query->whereId(auth()->id());
})->findOrFail($result_id);
\Mail::to('test@eam.com')->send(new ResultMail);
return view('client.result', compact('result'))->withStatus('Your test result has been sent successfully!');
}
与 result.blade.file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test No. {{ $result->id }}</title>
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
<style type="text/css">
html {
margin: 0;
}
body {
background-color: #FFFFFF;
font-size: 10px;
margin: 36pt;
}
</style>
</head>
<body>
<p class="mt-5">Total points: {{ $result->total_points }} points</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Question Text</th>
<th>Points</th>
</tr>
</thead>
<tbody>
@foreach($result->questions as $question)
<tr>
<td>{{ $question->question_text }}</td>
<td>{{ $question->pivot->points }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
但是我收到一个错误
Undefined variable: result (View: C:\Users\USER\Documents\Laravel-Test-Result-PDF-master\Laravel-Test-Result-PDF-master\resources\views\client\result.blade.php)
好吧,据我所知,您需要 Mailable class 并且从 mailable class,您需要 return 视图并在那里传递数据。 你可以邮寄 class 应该
class ResultMail extends Mailable
{
use Queueable, SerializesModels;
public $result;
/**
* Create a new message instance.
*
*/
public function __construct($result)
{
$this->result = $result;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('client.result');
}
}
应该是 this.And 那么你需要将数据传递给 ResultMail
\Mail::to('test@eam.com')->send(new ResultMail($result));