Skip to content

[Mailer] Add additional headers in Scaleway bridge #57499

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

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public function testSend()
$this->assertSame('attachment.txt', $body['attachments'][0]['name']);
$this->assertSame('text/plain', $body['attachments'][0]['type']);
$this->assertSame(base64_encode('some attachment'), $body['attachments'][0]['content']);
$this->assertSame('Reply-To', $body['additional_headers'][0]['key']);
$this->assertStringContainsString('foo@bar.fr', $body['additional_headers'][0]['value']);

return new JsonMockResponse(['emails' => [['message_id' => 'foobar']]], [
'http_code' => 200,
Expand All @@ -81,6 +83,7 @@ public function testSend()
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->replyTo(new Address('foo@bar.fr', 'Foo'))
->text('Hello There!')
->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ private function getPayload(Email $email, Envelope $envelope): array
if ($attachements = $this->prepareAttachments($email)) {
$payload['attachments'] = $attachements;
}
if ($headers = $this->getCustomHeaders($email)) {
$payload['additional_headers'] = $headers;
}

return $payload;
}
Expand All @@ -122,6 +125,24 @@ private function prepareAttachments(Email $email): array
return $attachments;
}

private function getCustomHeaders(Email $email): array
{
$headers = [];
$headersToBypass = ['from', 'to', 'cc', 'bcc', 'subject', 'content-type', 'sender'];
foreach ($email->getHeaders()->all() as $name => $header) {
if (\in_array($name, $headersToBypass, true)) {
continue;
}

$headers[] = [
'key' => $header->getName(),
'value' => $header->getBodyAsString(),
];
}

return $headers;
}

private function formatAddress(Address $address): array
{
$array = ['email' => $address->getAddress()];
Expand Down
Loading