From 181c1d6d3f386adecdda08c5e28e09feb2399969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20M=2E?= Date: Mon, 9 Dec 2024 11:55:46 +0100 Subject: [PATCH 1/3] Allow passing fake --- src/Concerns/UsesMailcoachMail.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Concerns/UsesMailcoachMail.php b/src/Concerns/UsesMailcoachMail.php index a501046..f29aa6f 100644 --- a/src/Concerns/UsesMailcoachMail.php +++ b/src/Concerns/UsesMailcoachMail.php @@ -2,6 +2,7 @@ namespace Spatie\MailcoachMailer\Concerns; +use Spatie\MailcoachMailer\Headers\FakeHeader; use Spatie\MailcoachMailer\Headers\MailerHeader; use Spatie\MailcoachMailer\Headers\ReplacementHeader; use Spatie\MailcoachMailer\Headers\TransactionalMailHeader; @@ -12,7 +13,7 @@ trait UsesMailcoachMail { private bool $usingMailcoachMail = false; - public function mailcoachMail(string $mailName, array $replacements = [], ?string $mailer = null): self + public function mailcoachMail(string $mailName, array $replacements = [], ?string $mailer = null, ?bool $fake = null): self { $this->usingMailcoachMail = true; @@ -20,6 +21,7 @@ public function mailcoachMail(string $mailName, array $replacements = [], ?strin $this->replacing($replacements); $this->usingMailer($mailer); + $this->faking($fake); $this->withSymfonyMessage(function (Email $email) use ($mailName) { $transactionalHeader = new TransactionalMailHeader($mailName); @@ -70,6 +72,25 @@ public function replacing(array|string $key, string|array|null $value = null): s return $this; } + public function faking(?bool $value): self + { + if (! $value) { + return $this; + } + + $this->withSymfonyMessage(function (Email $email) use ($value) { + $fakeHeader = new FakeHeader($value); + + if ($email->getHeaders()->has($fakeHeader->getName())) { + $email->getHeaders()->remove($fakeHeader->getName()); + } + + $email->getHeaders()->add($fakeHeader); + }); + + return $this; + } + protected function buildSubject($message): self { if (! $this->usingMailcoachMail) { From 0f952303611f4e28f8b60d8c8d2ce5f95b740cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20M=2E?= Date: Mon, 9 Dec 2024 11:59:43 +0100 Subject: [PATCH 2/3] Add Fake header support --- src/Notifications/MailcoachMessage.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Notifications/MailcoachMessage.php b/src/Notifications/MailcoachMessage.php index 1ef6f44..15ffb2b 100644 --- a/src/Notifications/MailcoachMessage.php +++ b/src/Notifications/MailcoachMessage.php @@ -4,6 +4,7 @@ use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Support\Arr; +use Spatie\MailcoachMailer\Headers\FakeHeader; use Spatie\MailcoachMailer\Headers\MailerHeader; use Spatie\MailcoachMailer\Headers\ReplacementHeader; use Spatie\MailcoachMailer\Headers\TransactionalMailHeader; @@ -15,6 +16,8 @@ class MailcoachMessage extends MailMessage public array $replacements = []; + public bool $fake = false; + public function usingMail(string $mailName): self { $this->mailName = $mailName; @@ -65,4 +68,21 @@ public function replacing(array|string $key, string|array|null $value = null): s return $this; } + + public function fake(bool $value): self + { + $this->fake = $value; + + $this->withSymfonyMessage(function (Email $email) use ($value) { + $fakeHeader = new FakeHeader($mailer); + + if ($email->getHeaders()->has($fakeHeader->getName())) { + $email->getHeaders()->remove($fakeHeader->getName()); + } + + $email->getHeaders()->add($fakeHeader); + }); + + return $this; + } } From 143ee3b1e94599b4a20389151bcd9e3d285509cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20M=2E?= Date: Mon, 9 Dec 2024 12:07:33 +0100 Subject: [PATCH 3/3] Rename to faking --- src/Notifications/MailcoachMessage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Notifications/MailcoachMessage.php b/src/Notifications/MailcoachMessage.php index 15ffb2b..0523341 100644 --- a/src/Notifications/MailcoachMessage.php +++ b/src/Notifications/MailcoachMessage.php @@ -69,7 +69,7 @@ public function replacing(array|string $key, string|array|null $value = null): s return $this; } - public function fake(bool $value): self + public function faking(bool $value): self { $this->fake = $value;