Laravel gửi lỗi người dùng đến Email

post-title

Trong thực tế khi bạn đưa sản phẩm của mình chạy production thì chúng ta sẽ tắt debug đi bảo mật. Vì vậy khi báo lỗi chỉ trả về 500. Vì vậy mà bạn khó có thể phán đoán được là lỗi gì. Cũng như user phải báo lại cho bạn. Ở bài viết trước mình đã gửi qua Slack hôm nay mình sẽ làm để gửi qua email.

Trong thực tế khi bạn đưa sản phẩm của mình chạy production thì chúng ta sẽ tắt debug đi bảo mật. Vì vậy khi báo lỗi chỉ trả về 500. Vì vậy mà bạn khó có thể phán đoán được là lỗi gì. Cũng như user phải báo lại cho bạn. Ở bài viết trước mình đã gửi qua Slack hôm nay mình sẽ làm để gửi qua email. Bạn có thể đọc lại bài viết trước ở đây https://nghiane.com/laravel-gui-loi-nguoi-dung-den-slack

Đây là 1 nội dung thông báo lỗi được gửi qua email của mình

laravel-huong-dan-gui-thong-bao-loi-qua-email-min


 

Tạo gửi email bằng make:mail command

 php artisan make:mail ExceptionOccured

Sau khi tạo xong bạn chỉnh sửa nội dung file vừa được tạo như sau:

namespace App\Mail;

use Illuminate\Bus\Queueable; 
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ExceptionOccured extends Mailable 
{
    use Queueable, SerializesModels;

    public $content;


    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->content = $content;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
         return $this->view('emails.exception')
                    ->with('content', $this->content);
    }
}

Tạo View cho nội dung email

Tiếp theo bạn vào resouces/views/emails  tạo file với tên là  exception.blade.php

Nội dung file file đó đơn giản như sau:

{!! $content !!}

Gửi nội dung email lỗi ở file Handle.php

Các bạn vào app →  Exceptions → Handler.php và mở file này lên chúng ta sẽ chỉnh sửa ở đây

và thêm 2 function sau vào:

 public function report(Throwable $exception)
 {
         // emails.exception is the template of your email
         // it will have access to the $error that we are passing below
        if ($this->shouldReport($exception)) {
             $this->sendEmail($exception); // sends an email
        }
        return parent::report($exception);

}
public function sendEmail(Throwable $exception)
{
       try {
            $e = FlattenException::create($exception);
            $handler = new HtmlErrorRenderer(true); // boolean, true raises debug flag...
            $css = $handler->getStylesheet();
            $content = $handler->getBody($e);

            \Mail::send('emails.exception', compact('css','content'), function ($message) {
                $message->to(['youremail1@gmail.com','youremail2@gmail.com'])
                                    ->subject('Exception: ' . \Request::fullUrl());
            });
        } catch (Throwable $exception) {
            Log::error($exception);
        }
}

Bạn thay thế   $message->to(['youremail1@gmail.com','youremail2@gmail.com'])  bằng các email của bạn mà muốn gửi đến đẻ báo lỗi.

Oke giờ các bạn có thể thử xem nhé! Chúc các bạn thành công!

Nguồi bài viết tham khảo: https://www.elitechsystems.com/laravel-7-8-send-error-exceptions-on-email/

 

 

icon Subscribe

to Our Newsletter