Skip to content

[Mailer][Sendgrid] Support region #58264

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
Oct 7, 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
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
=========

7.2
---

* Add support for region in DSN

6.4
---

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Mailer/Bridge/Sendgrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Configuration example:

```env
# SMTP
MAILER_DSN=sendgrid+smtp://KEY@default
MAILER_DSN=sendgrid+smtp://KEY@default?region=REGION

# API
MAILER_DSN=sendgrid+api://KEY@default
MAILER_DSN=sendgrid+api://KEY@default?region=REGION
```

where:
- `KEY` is your Sendgrid API Key

- `REGION` is Sendgrid selected region (default to global)

Webhook
-------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static function getTransportData()
new SendgridApiTransport('KEY'),
'sendgrid+api://api.sendgrid.com',
],
[
new SendgridApiTransport('KEY', null, null, null, 'eu'),
'sendgrid+api://api.eu.sendgrid.com',
],
[
(new SendgridApiTransport('KEY'))->setHost('example.com'),
'sendgrid+api://example.com',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,22 @@ public static function supportsProvider(): iterable

public static function createProvider(): iterable
{
$client = new MockHttpClient();
$logger = new NullLogger();

yield [
new Dsn('sendgrid+api', 'default', self::USER),
new SendgridApiTransport(self::USER, new MockHttpClient(), null, $logger),
new SendgridApiTransport(self::USER, $client, null, $logger),
];

yield [
new Dsn('sendgrid+api', 'default', self::USER, '', null, ['region' => 'eu']),
new SendgridApiTransport(self::USER, $client, null, $logger, 'eu'),
];

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

yield [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
*/
class SendgridApiTransport extends AbstractApiTransport
{
private const HOST = 'api.sendgrid.com';
private const HOST = 'api.%region_dot%sendgrid.com';

public function __construct(
#[\SensitiveParameter] private string $key,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
?LoggerInterface $logger = null,
private ?string $region = null
) {
parent::__construct($client, $dispatcher, $logger);
}
Expand Down Expand Up @@ -190,6 +191,11 @@ private function getAttachments(Email $email): array

private function getEndpoint(): ?string
{
return ($this->host ?: self::HOST).($this->port ? ':'.$this->port : '');
$host = $this->host ?: str_replace('%region_dot%', '', self::HOST);
if (null !== $this->region && null === $this->host) {
$host = str_replace('%region_dot%', $this->region.'.', self::HOST);
}

return $host.($this->port ? ':'.$this->port : '');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*/
class SendgridSmtpTransport extends EsmtpTransport
{
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)
{
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry to comment on a merged PR, but I'm not sure this works as intended: we noticed mails were not being sent after upgrading to 7.2. Our region was null at this point, but the check is performed using 'null' (as a string). So this ended up as the following error:

Connection could not be established with host "ssl://smtp..sendgrid.net:465": stream_socket_client(): php_network_getaddresses: getaddrinfo for smtp..sendgrid.net failed: Name does not resolve

After appending ?region=global to the existing DSN it did work again.

Copy link
Member

Choose a reason for hiding this comment

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

Can you confirm that #59099 fixes this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, missed that in my search. Yes that fixes it, thanks!


$this->setUsername('apikey');
$this->setPassword($key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$key = $this->getUser($dsn);
$region = $dsn->getOption('region');

if ('sendgrid+api' === $scheme) {
$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());
Expand Down