We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d61e049 commit bd0ff06Copy full SHA for bd0ff06
CHANGELOG.md
@@ -1,6 +1,11 @@
1
CHANGELOG
2
=========
3
4
+7.2
5
+---
6
+
7
+ * Add support for region in DSN
8
9
6.4
10
---
11
README.md
@@ -7,15 +7,15 @@ Configuration example:
```env
# SMTP
-MAILER_DSN=sendgrid+smtp://KEY@default
+MAILER_DSN=sendgrid+smtp://KEY@default?region=REGION
12
# API
13
-MAILER_DSN=sendgrid+api://KEY@default
+MAILER_DSN=sendgrid+api://KEY@default?region=REGION
14
```
15
16
where:
17
- `KEY` is your Sendgrid API Key
18
-
+ - `REGION` is Sendgrid selected region (default to global)
19
20
Webhook
21
-------
Tests/Transport/SendgridApiTransportTest.php
@@ -39,6 +39,10 @@ public static function getTransportData()
39
new SendgridApiTransport('KEY'),
40
'sendgrid+api://api.sendgrid.com',
41
],
42
+ [
43
+ new SendgridApiTransport('KEY', null, null, null, 'eu'),
44
+ 'sendgrid+api://api.eu.sendgrid.com',
45
+ ],
46
[
47
(new SendgridApiTransport('KEY'))->setHost('example.com'),
48
'sendgrid+api://example.com',
Tests/Transport/SendgridTransportFactoryTest.php
@@ -57,16 +57,22 @@ public static function supportsProvider(): iterable
57
58
public static function createProvider(): iterable
59
{
60
+ $client = new MockHttpClient();
61
$logger = new NullLogger();
62
63
yield [
64
new Dsn('sendgrid+api', 'default', self::USER),
- 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'),
71
];
72
73
74
new Dsn('sendgrid+api', 'example.com', self::USER, '', 8080),
- (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),
76
77
78
Transport/SendgridApiTransport.php
@@ -32,13 +32,14 @@
32
*/
33
class SendgridApiTransport extends AbstractApiTransport
34
35
- private const HOST = 'api.sendgrid.com';
+ private const HOST = 'api.%region_dot%sendgrid.com';
36
37
public function __construct(
38
#[\SensitiveParameter] private string $key,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
?LoggerInterface $logger = null,
+ private ?string $region = null
) {
parent::__construct($client, $dispatcher, $logger);
}
@@ -190,6 +191,11 @@ private function getAttachments(Email $email): array
190
191
192
private function getEndpoint(): ?string
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 : '');
200
201
Transport/SendgridSmtpTransport.php
@@ -20,9 +20,9 @@
class SendgridSmtpTransport extends EsmtpTransport
22
23
- public function __construct(#[\SensitiveParameter] string $key, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null)
+ public function __construct(#[\SensitiveParameter] string $key, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, private ?string $region = null)
24
25
- parent::__construct('smtp.sendgrid.net', 465, true, $dispatcher, $logger);
+ parent::__construct('null' !== $region ? \sprintf('smtp.%s.sendgrid.net', $region) : 'smtp.sendgrid.net', 465, true, $dispatcher, $logger);
26
27
$this->setUsername('apikey');
28
$this->setPassword($key);
Transport/SendgridTransportFactory.php
@@ -25,16 +25,17 @@ public function create(Dsn $dsn): TransportInterface
$scheme = $dsn->getScheme();
$key = $this->getUser($dsn);
+ $region = $dsn->getOption('region');
29
30
if ('sendgrid+api' === $scheme) {
31
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();
- return (new SendgridApiTransport($key, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
+ return (new SendgridApiTransport($key, $this->client, $this->dispatcher, $this->logger, $region))->setHost($host)->setPort($port);
if ('sendgrid+smtp' === $scheme || 'sendgrid+smtps' === $scheme || 'sendgrid' === $scheme) {
- return new SendgridSmtpTransport($key, $this->dispatcher, $this->logger);
+ return new SendgridSmtpTransport($key, $this->dispatcher, $this->logger, $region);
throw new UnsupportedSchemeException($dsn, 'sendgrid', $this->getSupportedSchemes());
0 commit comments