1616use Symfony \Component \Mailer \Exception \TransportException ;
1717use Symfony \Component \Mailer \Exception \TransportExceptionInterface ;
1818use Symfony \Component \Mailer \Transport \Smtp \Auth \AuthenticatorInterface ;
19+ use Symfony \Component \Mailer \Transport \Smtp \Stream \AbstractStream ;
1920use Symfony \Component \Mailer \Transport \Smtp \Stream \SocketStream ;
2021
2122/**
@@ -29,10 +30,11 @@ class EsmtpTransport extends SmtpTransport
2930 private array $ authenticators = [];
3031 private string $ username = '' ;
3132 private string $ password = '' ;
33+ private array $ capabilities ;
3234
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 )
3436 {
35- parent ::__construct (null , $ dispatcher , $ logger );
37+ parent ::__construct ($ stream , $ dispatcher , $ logger );
3638
3739 // order is important here (roughly most secure and popular first)
3840 $ this ->authenticators = [
@@ -98,24 +100,32 @@ public function addAuthenticator(AuthenticatorInterface $authenticator): void
98100 $ this ->authenticators [] = $ authenticator ;
99101 }
100102
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
102114 {
103115 try {
104116 $ response = $ this ->executeCommand (sprintf ("EHLO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
105117 } catch (TransportExceptionInterface $ e ) {
106- parent ::doHeloCommand ();
107-
108- return ;
118+ return parent ::executeCommand (sprintf ("HELO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
109119 }
110120
111- $ capabilities = $ this ->getCapabilities ($ response );
121+ $ this -> capabilities = $ this ->parseCapabilities ($ response );
112122
113123 /** @var SocketStream $stream */
114124 $ stream = $ this ->getStream ();
115125 // WARNING: !$stream->isTLS() is right, 100% sure :)
116126 // if you think that the ! should be removed, read the code again
117127 // 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 )) {
119129 $ this ->executeCommand ("STARTTLS \r\n" , [220 ]);
120130
121131 if (!$ stream ->startTLS ()) {
@@ -124,20 +134,20 @@ protected function doHeloCommand(): void
124134
125135 try {
126136 $ response = $ this ->executeCommand (sprintf ("EHLO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
127- $ capabilities = $ this ->getCapabilities ($ response );
137+ $ this -> capabilities = $ this ->parseCapabilities ($ response );
128138 } catch (TransportExceptionInterface $ e ) {
129- parent ::doHeloCommand ();
130-
131- return ;
139+ return parent ::executeCommand (sprintf ("HELO %s \r\n" , $ this ->getLocalDomain ()), [250 ]);
132140 }
133141 }
134142
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 ' ]);
137145 }
146+
147+ return $ response ;
138148 }
139149
140- private function getCapabilities (string $ ehloResponse ): array
150+ private function parseCapabilities (string $ ehloResponse ): array
141151 {
142152 $ capabilities = [];
143153 $ lines = explode ("\r\n" , trim ($ ehloResponse ));
0 commit comments