Skip to content

Commit f2bfbed

Browse files
authored
[9.x] Make Illuminate\Mail\Mailable macroable (#40631)
This PR makes the \Illuminate\Mail\Mailable class macroable. A simple use-case is abstracting the options array when having multiple Mailables that you want to attach raw pdf data to: ```php // Current: Mail::send((new MyMail())->attachData($pdfData, 'filename.pdf', [ 'mime' => 'application/pdf', ])); // Proposal: Mailable::macro('attachPdfData', function ($data, $name) { return $this->attachData($data, $name, [ 'mime' => 'application/pdf', ]); }); Mail::send((new MyMail())->attachPdfData($pdfData, 'filename.pdf')); ```
1 parent 63cb622 commit f2bfbed

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
use Illuminate\Support\Traits\Conditionable;
1616
use Illuminate\Support\Traits\ForwardsCalls;
1717
use Illuminate\Support\Traits\Localizable;
18+
use Illuminate\Support\Traits\Macroable;
1819
use Illuminate\Testing\Constraints\SeeInOrder;
1920
use PHPUnit\Framework\Assert as PHPUnit;
2021
use ReflectionClass;
2122
use ReflectionProperty;
2223

2324
class Mailable implements MailableContract, Renderable
2425
{
25-
use Conditionable, ForwardsCalls, Localizable;
26+
use Conditionable, ForwardsCalls, Localizable, Macroable {
27+
__call as macroCall;
28+
}
2629

2730
/**
2831
* The locale of the message.
@@ -1053,6 +1056,10 @@ public static function buildViewDataUsing(callable $callback)
10531056
*/
10541057
public function __call($method, $parameters)
10551058
{
1059+
if (static::hasMacro($method)) {
1060+
return $this->macroCall($method, $parameters);
1061+
}
1062+
10561063
if (Str::startsWith($method, 'with')) {
10571064
return $this->with(Str::camel(substr($method, 4)), $parameters[0]);
10581065
}

0 commit comments

Comments
 (0)