From 1a9b67576ce7b9f677ee05556ff4ba26f7f0823f Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 13 Oct 2016 07:49:44 +0200 Subject: [PATCH 1/3] Support setLocalDomain() and setStreamOptions() --- DependencyInjection/Configuration.php | 30 +++++++++ DependencyInjection/Configurator.php | 51 ++++++++++++++ DependencyInjection/SwiftmailerExtension.php | 32 ++++++++- Resources/config/schema/swiftmailer-1.0.xsd | 17 +++++ Resources/config/swiftmailer.xml | 3 + .../Fixtures/config/php/full.php | 1 + .../Fixtures/config/php/many_mailers.php | 3 + .../Fixtures/config/php/one_mailer.php | 1 + .../Fixtures/config/php/sendmail.php | 1 + .../Fixtures/config/php/smtp.php | 1 + .../Fixtures/config/php/stream_options.php | 15 +++++ .../Fixtures/config/xml/full.xml | 1 + .../Fixtures/config/xml/many_mailers.xml | 3 + .../Fixtures/config/xml/one_mailer.xml | 3 +- .../Fixtures/config/xml/sendmail.xml | 1 + .../Fixtures/config/xml/smtp.xml | 4 +- .../Fixtures/config/xml/stream_options.xml | 22 ++++++ .../Fixtures/config/yml/full.yml | 21 +++--- .../Fixtures/config/yml/many_mailers.yml | 67 ++++++++++--------- .../Fixtures/config/yml/one_mailer.yml | 1 + .../Fixtures/config/yml/sendmail.yml | 1 + .../Fixtures/config/yml/smtp.yml | 21 +++--- .../Fixtures/config/yml/stream_options.yml | 11 +++ .../SwiftmailerExtensionTest.php | 61 +++++++++++++++-- 24 files changed, 312 insertions(+), 60 deletions(-) create mode 100644 DependencyInjection/Configurator.php create mode 100644 Tests/DependencyInjection/Fixtures/config/php/stream_options.php create mode 100644 Tests/DependencyInjection/Fixtures/config/xml/stream_options.xml create mode 100644 Tests/DependencyInjection/Fixtures/config/yml/stream_options.yml diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9d0d7c2b..4db77595 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -108,6 +108,36 @@ private function getMailersNode() ->scalarNode('port')->defaultNull()->end() ->scalarNode('timeout')->defaultValue(30)->end() ->scalarNode('source_ip')->defaultNull()->end() + ->scalarNode('local_domain')->defaultNull()->end() + ->arrayNode('stream_options') + ->ignoreExtraKeys(false) + ->normalizeKeys(false) + ->beforeNormalization() + ->ifTrue(function ($v) { return isset($v['stream-option']); }) + ->then(function ($v) { + $recurse = function ($array) use (&$recurse) { + if (isset($array['name'])) { + $array = array($array); + } + $n = array(); + foreach ($array as $v) { + $k = $v['name']; + if (isset($v['value'])) { + $n[$k] = $v['value']; + } elseif (isset($v['stream-option'])) { + $n[$k] = $recurse($v['stream-option']); + } + } + return $n; + }; + return $recurse($v['stream-option']); + }) + ->end() + ->validate() + ->ifTrue(function ($v) { return !method_exists('Swift_Transport_EsmtpTransport', 'setStreamOptions'); }) + ->thenInvalid('stream_options is only available in Swiftmailer 5.4.2 or later.') + ->end() + ->end() ->scalarNode('encryption') ->defaultNull() ->validate() diff --git a/DependencyInjection/Configurator.php b/DependencyInjection/Configurator.php new file mode 100644 index 00000000..541574b6 --- /dev/null +++ b/DependencyInjection/Configurator.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\SwiftmailerBundle\DependencyInjection; + +use Symfony\Component\Routing\RequestContext; + +/** + * Service configurator. + */ +class Configurator +{ + /** + * @var string + */ + protected $localDomain; + + /** + * @var RequestContext + */ + protected $requestContext; + + /** + * Sets the local domain based on the current request context. + * + * @param string $localDomain Fallback value if there is no request context. + * @param RequestContext $requestContext + */ + public function __construct($localDomain, RequestContext $requestContext = null) + { + $this->localDomain = $localDomain; + $this->requestContext = $requestContext; + } + + public function configureLocalDomain(\Swift_Transport_AbstractSmtpTransport $transport) + { + if ($this->localDomain) { + $transport->setLocalDomain($this->localDomain); + } elseif ($this->requestContext) { + $transport->setLocalDomain($this->requestContext->getHost()); + } + } +} diff --git a/DependencyInjection/SwiftmailerExtension.php b/DependencyInjection/SwiftmailerExtension.php index 15ce2c39..797f2670 100644 --- a/DependencyInjection/SwiftmailerExtension.php +++ b/DependencyInjection/SwiftmailerExtension.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\SwiftmailerBundle\DependencyInjection; use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\DefinitionDecorator; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; @@ -138,13 +139,15 @@ protected function configureMailer($name, array $mailer, ContainerBuilder $conta protected function configureMailerTransport($name, array $mailer, ContainerBuilder $container, $transport, $isDefaultMailer = false) { - foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode', 'timeout', 'source_ip') as $key) { + foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode', 'timeout', 'source_ip', 'local_domain') as $key) { $container->setParameter(sprintf('swiftmailer.mailer.%s.transport.smtp.%s', $name, $key), $mailer[$key]); } + $definitionDecorator = new DefinitionDecorator('swiftmailer.transport.eventdispatcher.abstract'); $container ->setDefinition(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name), $definitionDecorator) ; + if ('smtp' === $transport) { $authDecorator = new DefinitionDecorator('swiftmailer.transport.authhandler.abstract'); $container @@ -157,6 +160,15 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild $container ->setDefinition(sprintf('swiftmailer.mailer.%s.transport.buffer', $name), $bufferDecorator); + $configuratorDecorator = new DefinitionDecorator('swiftmailer.configurator.abstract'); + $container + ->setDefinition(sprintf('swiftmailer.configurator.transport.%s', $name), $configuratorDecorator) + ->setArguments(array( + sprintf('%%swiftmailer.mailer.%s.transport.smtp.local_domain%%', $name), + new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE), + )) + ; + $definitionDecorator = new DefinitionDecorator('swiftmailer.transport.smtp.abstract'); $container ->setDefinition(sprintf('swiftmailer.mailer.%s.transport.smtp', $name), $definitionDecorator) @@ -170,13 +182,29 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild ->addMethodCall('setEncryption', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.encryption%%', $name))) ->addMethodCall('setTimeout', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.timeout%%', $name))) ->addMethodCall('setSourceIp', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.source_ip%%', $name))) + ->setConfigurator(array(new Reference(sprintf('swiftmailer.configurator.transport.%s', $name)), 'configureLocalDomain')) ; + + if (isset($mailer['stream_options'])) { + $container->setParameter(sprintf('swiftmailer.mailer.%s.transport.smtp.stream_options', $name), $mailer['stream_options']); + $definitionDecorator->addMethodCall('setStreamOptions', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.stream_options%%', $name))); + } + $container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport)); } elseif ('sendmail' === $transport) { $bufferDecorator = new DefinitionDecorator('swiftmailer.transport.buffer.abstract'); $container ->setDefinition(sprintf('swiftmailer.mailer.%s.transport.buffer', $name), $bufferDecorator); + $configuratorDecorator = new DefinitionDecorator('swiftmailer.configurator.abstract'); + $container + ->setDefinition(sprintf('swiftmailer.configurator.transport.%s', $name), $configuratorDecorator) + ->setArguments(array( + sprintf('%%swiftmailer.mailer.%s.transport.smtp.local_domain%%', $name), + new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE), + )) + ; + $definitionDecorator = new DefinitionDecorator(sprintf('swiftmailer.transport.%s.abstract', $transport)); $container ->setDefinition(sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport), $definitionDecorator) @@ -184,7 +212,9 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild new Reference(sprintf('swiftmailer.mailer.%s.transport.buffer', $name)), new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)), )) + ->setConfigurator(array(new Reference(sprintf('swiftmailer.configurator.transport.%s', $name)), 'configureLocalDomain')) ; + $container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport)); } elseif ('mail' === $transport) { $definitionDecorator = new DefinitionDecorator(sprintf('swiftmailer.transport.%s.abstract', $transport)); diff --git a/Resources/config/schema/swiftmailer-1.0.xsd b/Resources/config/schema/swiftmailer-1.0.xsd index ea227fde..eb02fa5d 100644 --- a/Resources/config/schema/swiftmailer-1.0.xsd +++ b/Resources/config/schema/swiftmailer-1.0.xsd @@ -10,6 +10,7 @@ + @@ -21,6 +22,20 @@ + + + + + + + + + + + + + + @@ -31,6 +46,7 @@ + @@ -41,6 +57,7 @@ + diff --git a/Resources/config/swiftmailer.xml b/Resources/config/swiftmailer.xml index cd3cb4e2..aab240cf 100644 --- a/Resources/config/swiftmailer.xml +++ b/Resources/config/swiftmailer.xml @@ -27,6 +27,7 @@ Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector + Symfony\Bundle\SwiftmailerBundle\DependencyInjection\Configurator @@ -36,6 +37,8 @@ + + diff --git a/Tests/DependencyInjection/Fixtures/config/php/full.php b/Tests/DependencyInjection/Fixtures/config/php/full.php index 5b3cdc99..08b42b99 100644 --- a/Tests/DependencyInjection/Fixtures/config/php/full.php +++ b/Tests/DependencyInjection/Fixtures/config/php/full.php @@ -10,6 +10,7 @@ 'auth-mode' => 'login', 'timeout' => '1000', 'source_ip' => '127.0.0.1', + 'local_domain' => 'local.example.com', 'logging' => true, 'spool' => array('type' => 'memory'), 'delivery_address' => 'single@host.com', diff --git a/Tests/DependencyInjection/Fixtures/config/php/many_mailers.php b/Tests/DependencyInjection/Fixtures/config/php/many_mailers.php index 90254132..58bb98a6 100644 --- a/Tests/DependencyInjection/Fixtures/config/php/many_mailers.php +++ b/Tests/DependencyInjection/Fixtures/config/php/many_mailers.php @@ -13,6 +13,7 @@ 'auth-mode' => 'login', 'timeout' => '1000', 'source_ip' => '127.0.0.1', + 'local_domain' => 'first.example.org', 'logging' => true, 'sender_address' => 'first-sender@example.org', 'delivery_address' => 'first@example.org', @@ -31,6 +32,7 @@ 'auth-mode' => 'login', 'timeout' => '1000', 'source_ip' => '127.0.0.1', + 'local_domain' => 'second.example.org', 'logging' => true, 'spool' => array( 'type' => 'memory', @@ -51,6 +53,7 @@ 'auth-mode' => 'login', 'timeout' => '1000', 'source_ip' => '127.0.0.1', + 'local_domain' => 'third.example.org', 'logging' => true, 'spool' => array( 'type' => 'file', diff --git a/Tests/DependencyInjection/Fixtures/config/php/one_mailer.php b/Tests/DependencyInjection/Fixtures/config/php/one_mailer.php index f5989e9d..77592e14 100644 --- a/Tests/DependencyInjection/Fixtures/config/php/one_mailer.php +++ b/Tests/DependencyInjection/Fixtures/config/php/one_mailer.php @@ -13,6 +13,7 @@ 'auth-mode' => 'login', 'timeout' => '1000', 'source_ip' => '127.0.0.1', + 'local_domain' => 'local.example.org', ), ), )); diff --git a/Tests/DependencyInjection/Fixtures/config/php/sendmail.php b/Tests/DependencyInjection/Fixtures/config/php/sendmail.php index 77963c02..3325ca9d 100644 --- a/Tests/DependencyInjection/Fixtures/config/php/sendmail.php +++ b/Tests/DependencyInjection/Fixtures/config/php/sendmail.php @@ -2,4 +2,5 @@ $container->loadFromExtension('swiftmailer', array( 'transport' => 'sendmail', + 'local_domain' => 'local.example.org', )); diff --git a/Tests/DependencyInjection/Fixtures/config/php/smtp.php b/Tests/DependencyInjection/Fixtures/config/php/smtp.php index 61a5b6fa..bba0524f 100644 --- a/Tests/DependencyInjection/Fixtures/config/php/smtp.php +++ b/Tests/DependencyInjection/Fixtures/config/php/smtp.php @@ -11,4 +11,5 @@ 'timeout' => '1000', 'source_ip' => '127.0.0.1', 'logging' => true, + 'local_domain' => 'local.example.org', )); diff --git a/Tests/DependencyInjection/Fixtures/config/php/stream_options.php b/Tests/DependencyInjection/Fixtures/config/php/stream_options.php new file mode 100644 index 00000000..f379e1c4 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/stream_options.php @@ -0,0 +1,15 @@ +loadFromExtension('swiftmailer', array( + 'transport' => 'smtp', + 'host' => 'example.org', + 'port' => '12345', + 'source_ip' => '127.0.0.1', + 'stream_options' => array( + 'ssl' => array( + 'verify_peer' => true, + 'verify_depth' => 5, + 'cafile' => '/etc/ssl/cacert.pem', + 'CN_match' => 'ssl.example.com', + ), + ), +)); diff --git a/Tests/DependencyInjection/Fixtures/config/xml/full.xml b/Tests/DependencyInjection/Fixtures/config/xml/full.xml index f2a18705..263d2939 100644 --- a/Tests/DependencyInjection/Fixtures/config/xml/full.xml +++ b/Tests/DependencyInjection/Fixtures/config/xml/full.xml @@ -15,6 +15,7 @@ auth-mode="login" timeout="1000" source-ip="127.0.0.1" + local-domain="local.example.com" logging="true" delivery-address="single@host.com"> diff --git a/Tests/DependencyInjection/Fixtures/config/xml/many_mailers.xml b/Tests/DependencyInjection/Fixtures/config/xml/many_mailers.xml index 0a0bf27e..13922e55 100644 --- a/Tests/DependencyInjection/Fixtures/config/xml/many_mailers.xml +++ b/Tests/DependencyInjection/Fixtures/config/xml/many_mailers.xml @@ -16,6 +16,7 @@ auth-mode="login" timeout="1000" source-ip="127.0.0.1" + local-domain="first.example.org" logging="true" delivery-address="single@host.com"> @@ -30,6 +31,7 @@ auth-mode="login" timeout="1000" source-ip="127.0.0.1" + local-domain="second.example.org" logging="true" delivery-address="single@host.com"> @@ -44,6 +46,7 @@ auth-mode="login" timeout="1000" source-ip="127.0.0.1" + local-domain="third.example.org" logging="true" delivery-address="single@host.com"> diff --git a/Tests/DependencyInjection/Fixtures/config/xml/one_mailer.xml b/Tests/DependencyInjection/Fixtures/config/xml/one_mailer.xml index 046dd50f..a5d0f0cb 100644 --- a/Tests/DependencyInjection/Fixtures/config/xml/one_mailer.xml +++ b/Tests/DependencyInjection/Fixtures/config/xml/one_mailer.xml @@ -15,7 +15,8 @@ encryption="tls" auth-mode="login" timeout="1000" - source-ip="127.0.0.1"> + source-ip="127.0.0.1" + local-domain="local.example.org"> diff --git a/Tests/DependencyInjection/Fixtures/config/xml/sendmail.xml b/Tests/DependencyInjection/Fixtures/config/xml/sendmail.xml index 43ecab6a..a7b92fca 100644 --- a/Tests/DependencyInjection/Fixtures/config/xml/sendmail.xml +++ b/Tests/DependencyInjection/Fixtures/config/xml/sendmail.xml @@ -8,5 +8,6 @@ diff --git a/Tests/DependencyInjection/Fixtures/config/xml/smtp.xml b/Tests/DependencyInjection/Fixtures/config/xml/smtp.xml index d337f1ed..0026de8a 100644 --- a/Tests/DependencyInjection/Fixtures/config/xml/smtp.xml +++ b/Tests/DependencyInjection/Fixtures/config/xml/smtp.xml @@ -16,5 +16,7 @@ auth-mode="login" timeout="1000" source-ip="127.0.0.1" - logging="true"/> + local-domain="local.example.org" + logging="true"> + diff --git a/Tests/DependencyInjection/Fixtures/config/xml/stream_options.xml b/Tests/DependencyInjection/Fixtures/config/xml/stream_options.xml new file mode 100644 index 00000000..8e8eb632 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/stream_options.xml @@ -0,0 +1,22 @@ + + + + + + + true + 5 + /etc/ssl/cacert.pem + ssl.example.com + + + + diff --git a/Tests/DependencyInjection/Fixtures/config/yml/full.yml b/Tests/DependencyInjection/Fixtures/config/yml/full.yml index 7b5eea85..96a47b51 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/full.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/full.yml @@ -1,14 +1,15 @@ swiftmailer: - transport: smtp - username: user - password: pass - host: example.org - port: 12345 - encryption: tls - auth-mode: login - timeout: 1000 - source_ip: 127.0.0.1 - logging: true + transport: smtp + username: user + password: pass + host: example.org + port: 12345 + encryption: tls + auth-mode: login + timeout: 1000 + source_ip: 127.0.0.1 + local_domain: local.example.com + logging: true spool: type: memory delivery_address: single@host.com diff --git a/Tests/DependencyInjection/Fixtures/config/yml/many_mailers.yml b/Tests/DependencyInjection/Fixtures/config/yml/many_mailers.yml index b31f2014..ecf18f32 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/many_mailers.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/many_mailers.yml @@ -2,32 +2,34 @@ swiftmailer: default_mailer: secondary_mailer mailers: first_mailer: - transport: smtp - username: user_first - password: pass_first - host: example.org - port: 12345 - encryption: tls - auth-mode: login - timeout: 1000 - source_ip: 127.0.0.1 - logging: true - sender_address: first-sender@example.org + transport: smtp + username: user_first + password: pass_first + host: example.org + port: 12345 + encryption: tls + auth-mode: login + timeout: 1000 + source_ip: 127.0.0.1 + local_domain: first.example.org + logging: true + sender_address: first-sender@example.org delivery_address: first@example.org delivery_whitelist: - /firstfoo@.*/ - /.*@firstbar.com$/ secondary_mailer: - transport: smtp - username: user_secondary - password: pass_secondary - host: example.org - port: 54321 - encryption: tls - auth-mode: login - timeout: 1000 - source_ip: 127.0.0.1 - logging: true + transport: smtp + username: user_secondary + password: pass_secondary + host: example.org + port: 54321 + encryption: tls + auth-mode: login + timeout: 1000 + source_ip: 127.0.0.1 + local_domain: second.example.org + logging: true spool: type: memory delivery_address: secondary@example.org @@ -35,19 +37,20 @@ swiftmailer: - /secondaryfoo@.*/ - /.*@secondarybar.com$/ third_mailer: - transport: smtp - username: user_third - password: pass_third - host: example.org - port: 12345 - encryption: tls - auth-mode: login - timeout: 1000 - source_ip: 127.0.0.1 - logging: true + transport: smtp + username: user_third + password: pass_third + host: example.org + port: 12345 + encryption: tls + auth-mode: login + timeout: 1000 + source_ip: 127.0.0.1 + local_domain: third.example.org + logging: true spool: type: file - sender_address: third-sender@example.org + sender_address: third-sender@example.org delivery_address: third@example.org delivery_whitelist: - /thirdfoo@.*/ diff --git a/Tests/DependencyInjection/Fixtures/config/yml/one_mailer.yml b/Tests/DependencyInjection/Fixtures/config/yml/one_mailer.yml index 6cfc7eb9..c868bd6f 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/one_mailer.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/one_mailer.yml @@ -11,3 +11,4 @@ swiftmailer: auth-mode: login timeout: 1000 source_ip: 127.0.0.1 + local_domain: local.example.org diff --git a/Tests/DependencyInjection/Fixtures/config/yml/sendmail.yml b/Tests/DependencyInjection/Fixtures/config/yml/sendmail.yml index c1e8ed2c..b78f2079 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/sendmail.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/sendmail.yml @@ -1,2 +1,3 @@ swiftmailer: transport: sendmail + local_domain: local.example.org diff --git a/Tests/DependencyInjection/Fixtures/config/yml/smtp.yml b/Tests/DependencyInjection/Fixtures/config/yml/smtp.yml index b508e4a1..49a63ca0 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/smtp.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/smtp.yml @@ -1,11 +1,12 @@ swiftmailer: - transport: smtp - username: user - password: pass - host: example.org - port: 12345 - encryption: tls - auth-mode: login - timeout: 1000 - source_ip: 127.0.0.1 - logging: true + transport: smtp + username: user + password: pass + host: example.org + port: 12345 + encryption: tls + auth-mode: login + timeout: 1000 + source_ip: 127.0.0.1 + local_domain: local.example.org + logging: true diff --git a/Tests/DependencyInjection/Fixtures/config/yml/stream_options.yml b/Tests/DependencyInjection/Fixtures/config/yml/stream_options.yml new file mode 100644 index 00000000..315c3860 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/stream_options.yml @@ -0,0 +1,11 @@ +swiftmailer: + transport: smtp + host: example.org + port: 12345 + source_ip: 127.0.0.1 + stream_options: + ssl: + verify_peer: true + cafile: /etc/ssl/cacert.pem + verify_depth: 5 + CN_match: ssl.example.com diff --git a/Tests/DependencyInjection/SwiftmailerExtensionTest.php b/Tests/DependencyInjection/SwiftmailerExtensionTest.php index 6058cd2a..8cbdf4ef 100644 --- a/Tests/DependencyInjection/SwiftmailerExtensionTest.php +++ b/Tests/DependencyInjection/SwiftmailerExtensionTest.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\SwiftmailerBundle\Tests\DependencyInjection; use Symfony\Bundle\SwiftmailerBundle\DependencyInjection\SwiftmailerExtension; +use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; @@ -34,10 +35,20 @@ public function getConfigTypes() */ public function testDefaultConfig($type) { - $container = $this->loadContainerFromFile('empty', $type); + $requestContext = $this->getMock('Symfony\Component\Routing\RequestContext', array('getHost')); + $requestContext->expects($this->once())->method('getHost')->will($this->returnValue('example.org')); + $services = array('router.request_context' => $requestContext); + + $container = $this->loadContainerFromFile('empty', $type, $services); $this->assertEquals('swiftmailer.mailer.default.transport', (string) $container->getAlias('swiftmailer.transport')); $this->assertEquals('swiftmailer.mailer.default.transport.smtp', (string) $container->getAlias('swiftmailer.mailer.default.transport')); + + $this->assertEquals('localhost', $container->getParameter('swiftmailer.mailer.default.transport.smtp.host')); + $this->assertEquals(25, $container->getParameter('swiftmailer.mailer.default.transport.smtp.port')); + $this->assertFalse($container->hasParameter('swiftmailer.mailer.default.transport.smtp.stream_options')); + + $this->assertEquals('example.org', $container->get('swiftmailer.mailer.default.transport')->getLocalDomain()); } /** @@ -54,10 +65,17 @@ public function testMailerNullConfig($type) */ public function testSendmailConfig($type) { - $container = $this->loadContainerFromFile('sendmail', $type); + // Local domain is specified explicitly, so the request context host is ignored. + $requestContext = $this->getMock('Symfony\Component\Routing\RequestContext', array('getHost')); + $requestContext->expects($this->any())->method('getHost')->will($this->returnValue('example.org')); + $services = array('router.request_context' => $requestContext); + + $container = $this->loadContainerFromFile('sendmail', $type, $services); $this->assertEquals('swiftmailer.mailer.default.transport', (string) $container->getAlias('swiftmailer.transport')); $this->assertEquals('swiftmailer.mailer.default.transport.sendmail', (string) $container->getAlias('swiftmailer.mailer.default.transport')); + + $this->assertEquals('local.example.org', $container->get('swiftmailer.mailer.default.transport')->getLocalDomain()); } /** @@ -102,6 +120,7 @@ public function testFull($type) $this->assertEquals('login', $container->getParameter('swiftmailer.mailer.default.transport.smtp.auth_mode')); $this->assertEquals('1000', $container->getParameter('swiftmailer.mailer.default.transport.smtp.timeout')); $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.mailer.default.transport.smtp.source_ip')); + $this->assertEquals('local.example.com', $container->getParameter('swiftmailer.mailer.default.transport.smtp.local_domain')); $this->assertSame(array('swiftmailer.default.plugin' => array(array())), $container->getDefinition('swiftmailer.mailer.default.plugin.redirecting')->getTags()); $this->assertSame('single@host.com', $container->getParameter('swiftmailer.mailer.default.single_address')); $this->assertEquals(array('/foo@.*/', '/.*@bar.com$/'), $container->getParameter('swiftmailer.mailer.default.delivery_whitelist')); @@ -126,6 +145,8 @@ public function testManyMailers($type) $this->assertEquals('login', $container->getParameter('swiftmailer.mailer.first_mailer.transport.smtp.auth_mode')); $this->assertEquals('1000', $container->getParameter('swiftmailer.mailer.first_mailer.transport.smtp.timeout')); $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.mailer.first_mailer.transport.smtp.source_ip')); + $this->assertEquals('first.example.org', $container->getParameter('swiftmailer.mailer.first_mailer.transport.smtp.local_domain')); + $this->assertEquals('example.org', $container->getParameter('swiftmailer.mailer.secondary_mailer.transport.smtp.host')); $this->assertEquals('54321', $container->getParameter('swiftmailer.mailer.secondary_mailer.transport.smtp.port')); $this->assertEquals('tls', $container->getParameter('swiftmailer.mailer.secondary_mailer.transport.smtp.encryption')); @@ -133,7 +154,9 @@ public function testManyMailers($type) $this->assertEquals('pass_secondary', $container->getParameter('swiftmailer.mailer.secondary_mailer.transport.smtp.password')); $this->assertEquals('login', $container->getParameter('swiftmailer.mailer.secondary_mailer.transport.smtp.auth_mode')); $this->assertEquals('1000', $container->getParameter('swiftmailer.mailer.secondary_mailer.transport.smtp.timeout')); - $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.mailer.third_mailer.transport.smtp.source_ip')); + $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.mailer.secondary_mailer.transport.smtp.source_ip')); + $this->assertEquals('second.example.org', $container->getParameter('swiftmailer.mailer.secondary_mailer.transport.smtp.local_domain')); + $this->assertEquals('example.org', $container->getParameter('swiftmailer.mailer.third_mailer.transport.smtp.host')); $this->assertEquals('12345', $container->getParameter('swiftmailer.mailer.third_mailer.transport.smtp.port')); $this->assertEquals('tls', $container->getParameter('swiftmailer.mailer.third_mailer.transport.smtp.encryption')); @@ -142,7 +165,9 @@ public function testManyMailers($type) $this->assertEquals('login', $container->getParameter('swiftmailer.mailer.third_mailer.transport.smtp.auth_mode')); $this->assertEquals('1000', $container->getParameter('swiftmailer.mailer.third_mailer.transport.smtp.timeout')); $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.mailer.third_mailer.transport.smtp.source_ip')); + $this->assertEquals('third.example.org', $container->getParameter('swiftmailer.mailer.third_mailer.transport.smtp.local_domain')); } + /** * @dataProvider getConfigTypes */ @@ -175,6 +200,8 @@ public function testOneMailer($type) $this->assertEquals('pass', $container->getParameter('swiftmailer.mailer.main_mailer.transport.smtp.password')); $this->assertEquals('login', $container->getParameter('swiftmailer.mailer.main_mailer.transport.smtp.auth_mode')); $this->assertEquals('1000', $container->getParameter('swiftmailer.mailer.main_mailer.transport.smtp.timeout')); + $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.mailer.main_mailer.transport.smtp.source_ip')); + $this->assertEquals('local.example.org', $container->getParameter('swiftmailer.mailer.main_mailer.transport.smtp.local_domain')); } /** @@ -247,6 +274,23 @@ public function testSmtpConfig($type) $this->assertEquals('login', $container->getParameter('swiftmailer.mailer.default.transport.smtp.auth_mode')); $this->assertEquals('1000', $container->getParameter('swiftmailer.mailer.default.transport.smtp.timeout')); $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.mailer.default.transport.smtp.source_ip')); + $this->assertEquals('local.example.org', $container->getParameter('swiftmailer.mailer.default.transport.smtp.local_domain')); + } + + /** + * @dataProvider getConfigTypes + */ + public function testStreamOptions($type) + { + if (!method_exists('Swift_Transport_EsmtpTransport', 'setStreamOptions')) { + $this->markTestSkipped('This test requires Swiftmailer 5.4.2 or later.'); + } + + $container = $this->loadContainerFromFile('stream_options', $type); + $this->assertEquals('example.org', $container->getParameter('swiftmailer.mailer.default.transport.smtp.host')); + $this->assertEquals('12345', $container->getParameter('swiftmailer.mailer.default.transport.smtp.port')); + $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.mailer.default.transport.smtp.source_ip')); + $this->assertEquals(array('ssl' => array('verify_peer' => true, 'verify_depth' => 5, 'cafile' => '/etc/ssl/cacert.pem', 'CN_match' => 'ssl.example.com')), $container->getParameter('swiftmailer.mailer.default.transport.smtp.stream_options')); } /** @@ -310,16 +354,21 @@ public function testSenderAddress($type) /** * @param string $file * @param string $type + * @param array $services * * @return ContainerBuilder */ - private function loadContainerFromFile($file, $type) + private function loadContainerFromFile($file, $type, array $services = array()) { $container = new ContainerBuilder(); $container->setParameter('kernel.debug', false); $container->setParameter('kernel.cache_dir', '/tmp'); + foreach ($services as $id => $service) { + $container->set($id, $service); + } + $container->registerExtension(new SwiftmailerExtension()); $locator = new FileLocator(__DIR__.'/Fixtures/config/'.$type); @@ -339,7 +388,9 @@ private function loadContainerFromFile($file, $type) $loader->load($file.'.'.$type); - $container->getCompilerPassConfig()->setOptimizationPasses(array()); + $container->getCompilerPassConfig()->setOptimizationPasses(array( + new ResolveDefinitionTemplatesPass() + )); $container->getCompilerPassConfig()->setRemovingPasses(array()); $container->compile(); From 09488b9e2fbd5186dee455bf2f6ffe409b09a295 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Tue, 25 Oct 2016 22:39:57 +0200 Subject: [PATCH 2/3] Rename Configurator to AbstractSmtpTransportConfigurator --- ...tor.php => AbstractSmtpTransportConfigurator.php} | 4 ++-- DependencyInjection/SwiftmailerExtension.php | 12 ++++++------ Resources/config/swiftmailer.xml | 5 ++--- 3 files changed, 10 insertions(+), 11 deletions(-) rename DependencyInjection/{Configurator.php => AbstractSmtpTransportConfigurator.php} (90%) diff --git a/DependencyInjection/Configurator.php b/DependencyInjection/AbstractSmtpTransportConfigurator.php similarity index 90% rename from DependencyInjection/Configurator.php rename to DependencyInjection/AbstractSmtpTransportConfigurator.php index 541574b6..714cc0ba 100644 --- a/DependencyInjection/Configurator.php +++ b/DependencyInjection/AbstractSmtpTransportConfigurator.php @@ -16,7 +16,7 @@ /** * Service configurator. */ -class Configurator +class AbstractSmtpTransportConfigurator { /** * @var string @@ -40,7 +40,7 @@ public function __construct($localDomain, RequestContext $requestContext = null) $this->requestContext = $requestContext; } - public function configureLocalDomain(\Swift_Transport_AbstractSmtpTransport $transport) + public function configure(\Swift_Transport_AbstractSmtpTransport $transport) { if ($this->localDomain) { $transport->setLocalDomain($this->localDomain); diff --git a/DependencyInjection/SwiftmailerExtension.php b/DependencyInjection/SwiftmailerExtension.php index 797f2670..066d751f 100644 --- a/DependencyInjection/SwiftmailerExtension.php +++ b/DependencyInjection/SwiftmailerExtension.php @@ -160,9 +160,9 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild $container ->setDefinition(sprintf('swiftmailer.mailer.%s.transport.buffer', $name), $bufferDecorator); - $configuratorDecorator = new DefinitionDecorator('swiftmailer.configurator.abstract'); + $configuratorDecorator = new DefinitionDecorator('swiftmailer.transport.smtp.configurator.abstract'); $container - ->setDefinition(sprintf('swiftmailer.configurator.transport.%s', $name), $configuratorDecorator) + ->setDefinition(sprintf('swiftmailer.transport.configurator.%s', $name), $configuratorDecorator) ->setArguments(array( sprintf('%%swiftmailer.mailer.%s.transport.smtp.local_domain%%', $name), new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE), @@ -182,7 +182,7 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild ->addMethodCall('setEncryption', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.encryption%%', $name))) ->addMethodCall('setTimeout', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.timeout%%', $name))) ->addMethodCall('setSourceIp', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.source_ip%%', $name))) - ->setConfigurator(array(new Reference(sprintf('swiftmailer.configurator.transport.%s', $name)), 'configureLocalDomain')) + ->setConfigurator(array(new Reference(sprintf('swiftmailer.transport.configurator.%s', $name)), 'configure')) ; if (isset($mailer['stream_options'])) { @@ -196,9 +196,9 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild $container ->setDefinition(sprintf('swiftmailer.mailer.%s.transport.buffer', $name), $bufferDecorator); - $configuratorDecorator = new DefinitionDecorator('swiftmailer.configurator.abstract'); + $configuratorDecorator = new DefinitionDecorator('swiftmailer.transport.smtp.configurator.abstract'); $container - ->setDefinition(sprintf('swiftmailer.configurator.transport.%s', $name), $configuratorDecorator) + ->setDefinition(sprintf('swiftmailer.transport.configurator.%s', $name), $configuratorDecorator) ->setArguments(array( sprintf('%%swiftmailer.mailer.%s.transport.smtp.local_domain%%', $name), new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE), @@ -212,7 +212,7 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild new Reference(sprintf('swiftmailer.mailer.%s.transport.buffer', $name)), new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)), )) - ->setConfigurator(array(new Reference(sprintf('swiftmailer.configurator.transport.%s', $name)), 'configureLocalDomain')) + ->setConfigurator(array(new Reference(sprintf('swiftmailer.transport.configurator.%s', $name)), 'configure')) ; $container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport)); diff --git a/Resources/config/swiftmailer.xml b/Resources/config/swiftmailer.xml index aab240cf..4c25a51d 100644 --- a/Resources/config/swiftmailer.xml +++ b/Resources/config/swiftmailer.xml @@ -27,7 +27,6 @@ Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector - Symfony\Bundle\SwiftmailerBundle\DependencyInjection\Configurator @@ -35,9 +34,9 @@ - + - + From 84e4aba8f0461454354b6d4a57f08535c093ba89 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 27 Oct 2016 07:50:22 +0200 Subject: [PATCH 3/3] Code style fixes --- ...ator.php => SmtpTransportConfigurator.php} | 24 +++++-------------- Resources/config/swiftmailer.xml | 2 +- 2 files changed, 7 insertions(+), 19 deletions(-) rename DependencyInjection/{AbstractSmtpTransportConfigurator.php => SmtpTransportConfigurator.php} (72%) diff --git a/DependencyInjection/AbstractSmtpTransportConfigurator.php b/DependencyInjection/SmtpTransportConfigurator.php similarity index 72% rename from DependencyInjection/AbstractSmtpTransportConfigurator.php rename to DependencyInjection/SmtpTransportConfigurator.php index 714cc0ba..6135d1cf 100644 --- a/DependencyInjection/AbstractSmtpTransportConfigurator.php +++ b/DependencyInjection/SmtpTransportConfigurator.php @@ -13,33 +13,21 @@ use Symfony\Component\Routing\RequestContext; -/** - * Service configurator. - */ -class AbstractSmtpTransportConfigurator +class SmtpTransportConfigurator { - /** - * @var string - */ - protected $localDomain; + private $localDomain; - /** - * @var RequestContext - */ - protected $requestContext; + private $requestContext; - /** - * Sets the local domain based on the current request context. - * - * @param string $localDomain Fallback value if there is no request context. - * @param RequestContext $requestContext - */ public function __construct($localDomain, RequestContext $requestContext = null) { $this->localDomain = $localDomain; $this->requestContext = $requestContext; } + /** + * Sets the local domain based on the current request context. + */ public function configure(\Swift_Transport_AbstractSmtpTransport $transport) { if ($this->localDomain) { diff --git a/Resources/config/swiftmailer.xml b/Resources/config/swiftmailer.xml index 4c25a51d..1e5312ea 100644 --- a/Resources/config/swiftmailer.xml +++ b/Resources/config/swiftmailer.xml @@ -34,7 +34,7 @@ - +