Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
bug #273 Throwing Exception for bad URL formats (maitre-hibou)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.2-dev branch (closes #273).

Discussion
----------

Throwing Exception for bad URL formats

Hello there,

**Description** : When supplying a wrong URL format in the `MAILER_URL` environment variable, Swiftmailer Transport component is initialized with bundle default configuration values.
Therefore, when trying to send emails, a "silent" error is triggered with the following message : `Exception occurred while flushing email queue: Connection could not be established with host localhost [Address not available #99]`.

**Changes made** : This PR now throws an `InvalidArgumentException` if supplied URL is not parseable by PHP's `parse_url` method.

Commits
-------

c31bc23 Throwing Exception for bad URL formats
  • Loading branch information
fabpot committed Apr 18, 2019
2 parents ac774af + c31bc23 commit 218c925
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion DependencyInjection/SwiftmailerTransportFactory.php
Expand Up @@ -99,7 +99,9 @@ public static function resolveOptions(array $options)
];

if (isset($options['url'])) {
$parts = parse_url($options['url']);
if (false === ($parts = parse_url($options['url']))) {
throw new \InvalidArgumentException(sprintf('Supplied url [%s] is not a valid format.', $options['url']));
}
if (isset($parts['scheme'])) {
$options['transport'] = $parts['scheme'];
}
Expand Down
27 changes: 27 additions & 0 deletions Tests/DependencyInjection/SwiftmailerTransportFactoryTest.php
Expand Up @@ -161,6 +161,33 @@ public function testCreateTransportWithSmtpAndWithoutRequestContext()
$this->assertSame($authHandler->getAuthMode(), $options['auth_mode']);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Supplied url [smtp://localhost:25&auth_mode=cra-md5] is not a valid format.
*/
public function testCreateTransportWithBadURLFormat()
{
$options = [
'url' => 'smtp://localhost:25&auth_mode=cra-md5',
'transport' => 'smtp',
'username' => null,
'password' => null,
'host' => 'localhost',
'port' => null,
'timeout' => 30,
'source_ip' => null,
'local_domain' => null,
'encryption' => null,
'auth_mode' => null,
];

SwiftmailerTransportFactory::createTransport(
$options,
null,
new \Swift_Events_SimpleEventDispatcher()
);
}

/**
* @dataProvider optionsAndResultExpected
*/
Expand Down

0 comments on commit 218c925

Please sign in to comment.