Skip to content

Commit 08f51d8

Browse files
committed
[Mailer] fixed error message when connecting to a stream raises an error before connect()
1 parent 623d6dd commit 08f51d8

File tree

6 files changed

+134
-12
lines changed

6 files changed

+134
-12
lines changed

Smtp/Stream/SocketStream.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ public function initialize(): void
144144
$options['ssl']['crypto_method'] = $options['ssl']['crypto_method'] ?? STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
145145
}
146146
$streamContext = stream_context_create($options);
147-
$this->stream = @stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
148-
if (false === $this->stream) {
149-
throw new TransportException(sprintf('Connection could not be established with host "%s": %s (%s)', $this->url, $errstr, $errno));
147+
148+
set_error_handler(function ($type, $msg) {
149+
throw new TransportException(sprintf('Connection could not be established with host "%s": %s.', $this->url, $msg));
150+
});
151+
try {
152+
$this->stream = stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
153+
} finally {
154+
restore_error_handler();
150155
}
156+
151157
stream_set_blocking($this->stream, true);
152158
stream_set_timeout($this->stream, $this->timeout);
153159
$this->in = &$this->stream;

SocketStream.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ public function initialize(): void
144144
$options['ssl']['crypto_method'] = $options['ssl']['crypto_method'] ?? STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
145145
}
146146
$streamContext = stream_context_create($options);
147-
$this->stream = @stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
148-
if (false === $this->stream) {
149-
throw new TransportException(sprintf('Connection could not be established with host "%s": %s (%s)', $this->url, $errstr, $errno));
147+
148+
set_error_handler(function ($type, $msg) {
149+
throw new TransportException(sprintf('Connection could not be established with host "%s": %s.', $this->url, $msg));
150+
});
151+
try {
152+
$this->stream = stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
153+
} finally {
154+
restore_error_handler();
150155
}
156+
151157
stream_set_blocking($this->stream, true);
152158
stream_set_timeout($this->stream, $this->timeout);
153159
$this->in = &$this->stream;

SocketStreamTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests\Transport\Smtp\Stream;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
16+
17+
class SocketStreamTest extends TestCase
18+
{
19+
/**
20+
* @expectedException \Symfony\Component\Mailer\Exception\TransportException
21+
* @expectedExceptionMessage Connection refused
22+
*/
23+
public function testSocketErrorNoConnection()
24+
{
25+
$s = new SocketStream();
26+
$s->setTimeout(0.1);
27+
$s->setPort(9999);
28+
$s->initialize();
29+
}
30+
31+
/**
32+
* @expectedException \Symfony\Component\Mailer\Exception\TransportException
33+
* @expectedExceptionMessage no valid certs found cafile stream
34+
*/
35+
public function testSocketErrorBeforeConnectError()
36+
{
37+
$s = new SocketStream();
38+
$s->setStreamOptions([
39+
'ssl' => [
40+
// not a CA file :)
41+
'cafile' => __FILE__,
42+
],
43+
]);
44+
$s->setEncryption('ssl');
45+
$s->setHost('smtp.gmail.com');
46+
$s->setPort(465);
47+
$s->initialize();
48+
}
49+
}

Stream/SocketStream.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ public function initialize(): void
144144
$options['ssl']['crypto_method'] = $options['ssl']['crypto_method'] ?? STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
145145
}
146146
$streamContext = stream_context_create($options);
147-
$this->stream = @stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
148-
if (false === $this->stream) {
149-
throw new TransportException(sprintf('Connection could not be established with host "%s": %s (%s)', $this->url, $errstr, $errno));
147+
148+
set_error_handler(function ($type, $msg) {
149+
throw new TransportException(sprintf('Connection could not be established with host "%s": %s.', $this->url, $msg));
150+
});
151+
try {
152+
$this->stream = stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
153+
} finally {
154+
restore_error_handler();
150155
}
156+
151157
stream_set_blocking($this->stream, true);
152158
stream_set_timeout($this->stream, $this->timeout);
153159
$this->in = &$this->stream;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests\Transport\Smtp\Stream;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
16+
17+
class SocketStreamTest extends TestCase
18+
{
19+
/**
20+
* @expectedException \Symfony\Component\Mailer\Exception\TransportException
21+
* @expectedExceptionMessage Connection refused
22+
*/
23+
public function testSocketErrorNoConnection()
24+
{
25+
$s = new SocketStream();
26+
$s->setTimeout(0.1);
27+
$s->setPort(9999);
28+
$s->initialize();
29+
}
30+
31+
/**
32+
* @expectedException \Symfony\Component\Mailer\Exception\TransportException
33+
* @expectedExceptionMessage no valid certs found cafile stream
34+
*/
35+
public function testSocketErrorBeforeConnectError()
36+
{
37+
$s = new SocketStream();
38+
$s->setStreamOptions([
39+
'ssl' => [
40+
// not a CA file :)
41+
'cafile' => __FILE__,
42+
],
43+
]);
44+
$s->setEncryption('ssl');
45+
$s->setHost('smtp.gmail.com');
46+
$s->setPort(465);
47+
$s->initialize();
48+
}
49+
}

Transport/Smtp/Stream/SocketStream.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ public function initialize(): void
144144
$options['ssl']['crypto_method'] = $options['ssl']['crypto_method'] ?? STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
145145
}
146146
$streamContext = stream_context_create($options);
147-
$this->stream = @stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
148-
if (false === $this->stream) {
149-
throw new TransportException(sprintf('Connection could not be established with host "%s": %s (%s)', $this->url, $errstr, $errno));
147+
148+
set_error_handler(function ($type, $msg) {
149+
throw new TransportException(sprintf('Connection could not be established with host "%s": %s.', $this->url, $msg));
150+
});
151+
try {
152+
$this->stream = stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
153+
} finally {
154+
restore_error_handler();
150155
}
156+
151157
stream_set_blocking($this->stream, true);
152158
stream_set_timeout($this->stream, $this->timeout);
153159
$this->in = &$this->stream;

0 commit comments

Comments
 (0)