Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow queueing Mails by default #54271

Draft
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class Mailer implements MailerContract, MailQueueContract
*/
protected $queue;

/**
* Indicates if mails should be queued by default.
*
* @var bool
*/
protected $queueMailsByDefault = false;

/**
* Create a new Mailer instance.
*
Expand Down Expand Up @@ -349,7 +356,7 @@ public function send($view, array $data = [], $callback = null)
*/
protected function sendMailable(MailableContract $mailable)
{
return $mailable instanceof ShouldQueue
return ($mailable instanceof ShouldQueue || $this->queueMailsByDefault)
? $mailable->mailer($this->name)->queue($this->queue)
: $mailable->mailer($this->name)->send($this);
}
Expand Down Expand Up @@ -461,6 +468,17 @@ protected function setGlobalToAndRemoveCcAndBcc($message)
$message->forgetBcc();
}

/**
* Indicate that all mailables should be queued by default.
*
* @param bool $queue
* @return void
*/
public function queueMailsByDefault(bool $shouldQueue = true)
{
$this->queueMailsByDefault = $shouldQueue;
}

/**
* Queue a new mail message for sending.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* @method static bool hasSent(string $mailable)
* @method static \Illuminate\Support\Collection queued(string|\Closure $mailable, callable|null $callback = null)
* @method static bool hasQueued(string $mailable)
* @method static void queueMailsByDefault(bool $shouldQueue = true)
*
* @see \Illuminate\Mail\MailManager
* @see \Illuminate\Support\Testing\Fakes\MailFake
Expand Down
20 changes: 19 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/MailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ class MailFake implements Factory, Fake, Mailer, MailQueue
*/
protected $queuedMailables = [];

/**
* Indicates if mails should be queued by default.
*
* @var bool
*/
protected $queueMailsByDefault = false;

/**
* Create a new mail fake.
*
Expand Down Expand Up @@ -476,7 +483,7 @@ public function raw($text, $callback)
*/
public function send($view, array $data = [], $callback = null)
{
return $this->sendMail($view, $view instanceof ShouldQueue);
return $this->sendMail($view, ($view instanceof ShouldQueue || $this->queueMailsByDefault));
}

/**
Expand Down Expand Up @@ -516,6 +523,17 @@ protected function sendMail($view, $shouldQueue = false)
$this->mailables[] = $view;
}

/**
* Indicate that all mailables should be queued by default.
*
* @param bool $queue
* @return void
*/
public function queueMailsByDefault(bool $shouldQueue = true)
{
$this->queueMailsByDefault = $shouldQueue;
}

/**
* Queue a new message for sending.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Support/SupportTestingMailFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ public function testSendQueuesAMailableThatShouldBeQueued()
}
}

public function testSendQueuesAMailableThatShouldBeQueuedByDefault()
{
$this->fake->queueMailsByDefault();

$this->fake->to('[email protected]')->send($this->mailable);

$this->fake->assertQueued(MailableStub::class);

try {
$this->fake->assertSent(MailableStub::class);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\MailableStub] mailable was not sent.', $e->getMessage());
}
}

public function testAssertNothingSent()
{
$this->fake->assertNothingSent();
Expand Down
Loading