diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 48831c0e551a..b1ac21358009 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -352,7 +352,19 @@ public function buildViewData() } } - return $data; + return array_merge($data, $this->additionalMessageData()); + } + + /** + * Get additional meta-data to pass along with the view data. + * + * @return array + */ + protected function additionalMessageData(): array + { + return [ + '__laravel_mailable' => get_class($this), + ]; } /** diff --git a/tests/Mail/MailMailableDataTest.php b/tests/Mail/MailMailableDataTest.php index 0aa0aa21e3b9..01737705b033 100644 --- a/tests/Mail/MailMailableDataTest.php +++ b/tests/Mail/MailMailableDataTest.php @@ -9,9 +9,13 @@ class MailMailableDataTest extends TestCase { public function testMailableDataIsNotLost() { - $testData = ['first_name' => 'James']; - $mailable = new MailableStub; + + $testData = [ + 'first_name' => 'James', + '__laravel_mailable' => get_class($mailable), + ]; + $mailable->build(function ($m) use ($testData) { $m->view('view', $testData); }); diff --git a/tests/Mail/MailMailableTest.php b/tests/Mail/MailMailableTest.php index 9c21f8b06dc8..f610ec80262e 100644 --- a/tests/Mail/MailMailableTest.php +++ b/tests/Mail/MailMailableTest.php @@ -592,6 +592,7 @@ public function testMailableBuildsViewData() 'first_name' => 'Taylor', 'lastName' => 'Otwell', 'framework' => 'Laravel', + '__laravel_mailable' => get_class($mailable), ]; $this->assertSame($expected, $mailable->buildViewData());