Skip to content

Commit f852cc4

Browse files
committed
feature #58264 [Mailer] Support region in sendgrid bridge (MrYamous)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [Mailer] Support region in sendgrid bridge | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #58230 | License | MIT Still TODO - [ ] Fix failing test Commits ------- 2b6c459c55 [Mailer] Support region in sendgrid bridge
2 parents db97851 + bd0ff06 commit f852cc4

7 files changed

+33
-11
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Add support for region in DSN
8+
49
6.4
510
---
611

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ Configuration example:
77

88
```env
99
# SMTP
10-
MAILER_DSN=sendgrid+smtp://KEY@default
10+
MAILER_DSN=sendgrid+smtp://KEY@default?region=REGION
1111
1212
# API
13-
MAILER_DSN=sendgrid+api://KEY@default
13+
MAILER_DSN=sendgrid+api://KEY@default?region=REGION
1414
```
1515

1616
where:
1717
- `KEY` is your Sendgrid API Key
18-
18+
- `REGION` is Sendgrid selected region (default to global)
1919

2020
Webhook
2121
-------

Tests/Transport/SendgridApiTransportTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static function getTransportData()
3939
new SendgridApiTransport('KEY'),
4040
'sendgrid+api://api.sendgrid.com',
4141
],
42+
[
43+
new SendgridApiTransport('KEY', null, null, null, 'eu'),
44+
'sendgrid+api://api.eu.sendgrid.com',
45+
],
4246
[
4347
(new SendgridApiTransport('KEY'))->setHost('example.com'),
4448
'sendgrid+api://example.com',

Tests/Transport/SendgridTransportFactoryTest.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,22 @@ public static function supportsProvider(): iterable
6060

6161
public static function createProvider(): iterable
6262
{
63+
$client = new MockHttpClient();
6364
$logger = new NullLogger();
6465

6566
yield [
6667
new Dsn('sendgrid+api', 'default', self::USER),
67-
new SendgridApiTransport(self::USER, new MockHttpClient(), null, $logger),
68+
new SendgridApiTransport(self::USER, $client, null, $logger),
69+
];
70+
71+
yield [
72+
new Dsn('sendgrid+api', 'default', self::USER, '', null, ['region' => 'eu']),
73+
new SendgridApiTransport(self::USER, $client, null, $logger, 'eu'),
6874
];
6975

7076
yield [
7177
new Dsn('sendgrid+api', 'example.com', self::USER, '', 8080),
72-
(new SendgridApiTransport(self::USER, new MockHttpClient(), null, $logger))->setHost('example.com')->setPort(8080),
78+
(new SendgridApiTransport(self::USER, $client, null, $logger))->setHost('example.com')->setPort(8080),
7379
];
7480

7581
yield [

Transport/SendgridApiTransport.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
*/
3333
class SendgridApiTransport extends AbstractApiTransport
3434
{
35-
private const HOST = 'api.sendgrid.com';
35+
private const HOST = 'api.%region_dot%sendgrid.com';
3636

3737
public function __construct(
3838
#[\SensitiveParameter] private string $key,
3939
?HttpClientInterface $client = null,
4040
?EventDispatcherInterface $dispatcher = null,
4141
?LoggerInterface $logger = null,
42+
private ?string $region = null
4243
) {
4344
parent::__construct($client, $dispatcher, $logger);
4445
}
@@ -190,6 +191,11 @@ private function getAttachments(Email $email): array
190191

191192
private function getEndpoint(): ?string
192193
{
193-
return ($this->host ?: self::HOST).($this->port ? ':'.$this->port : '');
194+
$host = $this->host ?: str_replace('%region_dot%', '', self::HOST);
195+
if (null !== $this->region && null === $this->host) {
196+
$host = str_replace('%region_dot%', $this->region.'.', self::HOST);
197+
}
198+
199+
return $host.($this->port ? ':'.$this->port : '');
194200
}
195201
}

Transport/SendgridSmtpTransport.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
*/
2121
class SendgridSmtpTransport extends EsmtpTransport
2222
{
23-
public function __construct(#[\SensitiveParameter] string $key, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null)
23+
public function __construct(#[\SensitiveParameter] string $key, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, private ?string $region = null)
2424
{
25-
parent::__construct('smtp.sendgrid.net', 465, true, $dispatcher, $logger);
25+
parent::__construct('null' !== $region ? \sprintf('smtp.%s.sendgrid.net', $region) : 'smtp.sendgrid.net', 465, true, $dispatcher, $logger);
2626

2727
$this->setUsername('apikey');
2828
$this->setPassword($key);

Transport/SendgridTransportFactory.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ public function create(Dsn $dsn): TransportInterface
2525
{
2626
$scheme = $dsn->getScheme();
2727
$key = $this->getUser($dsn);
28+
$region = $dsn->getOption('region');
2829

2930
if ('sendgrid+api' === $scheme) {
3031
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
3132
$port = $dsn->getPort();
3233

33-
return (new SendgridApiTransport($key, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
34+
return (new SendgridApiTransport($key, $this->client, $this->dispatcher, $this->logger, $region))->setHost($host)->setPort($port);
3435
}
3536

3637
if ('sendgrid+smtp' === $scheme || 'sendgrid+smtps' === $scheme || 'sendgrid' === $scheme) {
37-
return new SendgridSmtpTransport($key, $this->dispatcher, $this->logger);
38+
return new SendgridSmtpTransport($key, $this->dispatcher, $this->logger, $region);
3839
}
3940

4041
throw new UnsupportedSchemeException($dsn, 'sendgrid', $this->getSupportedSchemes());

0 commit comments

Comments
 (0)