Skip to content

Commit 8787798

Browse files
srsbiznicolas-grekas
authored andcommitted
[Mailer] [Smtp] Add DSN param 'auto_tls' to disable automatic STARTTLS
1 parent fb8fc09 commit 8787798

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

CHANGELOG.md

+1
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
---

Tests/Transport/Smtp/EsmtpTransportFactoryTest.php

+23
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
}

Transport/Smtp/EsmtpTransport.php

+17-1
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()) {

Transport/Smtp/EsmtpTransportFactory.php

+3-1
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)