【问题标题】:How to send email notification in Laravel and MailTrap如何在 Laravel 和 MailTrap 中发送电子邮件通知
【发布时间】:2020-08-26 07:47:39
【问题描述】:

我正在尝试使用此方法将 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 )

【问题讨论】:

    标签: php laravel mailtrap


    【解决方案1】:

    好吧,据我所知,您需要有 Mailable 类,并且从 mailable 类中,您需要返回视图并在那里传递数据。 你的可邮寄课程应该

    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));
    

    【讨论】:

    • 可以用这个方法获取$user->email吗?
    • 您需要在构造函数public function __construct($result, $user) 中传递用户,然后将其分配给公共属性,然后您就可以在刀片文件中使用它。
    猜你喜欢
    • 2017-07-16
    • 2020-02-16
    • 2021-03-27
    • 2020-08-14
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    相关资源
    最近更新 更多