bug #32211 [Mailer] Fix error message when connecting to a stream raises an error before connect() (fabpot)

This PR was merged into the 4.3 branch.

Discussion
----------

[Mailer] Fix error message when connecting to a stream raises an error before connect()

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | see https://github.com/swiftmailer/swiftmailer/issues/1201
| License       | MIT
| Doc PR        | n/a

According to the PHP docs, "If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call.".

Using the `@` operator means that we get a generic error message without any clues about why connection cannot be done. Using an error handler allows to get the real issue.

Commits
-------

eb15bffa78 [Mailer] fixed error message when connecting to a stream raises an error before connect()
This commit is contained in:
Fabien Potencier 2019-06-27 13:12:59 +02:00
commit f2f7fb4cf2
2 changed files with 58 additions and 3 deletions

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Tests\Transport\Smtp\Stream;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
class SocketStreamTest extends TestCase
{
/**
* @expectedException \Symfony\Component\Mailer\Exception\TransportException
* @expectedExceptionMessage Connection refused
*/
public function testSocketErrorNoConnection()
{
$s = new SocketStream();
$s->setTimeout(0.1);
$s->setPort(9999);
$s->initialize();
}
/**
* @expectedException \Symfony\Component\Mailer\Exception\TransportException
* @expectedExceptionMessage no valid certs found cafile stream
*/
public function testSocketErrorBeforeConnectError()
{
$s = new SocketStream();
$s->setStreamOptions([
'ssl' => [
// not a CA file :)
'cafile' => __FILE__,
],
]);
$s->setEncryption('ssl');
$s->setHost('smtp.gmail.com');
$s->setPort(465);
$s->initialize();
}
}

View File

@ -144,10 +144,16 @@ final class SocketStream extends AbstractStream
$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;
}
$streamContext = stream_context_create($options);
$this->stream = @stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
if (false === $this->stream) {
throw new TransportException(sprintf('Connection could not be established with host "%s": %s (%s)', $this->url, $errstr, $errno));
set_error_handler(function ($type, $msg) {
throw new TransportException(sprintf('Connection could not be established with host "%s": %s.', $this->url, $msg));
});
try {
$this->stream = stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
} finally {
restore_error_handler();
}
stream_set_blocking($this->stream, true);
stream_set_timeout($this->stream, $this->timeout);
$this->in = &$this->stream;