Skip to content

Commit e9de8dc

Browse files
committed
Added option to require TLS/STARTTLS on EstmpTransport
For 7.2 branch, As done before: 6.4: 0497d6e 6.0: symfony/mailer@6.0...ssddanbrown:symfony-mailer:6.0
1 parent 84a37c1 commit e9de8dc

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Mailer Component
33

44
The Mailer component helps sending emails.
55

6+
Note: This is a (hopefully temporary) fork of [symfony/mailer](https://github.com/symfony/mailer) to patch in TLS
7+
assurance for use in BookStack. It's not advised to use this library fork for other projects.
8+
It only contains/patches the single branch used by BookStack.
9+
610
Getting Started
711
---------------
812

Transport/Smtp/EsmtpTransport.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class EsmtpTransport extends SmtpTransport
3333
private string $password = '';
3434
private array $capabilities;
3535
private bool $autoTls = true;
36+
private bool $requireTls = false;
3637

3738
public function __construct(string $host = 'localhost', int $port = 0, ?bool $tls = null, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, ?AbstractStream $stream = null, ?array $authenticators = null)
3839
{
@@ -62,6 +63,8 @@ public function __construct(string $host = 'localhost', int $port = 0, ?bool $tl
6263
}
6364
if (!$tls) {
6465
$stream->disableTls();
66+
} else {
67+
$this->requireTls = true;
6568
}
6669
if (0 === $port) {
6770
$port = $tls ? 465 : 25;
@@ -116,6 +119,20 @@ public function isAutoTls(): bool
116119
return $this->autoTls;
117120
}
118121

122+
/**
123+
* @return $this
124+
*/
125+
public function setTlsRequirement(bool $required): static
126+
{
127+
$this->requireTls = $required;
128+
return $this;
129+
}
130+
131+
public function getTlsRequirement(): bool
132+
{
133+
return $this->requireTls;
134+
}
135+
119136
public function setAuthenticators(array $authenticators): void
120137
{
121138
$this->authenticators = [];
@@ -159,6 +176,7 @@ private function doEhloCommand(): string
159176

160177
/** @var SocketStream $stream */
161178
$stream = $this->getStream();
179+
$tlsStarted = $stream->isTLS();
162180
// WARNING: !$stream->isTLS() is right, 100% sure :)
163181
// if you think that the ! should be removed, read the code again
164182
// if doing so "fixes" your issue then it probably means your SMTP server behaves incorrectly or is wrongly configured
@@ -169,10 +187,15 @@ private function doEhloCommand(): string
169187
throw new TransportException('Unable to connect with STARTTLS.');
170188
}
171189

190+
$tlsStarted = true;
172191
$response = $this->executeCommand(\sprintf("EHLO %s\r\n", $this->getLocalDomain()), [250]);
173192
$this->capabilities = $this->parseCapabilities($response);
174193
}
175194

195+
if (!$tlsStarted && $this->getTlsRequirement()) {
196+
throw new TransportException('TLS is required but neither TLS or STARTTLS is in use.');
197+
}
198+
176199
if (\array_key_exists('AUTH', $this->capabilities)) {
177200
$this->handleAuth($this->capabilities['AUTH']);
178201
}

Transport/Smtp/EsmtpTransportFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function create(Dsn $dsn): TransportInterface
7575
$transport->setPingThreshold((int) $pingThreshold);
7676
}
7777

78+
if (null !== ($tlsRequired = $dsn->getOption('tls_required'))) {
79+
$transport->setTlsRequirement((bool) $tlsRequired);
80+
}
81+
7882
return $transport;
7983
}
8084

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
2-
"name": "symfony/mailer",
2+
"name": "ssddanbrown/symfony-mailer",
33
"type": "library",
44
"description": "Helps sending emails",
55
"keywords": [],
66
"homepage": "https://symfony.com",
77
"license": "MIT",
88
"authors": [
9+
{
10+
"name": "Dan Brown",
11+
"homepage": "https://danb.me"
12+
},
913
{
1014
"name": "Fabien Potencier",
1115
"email": "fabien@symfony.com"
@@ -37,6 +41,9 @@
3741
"symfony/mime": "<6.4",
3842
"symfony/twig-bridge": "<6.4"
3943
},
44+
"replace": {
45+
"symfony/mailer": "^7.0"
46+
},
4047
"autoload": {
4148
"psr-4": { "Symfony\\Component\\Mailer\\": "" },
4249
"exclude-from-classmap": [

0 commit comments

Comments
 (0)