@@ -87,10 +87,12 @@ public function send(Message $mail): void
8787 $ this ->connect ();
8888 }
8989
90- if (
91- ($ from = $ mail ->getHeader ('Return-Path ' ))
92- || ($ from = array_keys ((array ) $ mail ->getHeader ('From ' ))[0 ] ?? null )
93- ) {
90+ $ from = $ mail ->getHeader ('Return-Path ' );
91+ if (!is_string ($ from )) {
92+ $ from = array_keys ((array ) $ mail ->getHeader ('From ' ))[0 ] ?? null ;
93+ }
94+
95+ if (is_string ($ from )) {
9496 $ this ->write ("MAIL FROM:< $ from> " , 250 );
9597 }
9698
@@ -127,26 +129,28 @@ public function send(Message $mail): void
127129 protected function connect (): void
128130 {
129131 $ port = $ this ->port ?? ($ this ->encryption === self ::EncryptionSSL ? 465 : 25 );
130- $ this -> connection = @stream_socket_client (// @ is escalated to exception
132+ $ connection = @stream_socket_client (// @ is escalated to exception
131133 ($ this ->encryption === self ::EncryptionSSL ? 'ssl:// ' : '' ) . $ this ->host . ': ' . $ port ,
132134 $ errno ,
133135 $ error ,
134136 $ this ->timeout ,
135137 STREAM_CLIENT_CONNECT ,
136138 $ this ->context ,
137139 );
138- if (!$ this -> connection ) {
139- throw new SmtpException ($ error ?: error_get_last ()['message ' ], $ errno );
140+ if (!$ connection ) {
141+ throw new SmtpException ($ error ?: error_get_last ()['message ' ] ?? ' Unknown error ' , ( int ) $ errno );
140142 }
141143
142- stream_set_timeout ($ this ->connection , $ this ->timeout , 0 );
144+ $ this ->connection = $ connection ;
145+
146+ stream_set_timeout ($ connection , $ this ->timeout , 0 );
143147 $ this ->read (); // greeting
144148
145149 if ($ this ->encryption === self ::EncryptionTLS) {
146150 $ this ->write ("EHLO $ this ->clientHost " , 250 );
147151 $ this ->write ('STARTTLS ' , 220 );
148152 if (!stream_socket_enable_crypto (
149- $ this -> connection ,
153+ $ connection ,
150154 true ,
151155 STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
152156 )) {
@@ -191,8 +195,10 @@ protected function connect(): void
191195 */
192196 protected function disconnect (): void
193197 {
194- fclose ($ this ->connection );
195- $ this ->connection = null ;
198+ if ($ this ->connection ) {
199+ fclose ($ this ->connection );
200+ $ this ->connection = null ;
201+ }
196202 }
197203
198204
@@ -202,7 +208,7 @@ protected function disconnect(): void
202208 */
203209 protected function write (string $ line , int |array |null $ expectedCode = null , ?string $ message = null ): void
204210 {
205- fwrite ($ this ->connection , $ line . Message::EOL );
211+ fwrite ($ this ->connection ?? throw new SmtpException ( ' Not connected to SMTP server. ' ) , $ line . Message::EOL );
206212 if ($ expectedCode ) {
207213 $ response = $ this ->read ();
208214 if (!in_array ((int ) $ response , (array ) $ expectedCode , strict: true )) {
@@ -229,6 +235,8 @@ protected function read(): string
229235 } elseif ($ info ['eof ' ]) {
230236 throw new SmtpException ('Connection has been closed unexpectedly. ' );
231237 }
238+
239+ continue ;
232240 }
233241
234242 $ data .= $ line ;
0 commit comments