Skip to content

Commit 3c7b4fa

Browse files
committed
Build mailable before extracting data from it
1 parent c70df73 commit 3c7b4fa

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/MailableReader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use function call_user_func_array;
7+
use Illuminate\Container\Container;
78

89
class MailableReader
910
{
@@ -14,6 +15,8 @@ class MailableReader
1415
*/
1516
public function read(EmailComposer $composer)
1617
{
18+
Container::getInstance()->call([$composer->getData('mailable'), 'build']);
19+
1720
$this->readRecipient($composer);
1821

1922
$this->readFrom($composer);
@@ -123,7 +126,9 @@ private function readBody(EmailComposer $composer)
123126

124127
$composer->setData('view', '');
125128

126-
$composer->setData('body', $composer->getData('mailable')->render());
129+
$mailable = $composer->getData('mailable');
130+
131+
$composer->setData('body', view($mailable->view, $mailable->buildViewData()));
127132
}
128133

129134
/**

tests/MailableReaderTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function it_extracts_the_recipient()
2020
(new TestMailable())->to(['[email protected]'])
2121
);
2222

23-
$this->assertEquals(['[email protected]', '[email protected]'], $composer->getData('recipient'));
23+
$this->assertCount(2, $composer->getData('recipient'));
24+
$this->assertContains('[email protected]', $composer->getData('recipient'));
25+
$this->assertContains('[email protected]', $composer->getData('recipient'));
2426
}
2527

2628
/** @test */
@@ -107,29 +109,20 @@ public function it_extracts_the_from_address_and_or_name()
107109
class TestMailable extends Mailable
108110
{
109111
/**
110-
* Create a new message instance.
112+
* Build the message.
111113
*
112-
* @return void
114+
* @return $this
113115
*/
114-
public function __construct()
116+
public function build()
115117
{
116-
$this->to('[email protected]')
118+
return $this->to('[email protected]')
117119
118120
119121
->subject('Your order has shipped!')
120122
->attach(__DIR__ . '/files/pdf-sample.pdf', [
121123
'mime' => 'application/pdf',
122124
])
123-
->attachData('<p>Thanks for your oder</p>', 'order.html');
124-
}
125-
126-
/**
127-
* Build the message.
128-
*
129-
* @return $this
130-
*/
131-
public function build()
132-
{
133-
return $this->view('tests::dummy', ['name' => 'John Doe']);
125+
->attachData('<p>Thanks for your oder</p>', 'order.html')
126+
->view('tests::dummy', ['name' => 'John Doe']);
134127
}
135128
}

0 commit comments

Comments
 (0)