1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kevincobain2000\LaravelAlertNotifications\Mail; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use Illuminate\Mail\Mailable; |
7
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
8
|
|
|
use Illuminate\Queue\SerializesModels; |
9
|
|
|
|
10
|
|
|
class ExceptionOccurredMail extends Mailable |
11
|
|
|
{ |
12
|
|
|
use InteractsWithQueue, Queueable, SerializesModels; |
|
|
|
|
13
|
|
|
|
14
|
|
|
public $exception; |
15
|
|
|
public $exceptionContext; |
16
|
|
|
|
17
|
|
|
public $view = 'vendor.laravel_alert_notifications.mail'; |
18
|
|
|
|
19
|
|
|
protected $notificationLevel; |
20
|
|
|
|
21
|
|
|
public function __construct($exception, string $notificationLevel, array $exceptionContext = []) |
22
|
|
|
{ |
23
|
|
|
$this->exception = $exception; |
24
|
|
|
$this->exceptionContext = $exceptionContext; |
25
|
|
|
$this->notificationLevel = $notificationLevel; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function build() |
29
|
|
|
{ |
30
|
|
|
$configPrefix = 'laravel_alert_notifications.mail.'.$this->notificationLevel.'.'; |
31
|
|
|
|
32
|
|
|
$from = config('laravel_alert_notifications.mail.fromAddress'); |
33
|
|
|
$to = config($configPrefix.'toAddress') ?? config('laravel_alert_notifications.mail.toAddress'); |
34
|
|
|
$subject = config($configPrefix.'subject') ?? config('laravel_alert_notifications.mail.subject'); |
35
|
|
|
$subject = $this->replaceSubjectPlaceholders($subject); |
36
|
|
|
|
37
|
|
|
$data = [ |
38
|
|
|
'exception' => $this->exception, |
39
|
|
|
'context' => $this->exceptionContext, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
return $this->subject($subject)->from($from)->to($to)->with($data); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function replaceSubjectPlaceholders(string $subject): string |
46
|
|
|
{ |
47
|
|
|
$subject = str_replace('%ExceptionType%', get_class($this->exception), $subject); |
48
|
|
|
$subject = str_replace('%ExceptionMessage%', $this->exception->getMessage(), $subject); |
49
|
|
|
$subject = str_replace('%ExceptionCode%', $this->exception->getCode(), $subject); |
50
|
|
|
$subject = str_replace('%ExceptionLevel%', ucfirst($this->notificationLevel), $subject); |
51
|
|
|
|
52
|
|
|
return $subject; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|