Skip to content

Commit 40c50bd

Browse files
committed
feat: support headers
1 parent ecef43d commit 40c50bd

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

packages/mailer/src/EmailToSymfonyEmailMapper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Symfony\Component\Mime\Address as SymfonyAddress;
88
use Symfony\Component\Mime\Email as SymfonyEmail;
9+
use Symfony\Component\Mime\Header\Headers;
910
use Tempest\Mail\Exceptions\MissingExpeditorAddressException;
1011
use Tempest\Mail\Exceptions\MissingRecipientAddressException;
1112
use Tempest\Mapper\Mapper;
@@ -37,6 +38,10 @@ public function map(mixed $from, mixed $to): SymfonyEmail
3738
$email = $from;
3839
$symfonyEmail = new SymfonyEmail();
3940

41+
foreach ($email->envelope->headers as $key => $value) {
42+
$symfonyEmail->getHeaders()->addHeader($key, $value);
43+
}
44+
4045
if ($email->envelope->from) {
4146
$symfonyEmail->from(...$this->convertAddresses($email->envelope->from));
4247
} elseif ($this->mailerConfig->from) {

packages/mailer/src/Envelope.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function __construct(
1414
public null|string|array|Address $cc = null,
1515
public null|string|array|Address $bcc = null,
1616
public null|string|array|Address $replyTo = null,
17+
public array $headers = [],
1718
public Priority $priority = Priority::NORMAL,
1819
) {}
1920
}

packages/mailer/src/Testing/SentTestingEmail.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function assertSubjectContains(string $expect): self
4747
{
4848
Assert::assertStringContainsString(
4949
needle: $expect,
50-
haystack: $this->symfonyEmail->getSubject(),
50+
haystack: $this->symfonyEmail->getSubject() ?? '',
5151
message: "Failed asserting that the email's subject is `{$expect}`.",
5252
);
5353

@@ -279,6 +279,30 @@ public function assertAttached(string $filename, ?\Closure $callback = null): se
279279
return $this;
280280
}
281281

282+
/**
283+
* Asserts that the email has a header with the given name.
284+
*/
285+
public function assertHasHeader(string $header, ?string $value = null): self
286+
{
287+
$headers = Arr\to_array($this->symfonyEmail->getHeaders()->all());
288+
289+
Assert::assertArrayHasKey(
290+
key: mb_strtolower($header),
291+
array: $headers,
292+
message: sprintf('Failed asserting that the email has a header `%s`.', $header),
293+
);
294+
295+
if ($value !== null) {
296+
Assert::assertSame(
297+
expected: $value,
298+
actual: $headers[mb_strtolower($header)]->getBodyAsString(),
299+
message: sprintf('Failed asserting that the email has a header `%s` with value `%s`.', $header, $value),
300+
);
301+
}
302+
303+
return $this;
304+
}
305+
282306
private function assertAddressListContains(null|string|array|Address $haystack, string|array $needles, string $message): self
283307
{
284308
$needles = Arr\wrap($needles);

tests/Integration/Mailer/SentEmailTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ private function sendTestEmail(?Envelope $envelope = null, ?Content $content = n
2626
2727
2828
29+
headers: ['X-Foo' => 'bar'],
2930
),
3031
content: $content ?? new Content(
3132
text: 'Hello Jon in Text',
@@ -70,6 +71,9 @@ public function test_sent_email_assertions(): void
7071
$sent->assertNotFrom('[email protected]');
7172

7273
$sent->assertPriority(Priority::NORMAL);
74+
75+
$sent->assertHasHeader('X-Foo');
76+
$sent->assertHasHeader('X-Foo', 'bar');
7377
}
7478

7579
public function test_send_to_address_vo(): void

0 commit comments

Comments
 (0)