16
16
use Symfony \Component \Mailer \Exception \TransportException ;
17
17
use Symfony \Component \Mailer \Exception \TransportExceptionInterface ;
18
18
use Symfony \Component \Mailer \Transport \Smtp \Auth \AuthenticatorInterface ;
19
+ use Symfony \Component \Mailer \Transport \Smtp \Stream \AbstractStream ;
19
20
use Symfony \Component \Mailer \Transport \Smtp \Stream \SocketStream ;
20
21
21
22
/**
@@ -29,10 +30,11 @@ class EsmtpTransport extends SmtpTransport
29
30
private array $ authenticators = [];
30
31
private string $ username = '' ;
31
32
private string $ password = '' ;
33
+ private array $ capabilities ;
32
34
33
- public function __construct (string $ host = 'localhost ' , int $ port = 0 , bool $ tls = null , EventDispatcherInterface $ dispatcher = null , LoggerInterface $ logger = null )
35
+ public function __construct (string $ host = 'localhost ' , int $ port = 0 , bool $ tls = null , EventDispatcherInterface $ dispatcher = null , LoggerInterface $ logger = null , AbstractStream $ stream = null )
34
36
{
35
- parent ::__construct (null , $ dispatcher , $ logger );
37
+ parent ::__construct ($ stream , $ dispatcher , $ logger );
36
38
37
39
// order is important here (roughly most secure and popular first)
38
40
$ this ->authenticators = [
@@ -98,24 +100,32 @@ public function addAuthenticator(AuthenticatorInterface $authenticator): void
98
100
$ this ->authenticators [] = $ authenticator ;
99
101
}
100
102
101
- protected function doHeloCommand (): void
103
+ public function executeCommand (string $ command , array $ codes ): string
104
+ {
105
+ return [250 ] === $ codes && str_starts_with ($ command , 'HELO ' ) ? $ this ->doEhloCommand () : parent ::executeCommand ($ command , $ codes );
106
+ }
107
+
108
+ final protected function getCapabilities (): array
109
+ {
110
+ return $ this ->capabilities ;
111
+ }
112
+
113
+ private function doEhloCommand (): string
102
114
{
103
115
try {
104
116
$ response = $ this ->executeCommand (sprintf ("EHLO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
105
117
} catch (TransportExceptionInterface $ e ) {
106
- parent ::doHeloCommand ();
107
-
108
- return ;
118
+ return parent ::executeCommand (sprintf ("HELO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
109
119
}
110
120
111
- $ capabilities = $ this ->getCapabilities ($ response );
121
+ $ this -> capabilities = $ this ->parseCapabilities ($ response );
112
122
113
123
/** @var SocketStream $stream */
114
124
$ stream = $ this ->getStream ();
115
125
// WARNING: !$stream->isTLS() is right, 100% sure :)
116
126
// if you think that the ! should be removed, read the code again
117
127
// if doing so "fixes" your issue then it probably means your SMTP server behaves incorrectly or is wrongly configured
118
- if (!$ stream ->isTLS () && \defined ('OPENSSL_VERSION_NUMBER ' ) && \array_key_exists ('STARTTLS ' , $ capabilities )) {
128
+ if (!$ stream ->isTLS () && \defined ('OPENSSL_VERSION_NUMBER ' ) && \array_key_exists ('STARTTLS ' , $ this -> capabilities )) {
119
129
$ this ->executeCommand ("STARTTLS \r\n" , [220 ]);
120
130
121
131
if (!$ stream ->startTLS ()) {
@@ -124,20 +134,20 @@ protected function doHeloCommand(): void
124
134
125
135
try {
126
136
$ response = $ this ->executeCommand (sprintf ("EHLO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
127
- $ capabilities = $ this ->getCapabilities ($ response );
137
+ $ this -> capabilities = $ this ->parseCapabilities ($ response );
128
138
} catch (TransportExceptionInterface $ e ) {
129
- parent ::doHeloCommand ();
130
-
131
- return ;
139
+ return parent ::executeCommand (sprintf ("HELO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
132
140
}
133
141
}
134
142
135
- if (\array_key_exists ('AUTH ' , $ capabilities )) {
136
- $ this ->handleAuth ($ capabilities ['AUTH ' ]);
143
+ if (\array_key_exists ('AUTH ' , $ this -> capabilities )) {
144
+ $ this ->handleAuth ($ this -> capabilities ['AUTH ' ]);
137
145
}
146
+
147
+ return $ response ;
138
148
}
139
149
140
- private function getCapabilities (string $ ehloResponse ): array
150
+ private function parseCapabilities (string $ ehloResponse ): array
141
151
{
142
152
$ capabilities = [];
143
153
$ lines = explode ("\r\n" , trim ($ ehloResponse ));
0 commit comments