簡體   English   中英

如何在 Laravel 和 MailTrap 中發送 email 通知

[英]How to send email notification in Laravel and MailTrap

我正在嘗試使用此方法將 HTML 模板發送到 MailTrap

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>

但我得到一個錯誤

未定義變量:結果(查看:C:\Users\USER\Documents\Laravel-Test-Result-PDF-master\Laravel-Test-Result-PDF-master\resources\views\client\result.blade.php)

好吧,據我所知,您需要有可郵寄的 class 和可郵寄的 class,您需要返回視圖並在那里傳遞數據。 你可以郵寄 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');
    }
}

應該是這樣的。然后你需要將數據傳遞給ResultMail

\Mail::to('test@eam.com')->send(new ResultMail($result));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM