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

Support setLocalDomain() and setStreamOptions() #141

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
51 changes: 51 additions & 0 deletions DependencyInjection/Configurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about renaming if to just configure, so that we can use this for other things in the future if needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the method name is Configurator::configure(), there is no obvious name to use if we some day want to configure other classes. So I have also changed the class name, so the method name becomes AbstractSmtpConfigurator::configure(). Ok?

{
if ($this->localDomain) {
$transport->setLocalDomain($this->localDomain);
} elseif ($this->requestContext) {
$transport->setLocalDomain($this->requestContext->getHost());
}
}
}
32 changes: 31 additions & 1 deletion DependencyInjection/SwiftmailerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -170,21 +182,39 @@ 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)
->setArguments(array(
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));
Expand Down
17 changes: 17 additions & 0 deletions Resources/config/schema/swiftmailer-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<xsd:complexType name="config">
<xsd:sequence>
<xsd:element name="mailer" type="mailer" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="stream-options" type="stream-options" minOccurs="0" maxOccurs="1" />
<xsd:element name="antiflood" type="antiflood" minOccurs="0" maxOccurs="1" />
<xsd:element name="spool" type="spool" minOccurs="0" maxOccurs="1" />
<xsd:element name="delivery-whitelist-pattern" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
Expand All @@ -21,6 +22,20 @@
<xsd:attributeGroup ref="mailer-config" />
</xsd:complexType>

<xsd:complexType name="stream-options">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element name="stream-option" type="stream-option" />
</xsd:choice>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="stream-option" mixed="true">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="stream-option" type="stream-option" />
</xsd:choice>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>

<xsd:attributeGroup name="mailer-config">
<xsd:attribute name="username" type="xsd:string" />
<xsd:attribute name="password" type="xsd:string" />
Expand All @@ -31,6 +46,7 @@
<xsd:attribute name="timeout" type="xsd:string"/>
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="source-ip" type="xsd:string"/>
<xsd:attribute name="local-domain" type="xsd:string"/>
<xsd:attribute name="transport" type="xsd:string" />
<xsd:attribute name="logging" type="xsd:string" />
<xsd:attribute name="delivery-address" type="xsd:string" />
Expand All @@ -41,6 +57,7 @@

<xsd:complexType name="mailer">
<xsd:sequence>
<xsd:element name="stream-options" type="stream-options" minOccurs="0" maxOccurs="1" />
<xsd:element name="antiflood" type="antiflood" minOccurs="0" maxOccurs="1" />
<xsd:element name="spool" type="spool" minOccurs="0" maxOccurs="1" />
<xsd:element name="delivery-whitelist-pattern" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/swiftmailer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<parameter key="swiftmailer.email_sender.listener.class">Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener</parameter>

<parameter key="swiftmailer.data_collector.class">Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector</parameter>
<parameter key="swiftmailer.configurator.class">Symfony\Bundle\SwiftmailerBundle\DependencyInjection\Configurator</parameter>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't define class name as parameter anymore, so this one need to be inlined

</parameters>

<services>
Expand All @@ -36,6 +37,8 @@

<service id="swiftmailer.transport.sendmail.abstract" class="%swiftmailer.transport.sendmail.class%" abstract="true" public="false" />

<service id="swiftmailer.configurator.abstract" class="%swiftmailer.configurator.class%" abstract="true" public="false" />

<service id="swiftmailer.transport.mail.abstract" class="%swiftmailer.transport.mail.class%" abstract="true" public="false">
<argument type="service" id="swiftmailer.transport.mailinvoker" />
</service>
Expand Down
1 change: 1 addition & 0 deletions Tests/DependencyInjection/Fixtures/config/php/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'auth-mode' => 'login',
'timeout' => '1000',
'source_ip' => '127.0.0.1',
'local_domain' => 'local.example.org',
),
),
));
1 change: 1 addition & 0 deletions Tests/DependencyInjection/Fixtures/config/php/sendmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

$container->loadFromExtension('swiftmailer', array(
'transport' => 'sendmail',
'local_domain' => 'local.example.org',
));
1 change: 1 addition & 0 deletions Tests/DependencyInjection/Fixtures/config/php/smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
'timeout' => '1000',
'source_ip' => '127.0.0.1',
'logging' => true,
'local_domain' => 'local.example.org',
));
15 changes: 15 additions & 0 deletions Tests/DependencyInjection/Fixtures/config/php/stream_options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
$container->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',
),
),
));
1 change: 1 addition & 0 deletions Tests/DependencyInjection/Fixtures/config/xml/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<swiftmailer:antiflood/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<swiftmailer:antiflood/>
Expand All @@ -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">
<swiftmailer:spool type="memory"/>
Expand All @@ -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">
</swiftmailer:mailer>
Expand Down
3 changes: 2 additions & 1 deletion Tests/DependencyInjection/Fixtures/config/xml/one_mailer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
</swiftmailer:mailer>
</swiftmailer:config>
</container>
1 change: 1 addition & 0 deletions Tests/DependencyInjection/Fixtures/config/xml/sendmail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@

<swiftmailer:config
transport="sendmail"
local-domain="local.example.org"
/>
</container>
4 changes: 3 additions & 1 deletion Tests/DependencyInjection/Fixtures/config/xml/smtp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
auth-mode="login"
timeout="1000"
source-ip="127.0.0.1"
logging="true"/>
local-domain="local.example.org"
logging="true">
</swiftmailer:config>
</container>
22 changes: 22 additions & 0 deletions Tests/DependencyInjection/Fixtures/config/xml/stream_options.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd">

<swiftmailer:config
transport="smtp"
host="example.org"
port="12345"
source-ip="127.0.0.1">
<swiftmailer:stream-options>
<swiftmailer:stream-option name="ssl">
<swiftmailer:stream-option name="verify_peer">true</swiftmailer:stream-option>
<swiftmailer:stream-option name="verify_depth">5</swiftmailer:stream-option>
<swiftmailer:stream-option name="cafile">/etc/ssl/cacert.pem</swiftmailer:stream-option>
<swiftmailer:stream-option name="CN_match">ssl.example.com</swiftmailer:stream-option>
</swiftmailer:stream-option>
</swiftmailer:stream-options>
</swiftmailer:config>
</container>
Loading