Skip to content

Commit f0748f8

Browse files
committed
feature #35262 [Mailer] add ability to disable the TLS peer verification via DSN (Aurélien Fontaine)
This PR was squashed before being merged into the 5.1-dev branch (closes #35262). Discussion ---------- [Mailer] add ability to disable the TLS peer verification via DSN | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix | License | MIT | Doc PR | symfony/symfony-docs/pull/12997 Add the ability to disable the peer TLS verification with the DNS when using `EsmtpTransport` like this : ``` MAILER_DSN=smtp://foo@default?verify_peer=false ``` By default the verification is enabled Commits ------- 4b854da [Mailer] add ability to disable the TLS peer verification via DSN
2 parents f0fbdee + 4b854da commit f0748f8

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/Symfony/Component/Mailer/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ CHANGELOG
3636
* Added `Symfony\Component\Mailer\Test\TransportFactoryTestCase` to ease testing custom transport factories.
3737
* Added `SentMessage::getDebug()` and `TransportExceptionInterface::getDebug` to help debugging
3838
* Made `MessageEvent` final
39+
* add DSN parameter `verify_peer` to disable TLS peer verification for SMTP transport
3940

4041
4.3.0
4142
-----

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

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Mailer\Transport\Dsn;
77
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
88
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
9+
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
910
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
1011

1112
class EsmtpTransportFactoryTest extends TransportFactoryTestCase
@@ -67,5 +68,18 @@ public function createProvider(): iterable
6768
new Dsn('smtps', 'example.com', '', '', 465),
6869
$transport,
6970
];
71+
72+
$transport = new EsmtpTransport('example.com', 465, true, $eventDispatcher, $logger);
73+
/** @var SocketStream $stream */
74+
$stream = $transport->getStream();
75+
$streamOptions = $stream->getStreamOptions();
76+
$streamOptions['ssl']['verify_peer'] = false;
77+
$streamOptions['ssl']['verify_peer_name'] = false;
78+
$stream->setStreamOptions($streamOptions);
79+
80+
yield [
81+
new Dsn('smtps', 'example.com', '', '', 465, ['verify_peer' => false]),
82+
$transport,
83+
];
7084
}
7185
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
1515
use Symfony\Component\Mailer\Transport\Dsn;
16+
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
1617
use Symfony\Component\Mailer\Transport\TransportInterface;
1718

1819
/**
@@ -28,6 +29,17 @@ public function create(Dsn $dsn): TransportInterface
2829

2930
$transport = new EsmtpTransport($host, $port, $tls, $this->dispatcher, $this->logger);
3031

32+
if (!$dsn->getOption('verify_peer', true)) {
33+
/** @var SocketStream $stream */
34+
$stream = $transport->getStream();
35+
$streamOptions = $stream->getStreamOptions();
36+
37+
$streamOptions['ssl']['verify_peer'] = false;
38+
$streamOptions['ssl']['verify_peer_name'] = false;
39+
40+
$stream->setStreamOptions($streamOptions);
41+
}
42+
3143
if ($user = $dsn->getUser()) {
3244
$transport->setUsername($user);
3345
}

0 commit comments

Comments
 (0)