10000 feature #53621 [Mailer] [Smtp] Add DSN param 'auto_tls' to disable au… · symfony/symfony@5091cd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5091cd5

Browse files
feature #53621 [Mailer] [Smtp] Add DSN param 'auto_tls' to disable automatic STARTTLS (srsbiz)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Mailer] [Smtp] Add DSN param 'auto_tls' to disable automatic STARTTLS | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #49114 | License | MIT Many times we don't have any jurisdiction over configuration of SMTP server we are trying to connect to. If such server claims to be capable of STARTTLS, but drops connection after sending this command, there was no way to prevent mailer from sending it - despite defining protocol as `stmp` and port `25`. Now adding `auto_tls=false` to DSN we can prevent transport to automatically send STARTTLS when we do not intend to use TLS. Commits ------- 189a1ac [Mailer] [Smtp] Add DSN param 'auto_tls' to disable automatic STARTTLS
2 parents e00c12e + 189a1ac commit 5091cd5

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/Symfony/Component/Mailer/CHANGELOG.md

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

77
* Dispatch Postmark's "406 - Inactive recipient" API error code as a `PostmarkDeliveryEvent` instead of throwing an exception
8+
* Add DSN param `auto_tls` to disable automatic STARTTLS
89

910
7.0
1011
---

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,28 @@ public static function createProvider(): iterable
157157
new Dsn('smtps', 'example.com', '', '', 465, ['ping_threshold' => '10']),
158158
$transport,
159159
];
160+
161+
$transport = new EsmtpTransport('example.com', 25, false, null, $logger);
162+
$transport->setAutoTls(false);
163+
164+
yield [
165+
new Dsn('smtp', 'example.com', '', '', 25, ['auto_tls' => false]),
166+
$transport,
167+
];
168+
yield [
169+
new Dsn('smtp', 'example.com', '', '', 0, ['auto_tls' => false]),
170+
$transport,
171+
];
172+
yield [
173+
Dsn::fromString('smtp://:@example.com?auto_tls=false'),
174+
$transport,
175+
];
176+
177+
$transport = new EsmtpTransport('example.com', 465, false, null, $logger);
178+
$transport->setAutoTls(false);
179+
yield [
180+
Dsn::fromString('smtp://:@example.com:465?auto_tls=false'),
181+
$transport,
182+
];
160183
}
161184
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class EsmtpTransport extends SmtpTransport
3131
private string $username = '';
3232
private string $password = '';
3333
private array $capabilities;
34+
private bool $autoTls = true;
3435

3536
public function __construct(string $host = 'localhost', int $port = 0, ?bool $tls = null, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, ?AbstractStream $stream = null, ?array $authenticators = null)
3637
{
@@ -99,6 +100,21 @@ public function getPassword(): string
99100
return $this->password;
100101
}
101102

103+
/**
104+
* @return $this
105+
*/
106+
public function setAutoTls(bool $autoTls): static
107+
{
108+
$this->autoTls = $autoTls;
109+
110+
return $this;
111+
}
112+
113+
public function isAutoTls(): bool
114+
{
115+
return $this->autoTls;
116+
}
117+
102118
public function setAuthenticators(array $authenticators): void
103119
{
104120
$this->authenticators = [];
@@ -145,7 +161,7 @@ private function doEhloCommand(): string
145161
// WARNING: !$stream->isTLS() is right, 100% sure :)
146162
// if you think that the ! should be removed, read the code again
147163
// if doing so "fixes" your issue then it probably means your SMTP server behaves incorrectly or is wrongly configured
148-
if (!$stream->isTLS() && \defined('OPENSSL_VERSION_NUMBER') && \array_key_exists('STARTTLS', $this->capabilities)) {
164+
if ($this->autoTls && !$stream->isTLS() && \defined('OPENSSL_VERSION_NUMBER') && \array_key_exists('STARTTLS', $this->capabilities)) {
149165
$this->executeCommand("STARTTLS\r\n", [220]);
150166

151167
if (!$stream->startTLS()) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ final class EsmtpTransportFactory extends AbstractTransportFactory
2323
{
2424
public function create(Dsn $dsn): TransportInterface
2525
{
26-
$tls = 'smtps' === $dsn->getScheme() ? true : null;
26+
$autoTls = '' === $dsn->getOption('auto_tls') || filter_var($dsn->getOption('auto_tls', true), \FILTER_VALIDATE_BOOL);
27+
$tls = 'smtps' === $dsn->getScheme() ? true : ($autoTls ? null : false);
2728
$port = $dsn->getPort(0);
2829
$host = $dsn->getHost();
2930

3031
$transport = new EsmtpTransport($host, $port, $tls, $this->dispatcher, $this->logger);
32+
$transport->setAutoTls($autoTls);
3133

3234
/** @var SocketStream $stream */
3335
$stream = $transport->getStream();

0 commit comments

Comments
 (0)
0