10000 feature #59482 [Mailer] [Smtp] Add DSN option to make SocketStream bi… · symfony/symfony@7b0cdc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b0cdc8

Browse files
committed
feature #59482 [Mailer] [Smtp] Add DSN option to make SocketStream bind to IPv4 (quilius)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Mailer] [Smtp] Add DSN option to make SocketStream bind to IPv4 | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | see below | License | MIT We are sending emails through a Microsoft connector, which verifies the sending server's IP address and sender email address. We started getting these error messages recently: ``` Symfony\Component\Messenger\Exception\HandlerFailedException(code: 450): Handling "\Symfony\\Component\\Mailer\\Messenger\\SendEmailMessage" failed: Expected response code "250" but got code "450", with message "450 4.7.26 Service does not accept messages sent over IPv6" ``` Apparently, our server started to connect using IPv6 and the connector only accepts IPv4 addresses. With this PR, we can force the SocketStream to connect to the smtp server via IPv4, by supplying an option in the DSN. Example usage: ``` MAILER_DSN=smtp://<mydomain>.mail.protection.outlook.com:25?force_ipv4=true ``` This is my first PR to Symfony, so I tried doing my best to follow the guide. From the errors I received, I think with DKIM signing IPv6 should be allowed as well. However, I do not have access the DNS records and needed a quick fix. This PR is based on a patch I did in my own project. Probably I will implement an available Microsoft Graph Transport in the future. But perhaps it's worth to keep this option available as well. Commits ------- c50235a [Mailer] [Smtp] Add DSN option to make SocketStream bind to IPv4
2 parents cad21a9 + c50235a commit 7b0cdc8

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/Symfony/Component/Mailer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add DSN param `retry_period` to override default email transport retry period
88
* Add `Dsn::getBooleanOption()`
9+
* Add DSN param `source_ip` to allow binding to a (specific) IPv4 or IPv6 address.
910

1011
7.2
1112
---

src/Symfony/Component/Mailer/Tests/Transport/Smtp/EsmtpTransportFactoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ public static function createProvider(): iterable
180180
Dsn::fromString('smtp://:@example.com:465?auto_tls=false'),
181181
$transport,
182182
];
183+
184+
$transport = new EsmtpTransport('example.com', 465, true, null, $logger);
185+
$transport->getStream()->setSourceIp('0.0.0.0');
186+
yield [
187+
Dsn::fromString('smtps://:@example.com:465?source_ip=0.0.0.0'),
188+
$transport,
189+
];
190+
191+
$transport = new EsmtpTransport('example.com', 465, true, null, $logger);
192+
$transport->getStream()->setSourceIp('[2606:4700:20::681a:5fb]');
193+
yield [
194+
Dsn::fromString('smtps://:@example.com:465?source_ip=[2606:4700:20::681a:5fb]'),
195+
$transport,
196+
];
183197
}
184198

185199
public static function unsupportedSchemeProvider(): iterable

src/Symfony/Component/Mailer/Transport/Smtp/EsmtpTransportFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function create(Dsn $dsn): TransportInterface
3838

3939
/** @var SocketStream $stream */
4040
$stream = $transport->getStream();
41+
if ('' !== $sourceIp = $dsn->getOption('source_ip', '')) {
42+
$stream->setSourceIp($sourceIp);
43+
}
4144
$streamOptions = $stream->getStreamOptions();
4245

4346
if ('' !== $dsn->getOption('verify_peer') && !filter_var($dsn->getOption('verify_peer', true), \FILTER_VALIDATE_BOOL)) {

0 commit comments

Comments
 (0)
0