Skip to content

Commit

Permalink
allow failure of starttls when using TryTls (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch authored Sep 1, 2020
1 parent bd2b4b2 commit e19a0ae
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Exception/SecureConnectionUpgradeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Genkgo\Mail\Exception;

final class SecureConnectionUpgradeException extends AbstractProtocolException
{
}
3 changes: 3 additions & 0 deletions src/Protocol/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Genkgo\Mail\Protocol;

use Genkgo\Mail\Exception\SecureConnectionUpgradeException;

interface ConnectionInterface
{
/**
Expand Down Expand Up @@ -34,6 +36,7 @@ public function receive(): string;

/**
* @param int $type
* @throws SecureConnectionUpgradeException
*/
public function upgrade(int $type): void;

Expand Down
6 changes: 5 additions & 1 deletion src/Protocol/Imap/Negotiation/TryTlsUpgradeNegotiation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Genkgo\Mail\Protocol\Imap\Negotiation;

use Genkgo\Mail\Exception\ConnectionInsecureException;
use Genkgo\Mail\Exception\SecureConnectionUpgradeException;
use Genkgo\Mail\Protocol\ConnectionInterface;
use Genkgo\Mail\Protocol\Imap\Client;
use Genkgo\Mail\Protocol\Imap\NegotiationInterface;
Expand Down Expand Up @@ -59,7 +60,10 @@ public function negotiate(Client $client): void
->assertCompletion(CompletionResult::ok())
->assertTagged();

$this->connection->upgrade($this->crypto);
try {
$this->connection->upgrade($this->crypto);
} catch (SecureConnectionUpgradeException $e) {
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Protocol/PlainTcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Genkgo\Mail\Protocol;

use Genkgo\Mail\Exception\ConnectionRefusedException;
use Genkgo\Mail\Exception\SecureConnectionUpgradeException;

/**
* @codeCoverageIgnore
Expand Down Expand Up @@ -49,9 +50,13 @@ public function __construct(string $host, int $port, float $connectionTimeout =
*/
public function upgrade(int $type): void
{
if ($this->resource === null || \stream_socket_enable_crypto($this->resource, true, $type) === false) {
if ($this->resource === null) {
throw new \InvalidArgumentException('Cannot upgrade connection, resource not available');
}

if (@\stream_socket_enable_crypto($this->resource, true, $type) === false) {
throw new SecureConnectionUpgradeException('Failed to upgrade connection to a secure one');
}
}

public function connect(): void
Expand Down
4 changes: 4 additions & 0 deletions src/Protocol/Smtp/Negotiation/TryTlsUpgradeNegotiation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Genkgo\Mail\Exception\AssertionFailedException;
use Genkgo\Mail\Exception\ConnectionInsecureException;
use Genkgo\Mail\Exception\SecureConnectionUpgradeException;
use Genkgo\Mail\Protocol\ConnectionInterface;
use Genkgo\Mail\Protocol\Smtp\Client;
use Genkgo\Mail\Protocol\Smtp\NegotiationInterface;
Expand Down Expand Up @@ -71,6 +72,9 @@ public function negotiate(Client $client): void
} catch (AssertionFailedException $e) {
// apparently HELO OR STARTTLS command is also not implemented
// but failure of STARTTLS is allowed
} catch (SecureConnectionUpgradeException $e) {
// apparently STARTTLS command is implemented
// but failed to start a secure connection
}
} else {
$reply->assertCompleted();
Expand Down
1 change: 0 additions & 1 deletion test/Unit/Header/SubjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function it_does_not_add_a_space_in_subject(): void
);
}


/**
* @test
*/
Expand Down

0 comments on commit e19a0ae

Please sign in to comment.