Skip to content

[Mailer] [Smtp] Add DSN param 'auto_tls' to disable automatic STARTTLS #53621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,28 @@ public static function createProvider(): iterable
new Dsn('smtps', 'example.com', '', '', 465, ['ping_threshold' => '10']),
$transport,
];

$transport = new EsmtpTransport('example.com', 25, false, null, $logger);
$transport->setAutoTls(false);

yield [
new Dsn('smtp', 'example.com', '', '', 25, ['auto_tls' => false]),
$transport,
];
yield [
new Dsn('smtp', 'example.com', '', '', 0, ['auto_tls' => false]),
$transport,
];
yield [
Dsn::fromString('smtp://:@example.com?auto_tls=false'),
$transport,
];

$transport = new EsmtpTransport('example.com', 465, false, null, $logger);
$transport->setAutoTls(false);
yield [
Dsn::fromString('smtp://:@example.com:465?auto_tls=false'),
$transport,
];
}
}
18 changes: 17 additions & 1 deletion src/Symfony/Component/Mailer/Transport/Smtp/EsmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class EsmtpTransport extends SmtpTransport
private string $username = '';
private string $password = '';
private array $capabilities;
private bool $autoTls = true;

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

/**
* @return $this
*/
public function setAutoTls(bool $autoTls): static
{
$this->autoTls = $autoTls;

return $this;
}

public function isAutoTls(): bool
{
return $this->autoTls;
}

public function setAuthenticators(array $authenticators): void
{
$this->authenticators = [];
Expand Down Expand Up @@ -145,7 +161,7 @@ private function doEhloCommand(): string
// WARNING: !$stream->isTLS() is right, 100% sure :)
// if you think that the ! should be removed, read the code again
// if doing so "fixes" your issue then it probably means your SMTP server behaves incorrectly or is wrongly configured
if (!$stream->isTLS() && \defined('OPENSSL_VERSION_NUMBER') && \array_key_exists('STARTTLS', $this->capabilities)) {
if ($this->autoTls && !$stream->isTLS() && \defined('OPENSSL_VERSION_NUMBER') && \array_key_exists('STARTTLS', $this->capabilities)) {
$this->executeCommand("STARTTLS\r\n", [220]);

if (!$stream->startTLS()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ final class EsmtpTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$tls = 'smtps' === $dsn->getScheme() ? true : null;
$autoTls = '' === $dsn->getOption('auto_tls') || filter_var($dsn->getOption('auto_tls', true), \FILTER_VALIDATE_BOOL);
$tls = 'smtps' === $dsn->getScheme() ? true : ($autoTls ? null : false);
$port = $dsn->getPort(0);
$host = $dsn->getHost();

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

/** @var SocketStream $stream */
$stream = $transport->getStream();
Expand Down