The first step is to modify the configuration file:

MAIL_DRIVER= SMTP MAIL_HOST=smtp.exmail.qq.com MAIL_PORT=465 MAIL_USERNAME= mailbox address MAIL_PASSWORD= mailbox authorization code MAIL_ENCRYPTION= SSL MAIL_FROM_ADDRESS= MAIL_FROM_NAME= mail_name (self-defined) The SMTP driver used here is Tencent enterprise mailbox, can be changed according to the actual situation

The second step is to generate the mail class. All the mailing class configuration is done in the build method, which calls From, Subject, View, and Attach to configure the content of the mail and send it

php artisan make:mail AlarmsMail

Third, edit the mail class

<? php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class AlarmsMail extends Mailable implements ShouldQueue { use Queueable, SerializesModels; private $err; /** * Create a new message instance. * * @return void */ public function __construct($e) { $data = [ 'msg' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile(), 'line' => $e->getLine(), ]; $this->err = $data; $this->subject = "XXXX alert "; } /** * Build the message. * * @return $this */ public function build() { return $this->view('mail.alarms', $this->err); }}

Step 4, create the view

<! DOCTYPE html> <html> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <head> <style> .list-group { padding-left: 0; margin-bottom: 20px; } .list-group-item { position: relative; display: block; padding: 10px 15px; margin-bottom: -1px; background-color: #fff; border: 1px solid #ddd; } .code { color: red; } .col-md-12 { width: 100%; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <div class="list-group"> <span class="list-group-item code" {{$code}}</span class="list-group-item code"} {{$file}}</span> <span class="list-group-item code"> <span class="list-group-item code"> <span class="list-group-item code"> {{$line}}</span> </div> </div> </div> </div> </body> </html>

Step 5, call

use Illuminate\Support\Facades\Mail; use App\Mail\AlarmsMail; Try {throw new \Exception(" I am an Exception, I am an Exception "); } catch (\Exception $e){ Mail::to(config('services.alarms_receive_mail'))->send(new AlarmsMail($e)); }

Results:

Thus a simple version of the abnormal alarm email has completed, there are a lot of configuration can operation, such as sending plain text, add accessories, can also be copied and send to the recipient, specific can consult document https://learnku.com/docs/lara…