Skip to content

[Mailer] Sendgrid API Transport: allow use of mail_settings #42915

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

Open
wants to merge 15 commits into
base: 7.4
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Sendgrid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.2
---

* Add support for the `Envelope`'s options to the Sendgrid API transport

5.4
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,21 @@ public function testTagAndMetadataHeaders()
$this->assertSame('blue', $payload['personalizations'][0]['custom_args']['Color']);
$this->assertSame('12345', $payload['personalizations'][0]['custom_args']['Client-ID']);
}

public function testEnvelopeOptions()
{
$expectedMailerSettings = ['bypass_unsubscribe_management' => ['enable' => true]];

$email = new Email();
$envelope = new Envelope(new Address('envelopefrom@example.com'), [new Address('envelopeto@example.com')]);
$envelope->setOption('mail_settings', $expectedMailerSettings);

$transport = new SendgridApiTransport('ACCESS_KEY');
$method = new \ReflectionMethod(SendgridApiTransport::class, 'getPayload');
$method->setAccessible(true);
$payload = $method->invoke($transport, $email, $envelope);

$this->assertArrayHasKey('mail_settings', $payload);
$this->assertSame($payload['mail_settings'], $expectedMailerSettings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ private function getPayload(Email $email, Envelope $envelope): array
$personalization['custom_args'] = $customArguments;
}

foreach ($envelope->getOptions() as $name => $value) {
$payload[$name] = $value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows overriding any pre-existing payload key. This might be too risky.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do this instead?

Suggested change
$payload[$name] = $value;
if (!array_key_exists($name, $payload) {
$payload[$name] = $value;
}

}

$payload['personalizations'][] = $personalization;

return $payload;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.2
---

* Add the `options` property to `Envelope`

6.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6.2

---

Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Mailer/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Envelope
{
private Address $sender;
private array $recipients = [];
private array $options = [];

/**
* @param Address[] $recipients
Expand Down Expand Up @@ -85,4 +86,20 @@ public function getRecipients(): array
{
return $this->recipients;
}

/**
* @param mixed $value
*/
public function setOption(string $name, $value): void
{
$this->options[$name] = $value;
}

/**
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->options;
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/Mailer/Tests/EnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,12 @@ public function testFromRawMessages()

Envelope::create(new RawMessage('Some raw email message'));
}

public function testSettingOptions()
{
$e = Envelope::create(new Message());
$e->setOption('setting_name1', 'setting_value1');
$e->setOption('setting_name2', 'setting_value2');
$this->assertEquals(['setting_name1' => 'setting_value1', 'setting_name2' => 'setting_value2'], $e->getOptions());
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy