Skip to content

Commit bd0ff06

Browse files
MrYamousfabpot
authored andcommitted
[Mailer] Support region in sendgrid bridge
1 parent d61e049 commit bd0ff06

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
@@ -57,16 +57,22 @@ public static function supportsProvider(): iterable
5757

5858
public static function createProvider(): iterable
5959
{
60+
$client = new MockHttpClient();
6061
$logger = new NullLogger();
6162

6263
yield [
6364
new Dsn('sendgrid+api', 'default', self::USER),
64-
new SendgridApiTransport(self::USER, new MockHttpClient(), null, $logger),
65+
new SendgridApiTransport(self::USER, $client, null, $logger),
66+
];
67+
68+
yield [
69+
new Dsn('sendgrid+api', 'default', self::USER, '', null, ['region' => 'eu']),
70+
new SendgridApiTransport(self::USER, $client, null, $logger, 'eu'),
6571
];
6672

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

7278
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)