|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\HeloLaravel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Mail\Mailer as MailerContract; |
|
6
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
7
|
|
|
use Illuminate\Mail\Mailable; |
|
8
|
|
|
use Illuminate\Mail\Mailer as LaravelMailer; |
|
9
|
|
|
use Illuminate\Support\Facades\View; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
|
12
|
|
|
use Symfony\Component\VarDumper\Dumper\HtmlDumper; |
|
13
|
|
|
|
|
14
|
|
|
class Mailer extends LaravelMailer implements MailerContract |
|
15
|
|
|
{ |
|
16
|
|
|
public function send($view, array $data = [], $callback = null) |
|
17
|
|
|
{ |
|
18
|
|
|
if ($view instanceof Mailable |
|
19
|
|
|
&& ! $view instanceof ShouldQueue |
|
20
|
|
|
) { |
|
21
|
|
|
$this->applyDebugHeaders($view); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
parent::send($view, $data, $callback); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function applyDebugHeaders(Mailable $mailable) |
|
28
|
|
|
{ |
|
29
|
|
|
$mailable->withSwiftMessage(function (\Swift_Message $swiftMessage) use ($mailable) { |
|
30
|
|
|
$viewFile = $view = $viewContent = $viewData = null; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$viewFile = $this->getMailableViewFile($mailable); |
|
33
|
|
|
|
|
34
|
|
|
if (! is_null($viewFile)) { |
|
35
|
|
|
$view = $this->getMailableView($viewFile); |
|
36
|
|
|
$viewContent = $this->getMailableViewContent($view); |
|
37
|
|
|
$viewData = $this->getMailableViewData($mailable); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* We need to base64 encode the data, as the SMTP header mime encoding could add unwanted |
|
43
|
|
|
* CLRF line breaks. |
|
44
|
|
|
*/ |
|
45
|
|
|
$headers = $swiftMessage->getHeaders(); |
|
46
|
|
|
$headers->addTextHeader('X-HELO-View', base64_encode($viewContent)); |
|
47
|
|
|
$headers->addTextHeader('X-HELO-View-File', base64_encode($viewFile)); |
|
48
|
|
|
$headers->addTextHeader('X-HELO-View-Data', base64_encode($viewData)); |
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function getMailableProperty($mailable, string $property) |
|
53
|
|
|
{ |
|
54
|
|
|
$reflection = new ReflectionClass($mailable); |
|
55
|
|
|
$property = $reflection->getProperty($property); |
|
56
|
|
|
|
|
57
|
|
|
$property->setAccessible(true); |
|
58
|
|
|
|
|
59
|
|
|
return $property->getValue($mailable); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function getMailableViewFile(Mailable $mailable) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!is_null($markdown = $this->getMailableProperty($mailable, 'markdown'))) { |
|
65
|
|
|
return $markdown; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->getMailableProperty($mailable, 'view'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function getMailableView(string $viewFile) |
|
72
|
|
|
{ |
|
73
|
|
|
return View::make($viewFile); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function getMailableViewContent($view) |
|
77
|
|
|
{ |
|
78
|
|
|
return file_get_contents($view->getPath()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function getMailableViewData(Mailable $mailable) |
|
82
|
|
|
{ |
|
83
|
|
|
$dumper = new HtmlDumper(); |
|
84
|
|
|
$cloner = new VarCloner(); |
|
85
|
|
|
$clonedData = $cloner->cloneVar($mailable->buildViewData()); |
|
86
|
|
|
|
|
87
|
|
|
return $dumper->dump($clonedData, true, [ |
|
88
|
|
|
'maxDepth' => 3, |
|
89
|
|
|
'maxStringLength' => 160, |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.