allow user/pass on dns while using failover/roundrobin and type fix for username/password

This commit is contained in:
Patrick Landolt 2019-04-07 22:39:37 +02:00
parent 2efd7b2149
commit 4518ac56a1
3 changed files with 22 additions and 4 deletions

View File

@ -236,9 +236,11 @@ class TransportTest extends TestCase
public function testFromDsnFailover()
{
$user = 'user';
$pass = 'pass';
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$logger = $this->createMock(LoggerInterface::class);
$transport = Transport::fromDsn('smtp://null || smtp://null || smtp://null', $dispatcher, null, $logger);
$transport = Transport::fromDsn('smtp://example.com || smtp://'.urlencode($user).'@example.com || smtp://'.urlencode($user).':'.urlencode($pass).'@example.com', $dispatcher, null, $logger);
$this->assertInstanceOf(Transport\FailoverTransport::class, $transport);
$p = new \ReflectionProperty(Transport\RoundRobinTransport::class, 'transports');
$p->setAccessible(true);
@ -247,6 +249,12 @@ class TransportTest extends TestCase
foreach ($transports as $transport) {
$this->assertProperties($transport, $dispatcher, $logger);
}
$this->assertSame('', $transports[0]->getUsername());
$this->assertSame('', $transports[0]->getPassword());
$this->assertSame($user, $transports[1]->getUsername());
$this->assertSame('', $transports[1]->getPassword());
$this->assertSame($user, $transports[2]->getUsername());
$this->assertSame($pass, $transports[2]->getPassword());
}
public function testFromDsnRoundRobin()

View File

@ -171,7 +171,17 @@ class Transport
throw new LogicException(sprintf('The "%s" scheme is not supported for mailer "%s".', $parsedDsn['scheme'], $parsedDsn['host']));
default:
if ('smtp' === $parsedDsn['scheme']) {
return new Transport\Smtp\EsmtpTransport($parsedDsn['host'], $parsedDsn['port'] ?? 25, $query['encryption'] ?? null, $query['auth_mode'] ?? null, $dispatcher, $logger);
$transport = new Transport\Smtp\EsmtpTransport($parsedDsn['host'], $parsedDsn['port'] ?? 25, $query['encryption'] ?? null, $query['auth_mode'] ?? null, $dispatcher, $logger);
if ($user) {
$transport->setUsername($user);
}
if ($pass) {
$transport->setPassword($pass);
}
return $transport;
}
throw new LogicException(sprintf('The "%s" mailer is not supported.', $parsedDsn['host']));

View File

@ -29,8 +29,8 @@ use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
class EsmtpTransport extends SmtpTransport
{
private $authenticators = [];
private $username;
private $password;
private $username = '';
private $password = '';
private $authMode;
public function __construct(string $host = 'localhost', int $port = 25, string $encryption = null, string $authMode = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)