Skip to content

Commit c50235a

Browse files
quiliusfabpot
authored andcommitted
[Mailer] [Smtp] Add DSN option to make SocketStream bind to IPv4
1 parent cad21a9 commit c50235a

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)