Skip to content

Commit

Permalink
bug #30995 [Mailer] allow user/pass on dsn while using failover/round…
Browse files Browse the repository at this point in the history
…robin (scuben)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Mailer] allow user/pass on dsn while using failover/roundrobin

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

this PR provides two things:
1. It is possible now to user `username` and `password` in a failover or round robin transport when using smtp
2. Fixed a type problem with `username` and `password` for the smtp transport as `getUsername()` cannot return `null` because of its signature but if no `username` is provided then the property would have been `null`. Fixed with setting an empty string as default. Same for `password`. (This was discovered by adding a test - yeah!)

Commits
-------

4518ac5 allow user/pass on dns while using failover/roundrobin and type fix for username/password
  • Loading branch information
fabpot committed Apr 8, 2019
2 parents e02cbe9 + 4518ac5 commit 3568418
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/Symfony/Component/Mailer/Tests/TransportTest.php
Expand Up @@ -236,9 +236,11 @@ public function testFromDsnMailchimp()

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);
Expand All @@ -247,6 +249,12 @@ public function testFromDsnFailover()
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()
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Mailer/Transport.php
Expand Up @@ -171,7 +171,17 @@ private static function createTransport(string $dsn, EventDispatcherInterface $d
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']));
Expand Down
Expand Up @@ -29,8 +29,8 @@
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)
Expand Down

0 comments on commit 3568418

Please sign in to comment.