@@ -33,6 +33,7 @@ class EsmtpTransport extends SmtpTransport
33
33
private string $ password = '' ;
34
34
private array $ capabilities ;
35
35
private bool $ autoTls = true ;
36
+ private bool $ requireTls = false ;
36
37
37
38
public function __construct (string $ host = 'localhost ' , int $ port = 0 , ?bool $ tls = null , ?EventDispatcherInterface $ dispatcher = null , ?LoggerInterface $ logger = null , ?AbstractStream $ stream = null , ?array $ authenticators = null )
38
39
{
@@ -62,6 +63,8 @@ public function __construct(string $host = 'localhost', int $port = 0, ?bool $tl
62
63
}
63
64
if (!$ tls ) {
64
65
$ stream ->disableTls ();
66
+ } else {
67
+ $ this ->requireTls = true ;
65
68
}
66
69
if (0 === $ port ) {
67
70
$ port = $ tls ? 465 : 25 ;
@@ -116,6 +119,20 @@ public function isAutoTls(): bool
116
119
return $ this ->autoTls ;
117
120
}
118
121
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
+
119
136
public function setAuthenticators (array $ authenticators ): void
120
137
{
121
138
$ this ->authenticators = [];
@@ -159,6 +176,7 @@ private function doEhloCommand(): string
159
176
160
177
/** @var SocketStream $stream */
161
178
$ stream = $ this ->getStream ();
179
+ $ tlsStarted = $ stream ->isTLS ();
162
180
// WARNING: !$stream->isTLS() is right, 100% sure :)
163
181
// if you think that the ! should be removed, read the code again
164
182
// 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
169
187
throw new TransportException ('Unable to connect with STARTTLS. ' );
170
188
}
171
189
190
+ $ tlsStarted = true ;
172
191
$ response = $ this ->executeCommand (\sprintf ("EHLO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
173
192
$ this ->capabilities = $ this ->parseCapabilities ($ response );
174
193
}
175
194
195
+ if (!$ tlsStarted && $ this ->getTlsRequirement ()) {
196
+ throw new TransportException ('TLS is required but neither TLS or STARTTLS is in use. ' );
197
+ }
198
+
176
199
if (\array_key_exists ('AUTH ' , $ this ->capabilities )) {
177
200
$ this ->handleAuth ($ this ->capabilities ['AUTH ' ]);
178
201
}
0 commit comments